Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 如何使用PHP邮件功能将PDF附加到电子邮件_Email_Email Attachments_Php - Fatal编程技术网

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

Email 如何使用PHP邮件功能将PDF附加到电子邮件,email,email-attachments,php,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

我正在使用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!";
$to=”me@myemail.com";
$subject=“我的邮件主题”;
$message=“您好,\n\n这是发送纯文本电子邮件,但如果可能,我想添加PDF附件。”;
$from=“Jane Doe”;
$headers=“From:”$从…起
邮件($to、$subject、$message、$headers);
回音“邮件已发送!”;

简单的回答:不要这样做。手工制作MIME电子邮件是一件痛苦的事情,而且很容易出错


相反,使用或。用附件做附件几乎是微不足道的,在某些情况下,你可以得到更好的反馈,V.S.(邮件)(E/P>< P>)。你应该考虑使用一个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的另一种替代方法是

,以消除弃用错误

替换

$body             = eregi_replace("[\]",'',$body);


[请按照此链接生成动态PDF并发送邮件][1][1]:如果我的附件存储在远程服务器上,该怎么办?是否有任何变化或它应该工作?不支持远程文件:
从文件系统上的路径添加附件
提前使用file\u get\u contents(),@paulalexandru…?:/你还没有回答这个问题。你只是提出了一个积极的解决办法。用户需要PHP邮件功能发送pdf.+@Nebula我不知道这是如何被接受的答案。请再次阅读问题!
$body             = preg_replace('/\.([^\.]*$)/i','',$body);