AddStringAttachment 给出异常结果

AddStringAttachment giving unusual results

我正在发送附件 (CSV),多年来我一直使用 mail() 发送这些附件,但我决定迁移到 SMTP 以获得更好的可靠性。

代码 1(CSV 附件)

$attachment = $this->CSVData; // "Date","Name","Dept" ... \n"2019-03-13","Dave" ...
$encoding = 'base64';
$contentType = 'text/csv';
$filename = $this->createFileName(); //Get FileDate and Name

$recipient = $delivery_email; // xxxxxxx@gmail.com
$subject = $this->emailHeader['subject'] . " CSV Data";
$message =  'Daily Data File';

$mail = new PHPMailer\PHPMailer\PHPMailer(true);          // Passing `true` enables exceptions                         
try {
    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = SMTP_HOST;                              // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = SMTP_USER;                          // SMTP username
    $mail->Password = SMTP_PASS;                          // SMTP password
    $mail->SMTPSecure = SMTP_AUTH;                        // Enable TLS encryption, `ssl` also accepted
    $mail->Port = SMTP_PORT;                              // TCP port to connect to

    //Recipients
    $mail->setFrom($this->fromEmail, $this->fromEmailName);      // Add a FROM
    $addresses = explode(',', $recipient);
    foreach ($addresses as $address) {
        $mail->AddAddress(trim($address));                       // Add a recipient(s)
    }           
    if ( !empty($this->emailCC) ) $mail->addCC($this->emailCC);  // Add a CC

    //13-03-2019: Add the attachment to the email
    $mail->AddStringAttachment($attachment, $filename, $encoding, $contentType);

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;
    $mail->AltBody = 'This email is formatted in HTML';
    $mail->send();
    $this->fo->printStatus('Email successfully sent to: '. $recipient );

    return true;
} catch (Exception $e) {
    $this->fo->printStatus( basename(__FILE__) .' '. __LINE__ . ': Message could not be sent. Mailer Error: '. $mail->ErrorInfo );
    return false;
}

电子邮件已发送给我,但是... 问题:

  1. 在 Gmail 浏览器中查看时 - 我收到消息:“ Gmail 病毒扫描程序暂时不可用 – 附件未进行病毒扫描。下载这些文件需要您自担风险。"
  2. 在 Gmail 浏览器中查看时 - 我无法 save/download 文件? (点击下载按钮没有任何作用)
  3. 点击附件在浏览器中查看时,出现错误:"Whoops. There was a problem while previewing this document"
  4. 我尝试 "Show Original" 并且需要 30 秒以上的时间来加载仅显示 base64 编码数据的电子邮件
  5. 我尝试在 Outlook 的收件箱中打开,但在 5 分钟的电子邮件未加载后我放弃了(认为电子邮件编码不正确或导致 Outlook 卡住的原因)

看起来它正在工作(即根据 Gmail 图标预览,该文件看起来合法)但我不能用它做任何其他事情,我不知道这是 Gmail 问题还是文件问题。

有什么建议吗?

原来 Gmail 昨天下午都出现了附件问题。这是一个 Gmail 问题 - 时机令人难以置信

https://www.theguardian.com/technology/2019/mar/13/googles-gmail-and-drive-suffer-global-outages