如何使用PHP邮件功能将PDF附加到电子邮件

前端之家收集整理的这篇文章主要介绍了如何使用PHP邮件功能将PDF附加到电子邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 PHP邮件功能发送电子邮件,但我想添加一个指定的PDF文件作为电子邮件文件附件.我该怎么办?

这是我现在的代码

$to = "me@myemail.com";
$subject = "My message subject";
$message = "Hello,\n\nThis is sending a text only email,but I would like to add a PDF attachment if possible.";
$from = "Jane Doe <janedoe@myemail.com>";

$headers = "From:" . $from; 
mail($to,$subject,$message,$headers);

echo "Mail Sent!";
您应该考虑使用诸如 PHPMailer之类的PHP邮件库,这样可以使发送邮件的过程更简单和更好.

这里是一个如何使用PHPMailer的例子,它真的很简单!

<?PHP

require_once('../class.PHPmailer.PHP');

$mail             = new PHPMailer(); // defaults to using PHP "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com','First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address,"John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(),basic";

$mail->AltBody    = "To view the message,please use an HTML compatible email viewer!"; // optional,comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/PHPmailer.gif");      // attachment
$mail->AddAttachment("images/PHPmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

PHPMailer的替代品是http://swiftmailer.org/

原文链接:https://www.f2er.com/php/139824.html

猜你在找的PHP相关文章