Search code examples
phpmaileremail-attachments

AddStringAttachment giving unusual results


I am sending attachments (CSV) which I have been sending for years using mail() but decided to migrate to SMTP for better reliability.

Code 1 (CSV attachment)

$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; // [email protected]
$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;
}

The email gets delivered to me BUT ... Problems:

  1. When viewing in Gmail browser - I get message: " Gmail virus scanners are temporarily unavailable – The attached files haven't been scanned for viruses. Download these files at your own risk."
  2. When viewing in Gmail browser - I cant save/download the file? (clicking the download button does nothing)
  3. When clicking attachment to view in browser, I now get error: "Whoops. There was a problem while previewing this document"
  4. I try "Show Original" and it takes 30+ seconds for the email to load which just shows the base64 encoded data
  5. I tried to open in inbox in Outlook and after 5 minutes of the emails not loading I gave up (thinking the emails are not encoded properly or something causing outlook to get stuck)

It looks like it is working (i.e. the file looks legit based on the gmail icon preview) but I cant do anything else with it and I don't know if it is a Gmail issue or File Issue.

Any advice?


Solution

  • Turns out that Gmail was having issues all yesterday afternoon with attachments. It was a Gmail issue - The timing is unbelievable

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