我需要随邮件发送一份pdf,可以吗?
$to = "xxx";
$subject = "Subject" ;
$message = 'Example message with <b>html</b>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: xxx <xxx>' . "\r\n";
mail($to,$subject,$message,$headers);
我错过了什么?
ContactRequestSubmittedName:'.$name.'
Email:'.$email.'
Subject:'.$subject.'
Message:
'.$message.'
';$headers="From:$fromName"."";if(!empty($uploadedFile)&&file_exists($uploadedFile)){$semi_rand=md5(time());$mime_boundary="==Multipart_Boundary_x{$semi_rand}x";$headers.="\nMIME-Version:1.0\n"."Content-Type:multipart/mixed;\n"."boundary=\"{$mime_boundary}\"";$message="--{$mime_boundary}\n"."Content-Type:text/html;charset=\"UTF-8\"\n"."Content-Transfer-Encoding:7bit\n\n".$htmlContent."\n\n";if(is_file($uploadedFile)){$message.="--{$mime_boundary}\n";$fp=@fopen($uploadedFile,"rb");$data=@fread($fp,filesize($uploadedFile));@fclose($fp);$data=chunk_split(base64_encode($data));$message.="Content-Type:application/octet-stream;name=\"".basename($uploadedFile)."\"\n"."Content-Description:".basename($uploadedFile)."\n"."Content-Disposition:attachment;\n"."filename=\"".basename($uploadedFile)."\";size=".filesize($uploadedFile).";\n"."Content-Transfer-Encoding:base64\n\n".$data."\n\n";}$message.="--{$mime_boundary}--";$returnpath="-f".$email;$mail=mail($toEmail,$emailSubject,$message,$headers,$returnpath);@unlink($uploadedFile);}else{$headers.="\r\n"."MIME-Version:1.0";$headers.="\r\n"."Content-type:text/html;charset=UTF-8";$mail=mail($toEmail,$emailSubject,$htmlContent,$headers);}if($mail){$statusMsg='Yourcontactrequesthasbeensubmittedsuccessfully!';$msgClass='succdiv';$postData='';}else{$statusMsg='Yourcontactrequestsubmissionfailed,pleasetryagain.';}}}}else{$statusMsg='Pleasefillallthefields.';}}?>">"placeholder="Name"required="">"placeholder="Emailaddress"required="">"placeholder="Subject"required="">[Source][1]
https://www.findinall.com/blog/how-to-test-mail-and-send-attachment-in-mail/
Swiftmailer是另一个易于使用的脚本,可以自动防止电子邮件注入,使附件变得轻而易举。我也强烈反对使用PHP内置的mail()函数。
使用方法:
下载Swiftmailer,并将lib文件夹放在你的项目中
使用require_once 'lib/swift_required.php'包含主文件;
现在当你需要邮寄时添加代码:
// Create the message
$message = Swift_Message::newInstance()
->setSubject('Your subject')
->setFrom(array('webmaster@mysite.com' => 'Web Master'))
->setTo(array('receiver@example.com'))
->setBody('Here is the message itself')
->attach(Swift_Attachment::fromPath('myPDF.pdf'));
//send the message
$mailer->send($message);
更多信息和选项可以在Swiftmailer文档中找到。
您可以尝试使用以下代码:
$filename = 'myfile';
$path = 'your path goes here';
$file = $path . "/" . $filename;
$mailto = 'mail@mail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: name <test@test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
编辑14 - 6 - 2018
为更多的可读性在一些电子邮件提供商
使用
$body .= $eol。美元的消息。eol美元。eol美元;而且
$body .= $eol。美元的内容。eol美元。eol美元;
HTML代码:
<form enctype="multipart/form-data" method="POST" action="">
<label>Your Name <input type="text" name="sender_name" /> </label>
<label>Your Email <input type="email" name="sender_email" /> </label>
<label>Your Contact Number <input type="tel" name="contactnumber" /> </label>
<label>Subject <input type="text" name="subject" /> </label>
<label>Message <textarea name="description"></textarea> </label>
<label>Attachment <input type="file" name="attachment" /></label>
<label><input type="submit" name="button" value="Submit" /></label>
</form>
PHP代码:
<?php
if($_POST['button']){
{
//Server Variables
$server_name = "Your Name";
$server_mail = "your_mail@domain.com";
//Name Attributes of HTML FORM
$sender_email = "sender_email";
$sender_name = "sender_name";
$contact = "contactnumber";
$mail_subject = "subject";
$input_file = "attachment";
$message = "description";
//Fetching HTML Values
$sender_name = $_POST[$sender_name];
$sender_mail = $_POST[$sender_email];
$message = $_POST[$message];
$contact= $_POST[$contact];
$mail_subject = $_POST[$mail_subject];
//Checking if File is uploaded
if(isset($_FILES[$input_file]))
{
//Main Content
$main_subject = "Subject seen on server's mail";
$main_body = "Hello $server_name,<br><br>
$sender_name ,contacted you through your website and the details are as below: <br><br>
Name : $sender_name <br>
Contact Number : $contact <br>
Email : $sender_mail <br>
Subject : $mail_subject <br>
Message : $message.";
//Reply Content
$reply_subject = "Subject seen on sender's mail";
$reply_body = "Hello $sender_name,<br>
\t Thank you for filling the contact form. We will revert back to you shortly.<br><br>
This is an auto generated mail sent from our Mail Server.<br>
Please do not reply to this mail.<br>
Regards<br>
$server_name";
//#############################DO NOT CHANGE ANYTHING BELOW THIS LINE#############################
$filename= $_FILES[$input_file]['name'];
$file = chunk_split(base64_encode(file_get_contents($_FILES[$input_file]['tmp_name'])));
$uid = md5(uniqid(time()));
//Sending mail to Server
$retval = mail($server_mail, $main_subject, "--$uid\r\nContent-type:text/html; charset=iso-8859-1\r\nContent-Transfer-Encoding: 7bit\r\n\r\n $main_body \r\n\r\n--$uid\r\nContent-Type: application/octet-stream; name=\"$filename\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"$filename\"\r\n\r\n$file\r\n\r\n--$uid--", "From: $sender_name <$sender_mail>\r\nReply-To: $sender_mail\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$uid\"\r\n\r\n");
//Sending mail to Sender
$retval = mail($sender_mail, $reply_subject, $reply_body , "From: $server_name<$server_mail>\r\nMIME-Version: 1.0\r\nContent-type: text/html\r\n");
//#############################DO NOT CHANGE ANYTHING ABOVE THIS LINE#############################
//Output
if ($retval == true) {
echo "Message sent successfully...";
echo "<script>window.location.replace('index.html');</script>";
} else {
echo "Error<br>";
echo "Message could not be sent...Try again later";
echo "<script>window.location.replace('index.html');</script>";
}
}else{
echo "Error<br>";
echo "File Not Found";
}
}else{
echo "Error<br>";
echo "Unauthorised Access";
}