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

标签 email email-attachments php

我正在使用 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!";

最佳答案

您应该考虑使用 PHP 邮件库,例如 PHPMailer这将使发送邮件的过程更加简单和更好。

下面是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/

关于email - 如何使用 PHP 邮件功能将 PDF 附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10606558/

相关文章:

php - fopen : failed to open stream

php - 为什么我的 symfony 路由不起作用?

python - 尝试使用 Beaglebone Black 发送电子邮件时收到 getaddrinfo [Errno -2]

java邮件smtp连接问题

c# - 使用 .NET 生成 HTML 电子邮件

java - 在单击电子邮件之前发送显示附件图标的电子邮件中的内嵌图像?

html - 只获取 HTML 正文,没有标签

java - Xpages - java.lang.NoSuchMethodError : javax/mail/internet/MimeBodyPart. AttachFile(Ljava/io/File;)V

jakarta-mail - JavaMail 附件名称问题

php - 使用其他两个填充字段的 DateDiff 结果更新字段