php - TCPDF:PHPMailer 附件为空

标签 php phpmailer tcpdf

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

这是 TCPDF 代码的新电子邮件部分:

$pdf->writeHTML($tbl, true, false, false, false, '');

$js .= 'print(true);';

// set javascript
$pdf->IncludeJS($js); 

$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);

$sender = 'Me@mydomain.com';
$senderName = 'Information Centre';

// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
$recipient = $Email;

// Replace smtp_username with your Amazon SES SMTP user name.
$usernameSmtp = 'Mycredentials';

// Replace smtp_password with your Amazon SES SMTP password.
$passwordSmtp = 'Mycredentials';

 // Specify a configuration set. If you do not want to use a configuration
// set, comment or remove the next line.
//$configurationSet = 'ConfigSet';

// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
$host = 'email-smtp.eu-central-1.com';
$port = 587;

// The subject line of the email
$subject = "IdentyKidz Profile of $name";

// The plain-text body of the email
$bodyText =  "";

// The HTML-formatted body of the email
$bodyHtml = "Dear parent/guardian <br><br>
Kind regards<br>IdentyKidz Information Centre";

$mail = new PHPMailer(true);

try {
    // Specify the SMTP settings.
$mail->isSMTP();
$mail->setFrom($sender, $senderName);
$mail->Username   = $usernameSmtp;
$mail->Password   = $passwordSmtp;
$mail->Host       = $host;
$mail->Port       = $port;
$mail->SMTPAuth   = true;
$mail->SMTPSecure = 'tls';
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configurationSet);

// Specify the message recipients.
$mail->addAddress($recipient);
// You can also add CC, BCC, and additional To recipients here.

// Specify the content of the message.
$mail->isHTML(true);
$mail->Subject    = $subject;
$mail->Body       = $bodyHtml;
$mail->AltBody    = $bodyText;
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");

// Send the email
if($mail->Send()) {
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 (phpmailerException $e) {
echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
} catch (Exception $e) {
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}

}
//catch exception
catch(Exception $e) {
header("Location: something_went_wrong.php");
}

//============================================================+
// END OF FILE
//============================================================+

第二个代码(PHP Mailer)发送电子邮件,但表示已附加 PDF,但附件为空。

我错过了什么?

最佳答案

您好 CharlesWashington,我已经检查了您的代码示例:

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

这让我有点困惑,因为我立即收到了一个错误。您是否启用了 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 测试了您的脚本。

请告诉我这是否有帮助。

关于php - TCPDF:PHPMailer 附件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60280996/

相关文章:

php - 为什么我应该使用 MailChimp 或类似的而不是定制的脚本?

pdf - prestashop pdf发票中的订单号有误

PHP Exec在linux中同时执行两条命令

php - 如何在构造函数中调用实体管理器?

PHPMailer - 无法验证

php - 为什么PHP将一些尖括号转换为PHPMailer中的html实体

php - 来自 php 的内容长度 header 被覆盖!

php - 如何在 PHP 的下拉菜单中访问 MYSQL 数据库中的值

php - TCPDF:PNG 图像渲染不正确

html - 如何为我的表格获取圆形单元格