我需要随邮件发送一份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);
我错过了什么?
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文档中找到。
我同意评论中的@MihaiIorga -使用PHPMailer脚本。听起来你拒绝是因为你想要更简单的选择。相信我,比起自己使用PHP内置的mail()函数,PHPMailer是一个更简单的选择。PHP的mail()函数真的不是很好。
使用PHPMailer:
下载PHPMailer脚本:http://github.com/PHPMailer/PHPMailer
提取归档文件并将脚本文件夹复制到项目中方便的位置。
包括主脚本文件——require_once('path/to/file/class.phpmailer.php');
现在,发送带有附件的电子邮件从异常困难变得异常简单:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();
只有一行$email-> addatattachment ();——再简单不过了。
如果使用PHP的mail()函数,则需要编写大量代码,并且可能会遇到许多很难找到的错误。
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";
}
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/