【问题标题】:PHPMailer $mail->addAttachment attatchment missing sometimePHPMailer $mail->addAttachment 附件有时丢失
【发布时间】:2017-11-30 16:50:48
【问题描述】:

我正在使用 PHPMailer 发送带有附件的电子邮件。通常文件是 jpg png jpeg 格式的图像。有时图像会附加到电子邮件中,但有时它会丢失。
我正在使用如下代码。

for($i=0; $i < $count_rows; $i++){     
    $root_dr = $_SERVER['DOCUMENT_ROOT'];
    $img_url_dr = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
    $passport_attachment = str_replace($img_url_dr, $root_dr, $all_images[$i]); 
    $mail->addAttachment($passport_attachment, $all_data['firstname'][$i]); 
} 

它可以完美地处理附件,但有时附件会失败我很困惑问题出在哪里。
循环中的图像可以是循环中的 1 到 5 个图像。
请帮助我在哪里做错了,为什么有时附件会丢失?

【问题讨论】:

    标签: php email phpmailer


    【解决方案1】:

    您没有检查来自addAttachment 的返回值,并且您的循环效率有点低。这样做:

    $root_dr = $_SERVER['DOCUMENT_ROOT'];
    $img_url_dr = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
    for($i=0; $i < $count_rows; $i++){     
        $passport_attachment = str_replace($img_url_dr, $root_dr, $all_images[$i]); 
        if !($mail->addAttachment($passport_attachment, $all_data['firstname'][$i])) {
            echo "File $passport_attachment failed to attach\n";
        }
    }
    

    很难从您的代码中判断您是否正在执行此操作(我现在不知道 $all_images 中的内容),但 addAttachment 确实需要一个本地路径,而不是您要附加的文件的 URL。

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 2015-06-20
      • 1970-01-01
      • 2018-07-22
      • 2020-05-20
      • 2022-06-10
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多