PHP-电子邮件附件

时间:2019-01-25 13:47:21

标签: php email email-attachments

在我的网站上,我可以选择创建pdf文件,并将其作为附件发送到电子邮件中。

$document = new Dompdf();
$document->loadHtml($file_content);
$document->setPaper('A4', 'portrait');
$document->render();
$output = $document->output();
file_put_contents('../docs/purchase_orders/'.$file_name.'.pdf', $output);

$file = '../docs/purchase_orders/'.$file_name.'.pdf';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$subject = "New Purchase Order.";

$headers = "From: sales@example.com" . "\r\n";
$headers .= "Reply-To: sales@example.com" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";
$headers .= "Bcc: sales@example.com" . "\r\n";

$message = "--".$uid."\r\n";
$message .= "Content-type:text/html; charset=UTF-8" . "\r\n";

$message_header = file_get_contents("../mail/email_header.php");
$message_content = file_get_contents("../mail/new_purchase_order.php");
$message_content = str_replace('%supplier_name%', $supplier, $message_content); 
$message_content = str_replace('%file_name%', $file_name, $message_content); 
$message_footer = file_get_contents("../mail/email_footer.php");

$message .= $message_header.$message_content.$message_footer . "\r\n\r\n";
$message .= "--".$uid."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"".$file_name.".pdf\"\r\n";
$message .= "Content-Transfer-Encoding: base64" . "\r\n";
$message .= "Content-Disposition: attachment; filename=\"".$file_name.".pdf\"\r\n";
$message .= $content."\r\n";
$message .= "--".$uid."--";

mail($to, $subject, $message, $headers);

这似乎很好用,因为在我们的销售电子邮件中,密件抄送会收到eamil,并且可以毫无问题地打开附件。

但是,直接接收电子邮件的外部用户(如$to)有时在打开文件时遇到问题。

请参阅所附屏幕截图:

enter image description here

我知道有一些外部库可以使发送电子邮件和附件变得更加容易(例如phpmailer),但是我想知道为什么会这样或对此我该怎么办。

任何帮助或建议,将不胜感激。

0 个答案:

没有答案