为了账号安全,请及时绑定邮箱和手机立即绑定

三次方码:邮件附件为空

三次方码:邮件附件为空

PHP
皈依舞 2022-09-25 20:57:36
我正在使用 TCPDF 创建配置文件并将配置文件作为附件发送。我有许多客户使用该软件,并且需要批量电子邮件服务。我一直在从本地服务器电子邮件发送配置文件,它工作得很好。这是以前的代码(只是电子邮件部分):$pdf->writeHTML($tbl, true, false, false, false, '');$js .= 'print(true);';// set javascript$pdf->IncludeJS($js); //Close and output PDF document//Close and output PDF document//define the receiver of the email$to = $Email;$subject = "Profile";$repEmail = 'me@mydomain.com';$fileName = 'Profile.pdf';$fileatt = $pdf->Output($fileName, 'S');$attachment = chunk_split(base64_encode($fileatt));$eol = PHP_EOL;$separator = md5(time());$headers = 'From: Identykidz <'.$repEmail.'>'.$eol;$headers .= 'MIME-Version: 1.0' .$eol;$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";$message = "--".$separator.$eol;$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;$message .= "Dear parent/guardian  \r\n\r\nPlease find attached the Profile attached.Kind regards\r\nIdentyKidz Information Centre".$eol;$message .= "--".$separator.$eol;$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;$message .= "--".$separator.$eol;$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; $message .= "Content-Transfer-Encoding: base64".$eol;$message .= "Content-Disposition: attachment".$eol.$eol;$message .= $attachment.$eol;$message .= "--".$separator."--";// Send the emailif(mail($to, $subject, $message, $headers)) {echo '<span style="color:#1e73be; text-align: center; font-family: Verdana">The profile has been sent successfully to the email address entered.</span>';}else {echo "There was an error sending the mail.";}}//catch exceptioncatch(Exception $e) {header("Location: something_went_wrong.php");}//============================================================+// END OF FILE//============================================================+因为我需要使用批量电子邮件服务,所以我必须包括批量电子邮件服务提供商的SMTP详细信息,所以我将TCPDF的电子邮件部分从mail()更改为PHPMailer。
查看完整描述

4 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

你好查尔斯瓦辛顿,我已经检查了你的代码示例:


根据您的代码,我看到您正在尝试从字符串加载文件。


这让我有点困惑,因为我立即收到一个错误。您是否已启用并检查了日志?无论如何,你只需要将字符串放入正确的变量中,其他一切都可以正常工作。您可以在下面找到我对您的代码所做的更改。error_reportingerror_log


以前:


$fileatt = $pdf->Output('Profile.pdf', 'S'); // S = Means return the PDF as string

// ... code ... 

$pdf_content = file_get_contents($fileatt);

// .. code

$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");

后:


$pdf_content = $pdf->Output('Profile.pdf', 'S'); 

// ... code ... 

$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");

我刚刚使用 SES 凭据和自定义 PDF 以及以前生成的 PDF 测试了您的脚本。


如果这有帮助,请告诉我。


查看完整回答
反对 回复 2022-09-25
?
紫衣仙女

TA贡献1839条经验 获得超15个赞

通过使用


$fileatt = $pdf->Output('Profile.pdf', 'S');

//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');


require ('PHPMailer/PHPMailerAutoload.php');


$pdf_content = file_get_contents($fileatt);

您正在尝试从字符串中读取file_content。


您只需通过以下方式发送附件


 $mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");  


查看完整回答
反对 回复 2022-09-25
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

我想也许你必须依恋。请参阅此处,第一个答案:base64_decode

$mail->AddStringAttachment(base64_decode($pdf_content), "Prodile.pdf", "base64", "application/pdf");

的第三个参数仅告诉编码,但显然不会为您执行任何编码AddStringAttachment


查看完整回答
反对 回复 2022-09-25
?
忽然笑

TA贡献1806条经验 获得超5个赞

我能够根据ivion的评论对问题进行排序。

$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");


查看完整回答
反对 回复 2022-09-25
  • 4 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号