如何使用PHP邮件功能将PDF附加到电子邮件 [英] How to attach PDF to email using PHP mail function
本文介绍了如何使用PHP邮件功能将PDF附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用PHP邮件功能发送电子邮件,但我想添加一个指定的PDF文件作为电子邮件的文件附件。我该怎么做?
I am sending an email using PHP mail function, but I would like to add a specified PDF file as a file attachment to the email. How would I do that?
这是我现在的代码:
$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!";
推荐答案
您应该考虑使用PHP邮件库,一个href =https://github.com/PHPMailer/PHPMailer =noreferrer> PHPMailer ,这将使程序发送邮件更简单和更好。
You should consider using a PHP mail library such as PHPMailer which would make the procedure to send mail much simpler and better.
这是一个如何使用PHPMailer的例子,这真的很简单!
Here's an example of how to use PHPMailer, it's really simple!
<?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/
An alternative to PHPMailer is http://swiftmailer.org/
这篇关于如何使用PHP邮件功能将PDF附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文