三次方码:邮件附件为空

我正在使用 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 email

if(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 exception

catch(Exception $e) {

header("Location: something_went_wrong.php");

}


//============================================================+

// END OF FILE

//============================================================+

因为我需要使用批量电子邮件服务,所以我必须包括批量电子邮件服务提供商的SMTP详细信息,所以我将TCPDF的电子邮件部分从mail()更改为PHPMailer。


皈依舞
浏览 133回答 4
4回答

潇潇雨雨

你好查尔斯瓦辛顿,我已经检查了你的代码示例:根据您的代码,我看到您正在尝试从字符串加载文件。这让我有点困惑,因为我立即收到一个错误。您是否已启用并检查了日志?无论如何,你只需要将字符串放入正确的变量中,其他一切都可以正常工作。您可以在下面找到我对您的代码所做的更改。error_reportingerror_log以前:$fileatt = $pdf->Output('Profile.pdf', 'S'); // S = Means return the PDF as string// ... code ...&nbsp;$pdf_content = file_get_contents($fileatt);// .. code$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");后:$pdf_content = $pdf->Output('Profile.pdf', 'S');&nbsp;// ... code ...&nbsp;$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");我刚刚使用 SES 凭据和自定义 PDF 以及以前生成的 PDF 测试了您的脚本。如果这有帮助,请告诉我。

紫衣仙女

通过使用$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。您只需通过以下方式发送附件&nbsp;$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");&nbsp;&nbsp;

元芳怎么了

我想也许你必须依恋。请参阅此处,第一个答案:base64_decode$mail->AddStringAttachment(base64_decode($pdf_content),&nbsp;"Prodile.pdf",&nbsp;"base64",&nbsp;"application/pdf");的第三个参数仅告诉编码,但显然不会为您执行任何编码。AddStringAttachment

忽然笑

我能够根据ivion的评论对问题进行排序。$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");
打开App,查看更多内容
随时随地看视频慕课网APP