【问题标题】:PHPMailer using AddStringAttachment with AddAttachmentPHPMailer 使用 AddStringAttachment 和 AddAttachment
【发布时间】:2021-11-29 22:43:16
【问题描述】:

我有一个从用户输入生成 PDF 的表单,以及一个文件上传选项。

所以用户可以上传或不上传文件,我使用AddAttachment作为这部分,AddStringAttachment作为生成的PDF。

这是我的代码:

//PDF
$pdf = $dompdf->output();    

//Configuration
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->CharSet  = 'UTF-8';
$mail->Host     = 'localhost';
$mail->SMTPAuth = false;

//Email details
$mail->From     = 'email@email.com'; 
$mail->FromName = 'My email'; 
$mail->Subject  = 'You have a new email';
$mail->Body     = 'Hi there, You have a new message.<br>';
$mail->AltBody  = 'Hi there, You have a new message.';

//Loop through recipients
$recipients = ['email1@email.com', 'email2@email.com'];

foreach ($recipients as $recipient) {
    $mail->addAddress($recipient);
    $mail->AddStringAttachment($pdf, 'name.pdf'); 
    if (!empty($_FILES['upload']['tmp_path'])) {
        $mail->AddAttachment($_FILES['upload']['tmp_path'], 'upload.pdf');    
    }

    if ( !$mail->Send() ) {
        echo 'Not sent';     
    } else {
        echo 'Sent';
    }

    // Clear all addresses and attachments for next loop
    $mail->clearAddresses();
    $mail->clearAttachments();
}

问题是,当我上传文件时,我收到 2 封电子邮件: 1-带有附件和生成的PDF(这是预期的) 2-仅使用生成的pdf。但是内容乱七八糟,看起来不对。

当我不上传文件时,我只会收到一封电子邮件。

我尝试只发送到 1 封电子邮件,但结果相同。

【问题讨论】:

  • 你为什么要在循环中添加附件?
  • @miken32 因为附件会在每个循环结束时被清除。我不确定这部分是如何工作的$mail-&gt;clearAddresses(); $mail-&gt;clearAttachments();
  • 但您只是一遍又一遍地添加相同的附件。
  • 我不知道。谢谢
  • 您的问题缺少太多信息,无法确定,但您似乎有多个问题。我不知道我之前是否错过了这个或者您更新了问题,但是为什么您有 addStringAttachment 和 addAttachment。您还有至少 6 个我们不知道的变量,它们可能包含或不包含什么($recipients、$pdf、$filename、$path、$report、$attach)

标签: php phpmailer


【解决方案1】:

如果可行,请不要将其标记为 Gview 的答案,我只是展示了代码并修复了资源问题。

虽然 Gview 的答案应该可以解决问题,但您确实不应该在循环内添加和删除附件

这是使用 gview 发布的修复程序的完整代码版本,不会浪费 CPU 资源不必要地附加和删除您的 PDF 附件。正如 miken32 在 cmets 中所说的那样。

//Configuration
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->CharSet  = 'UTF-8';
$mail->Host     = 'localhost';
$mail->SMTPAuth = false;

//Email details
$mail->From     = 'email@email.com'; 
$mail->FromName = 'My email'; 
$mail->Subject  = 'You have a new email';
$mail->Body     = 'Hi there, You have a new message.<br>';
$mail->AltBody  = 'Hi there, You have a new message.';

//Loop through recipients
$mail->AddStringAttachment($pdf, $filename); 
if (!empty($attach)) {
    $mail->AddAttachment($path, $report);    
}

foreach ($recipients as $recipient) {
    $mail->addAddress($recipient);

    if ( !$mail->Send() ) {
        echo 'Not sent';     
    } else {
        echo 'Sent';
    }

    // Clear all addresses and attachments for next loop
    $mail->clearAllRecipients();
}

$mail->clearAttachments();

【讨论】:

  • 没有解决问题。我仍然收到 2 封电子邮件
  • 我认为这可能是因为您使用 StringAttachment,您的 $pdf 是二进制格式还是 base64 格式?注意AddStringAttachment 只支持二进制格式,如果你的$pdf 是base64 编码,你需要先base64_decode()
  • $pdf 是从 DomPdf $pdf = $dompdf-&gt;output(); 生成的,它只是一个字符串
  • 没有字符串这样的东西,字符串只是 char 的托管数组,而 char 只是 int8,所以任何二进制数据都可以显示为字符串,但是你使用的是 Dompdf-> output() 那么是的,输出是二进制的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 2015-06-20
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
相关资源
最近更新 更多