php – PHPMailer w/SMTP to MailGun after a few hundred emails, get error: 451 Failed to parse header: malformed data; unexpected EOF

Using PHPMailer v 6.6.0 with SMTP to MailGun running PHP 7.4.28 & MySQL. The below code is in a foreach loop generating over 1,000 emails. Can send emails locally (using PHPMailer) without issue, but when sending externally via SMTP to MailGun, after a hundred or more emails have been sent, will have an SMTP communication error. Last two entries of PHPMailer Debugging report, #145 was sent w/out issue, #146 had a communication error. MailGun is saying the error is a parsing one where the MIME header data is malformed. So I set charset to utf-8 for all header items and declared that in PHPMailer, but something is still not right.

Here is MailGuns official reply: “With the ‘451 Failed to parse header: malformed data; unexpected EOF’, it doesn’t give specifics as to what exactly caused that error. However, it means that the message MIME is coming in malformed. This can be because of special characters or invalid formatting, but again we don’t know the specifics.”

Below is the last two entries of the PHPMailer Debugging Report. Cut off a few dozen lines at beginning of #145 and the end of #146 where error occurred so not to be flagged as spam.

145
...removed above lines so as not to be flagged as spam...
2022-05-27 22:40:33 CLIENT -> SERVER: This is a multi-part message in MIME format.
2022-05-27 22:40:33 CLIENT -> SERVER: 
2022-05-27 22:40:33 CLIENT -> SERVER: --b1_qTte9OeCLb4bnOH0adZvABlTFkh21zO3VRKOPoXHBs
2022-05-27 22:40:33 CLIENT -> SERVER: Content-Type: text/plain; charset=utf-8
2022-05-27 22:40:33 CLIENT -> SERVER: Content-Transfer-Encoding: base64
2022-05-27 22:40:33 CLIENT -> SERVER: 
2022-05-27 22:40:33 CLIENT -> SERVER: NS0yNy0yMjxicj5BbHRCb2R5OiBUaGlzIGlzIGEgQnVsayBSZWxheSBFbWFpbCAoU01UUCkgVGVz
2022-05-27 22:40:33 CLIENT -> SERVER: dCAvci9uIFZpYSBQSFAgNy40LjI4LCBQSFBNYWlsZXIgNi42LjAgYW5kIE1haWxndW4gd2l0aCBI
2022-05-27 22:40:33 CLIENT -> SERVER: VE1MICYgUEhQTWFpbGVyIENoYXJTZXQgPSAndXRmLTgnICwgTXlTUUwgdXRmOG1iNCA9ICd1dGY4
2022-05-27 22:40:33 CLIENT -> SERVER: bWI0JywgYW5kIFBIUE1haWxlciBFbmNvZGluZyA9ICdxdW90ZWQtcHJpbnRhYmxlJyBhbmQgdXRm
2022-05-27 22:40:33 CLIENT -> SERVER: OF9lbmNvZGUgYmVmb3JlIGVhY2ggaGVhZGVyIGVsZW1lbnQ=
2022-05-27 22:40:33 CLIENT -> SERVER: 
2022-05-27 22:40:33 CLIENT -> SERVER: --b1_qTte9OeCLb4bnOH0adZvABlTFkh21zO3VRKOPoXHBs
2022-05-27 22:40:33 CLIENT -> SERVER: Content-Type: text/html; charset=utf-8
2022-05-27 22:40:33 CLIENT -> SERVER: Content-Transfer-Encoding: base64
2022-05-27 22:40:33 CLIENT -> SERVER: 
2022-05-27 22:40:33 CLIENT -> SERVER: PHA+NS0yNy0yMjxicj5Cb2R5OiBUaGlzIGlzIGEgQnVsayBSZWxheSBFbWFpbCAoU01UUCkgVGVz
2022-05-27 22:40:33 CLIENT -> SERVER: dDxicj5WaWEgUEhQIDcuNC4yOCwgUEhQTWFpbGVyIDYuNi4wIGFuZCBNYWlsZ3VuIHdpdGggSFRN
2022-05-27 22:40:33 CLIENT -> SERVER: TCAmIFBIUE1haWxlciBDaGFyU2V0ID0gIlVURi04IiAsIE15U1FMICRjaGFyc2V0ID0gInV0Zjht
2022-05-27 22:40:33 CLIENT -> SERVER: YjQiLCBhbmQgUEhQTWFpbGVyIEVuY29kaW5nID0gInF1b3RlZC1wcmludGFibGUiIGFuZCB1dGY4
2022-05-27 22:40:33 CLIENT -> SERVER: X2VuY29kZSBiZWZvcmUgZWFjaCBoZWFkZXIgZWxlbWVudDwvcD4=
2022-05-27 22:40:33 CLIENT -> SERVER: 
2022-05-27 22:40:33 CLIENT -> SERVER: 
2022-05-27 22:40:33 CLIENT -> SERVER: --b1_qTte9OeCLb4bnOH0adZvABlTFkh21zO3VRKOPoXHBs--
2022-05-27 22:40:33 CLIENT -> SERVER: 
2022-05-27 22:40:33 CLIENT -> SERVER: .
2022-05-27 22:40:34 SERVER -> CLIENT: 250 Great success
2022-05-27 22:40:34 CLIENT -> SERVER: RSET
2022-05-27 22:40:34 SERVER -> CLIENT: 250 OK


Message (customer@gmail.com) has been sent successfully

146
2022-05-27 22:40:35 CLIENT -> SERVER: QUIT
2022-05-27 22:40:35 SERVER -> CLIENT: 221 See you later. Yours truly, Mailgun
2022-05-27 22:40:35 Connection: closed
2022-05-27 22:40:35 Connection: opening to smtp.mailgun.org:587, timeout=300, options=array()
2022-05-27 22:40:35 Connection: opened
2022-05-27 22:40:35 SERVER -> CLIENT: 220 Mailgun Influx ready
2022-05-27 22:40:35 CLIENT -> SERVER: EHLO www.company.com
2022-05-27 22:40:35 SERVER -> CLIENT: 250-smtp-out-n03.prod.us-west-2.postgun.com250-AUTH PLAIN LOGIN250-SIZE 52428800250-8BITMIME250-SMTPUTF8250-PIPELINING250 STARTTLS
2022-05-27 22:40:35 CLIENT -> SERVER: STARTTLS
2022-05-27 22:40:35 SERVER -> CLIENT: 220 Go ahead
2022-05-27 22:40:35 CLIENT -> SERVER: EHLO www.company.com
2022-05-27 22:40:35 SERVER -> CLIENT: 250-smtp-out-n03.prod.us-west-2.postgun.com250-AUTH PLAIN LOGIN250-SIZE 52428800250-8BITMIME250-SMTPUTF8250 PIPELINING
2022-05-27 22:40:35 CLIENT -> SERVER: AUTH LOGIN
2022-05-27 22:40:35 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2022-05-27 22:40:35 CLIENT -> SERVER: [credentials hidden]
2022-05-27 22:40:35 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2022-05-27 22:40:35 CLIENT -> SERVER: [credentials hidden]
2022-05-27 22:40:36 SERVER -> CLIENT: 235 Authentication successful
2022-05-27 22:40:36 CLIENT -> SERVER: MAIL FROM:<quote@rfq.company.com>
2022-05-27 22:40:36 SERVER -> CLIENT: 250 Sender address accepted
2022-05-27 22:40:36 CLIENT -> SERVER: RCPT TO:<me@gmail.com>
2022-05-27 22:40:36 SERVER -> CLIENT: 250 Recipient address accepted
2022-05-27 22:40:36 CLIENT -> SERVER: DATA
2022-05-27 22:40:36 SERVER -> CLIENT: 354 Continue
2022-05-27 22:40:36 CLIENT -> SERVER: Date: Fri, 27 May 2022 17:40:35 -0500
2022-05-27 22:40:36 CLIENT -> SERVER: To: Ryne Allen <me@gmail.com>
2022-05-27 22:40:36 CLIENT -> SERVER: From: My Company <quote@rfq.company.com>
2022-05-27 22:40:36 CLIENT -> SERVER: Reply-To: My Company <quote@rfq.company.com>
2022-05-27 22:40:36 CLIENT -> SERVER: Subject: #146g-SMTP-MG-Test -> To: 10591 of customer@yyy.com
2022-05-27 22:40:36 CLIENT -> SERVER: Message-ID: <uBrObwrxzuPlfV2uIa8T2UUNuG4VAPAYqyNHqCgy24@www.company.com>
2022-05-27 22:40:36 CLIENT -> SERVER: X-Mailer: PHPMailer 6.6.0 (https://github.com/PHPMailer/PHPMailer)
2022-05-27 22:40:36 CLIENT -> SERVER: MIME-Version: 1.0
2022-05-27 22:40:36 CLIENT -> SERVER: Content-Type: multipart/alternative;
2022-05-27 22:40:36 CLIENT -> SERVER: boundary="b1_uBrObwrxzuPlfV2uIa8T2UUNuG4VAPAYqyNHqCgy24"
2022-05-27 22:40:36 CLIENT -> SERVER: 
2022-05-27 22:40:36 CLIENT -> SERVER: This is a multi-part message in MIME format.
2022-05-27 22:40:36 CLIENT -> SERVER: 
2022-05-27 22:40:36 CLIENT -> SERVER: 0krd GaOmW,:36u0ZoeKeWKeWKX t.j5yIZ hctyllnNAnaRn l t.ZlUB1ATCuSAu1UhZNpZu1UhZNpZBR4pf-Rn l t.ZlUB1ATCuSAu1UhZNpZu1UhZNpZBR4pf-Rn l undara0iipanzWT:l_NT -> SERVER: 
2022-05-27 22:40:36 CLIENT -> SERVER: 0krd GaOmW,:36u0ZoeKeWKeWp2.
2022-05-27 22:40:36 CLIENT --1e: mIR7VER: This is a multi-part SERVER: 0krd GaOmW,:36u0ZoeKeWKeWp2.
2022-05-27 22:40:36 CLIENT -> SERVER: 
2022-.W,:36u0ZoeKeWKeWKX t.j5yIZ hctyllnIJ9IWeKeWeoeKeWKeWKX t.j5yIZ hctyllnNAnaRn l t.ZlUB1ATCuSAua40:3VF4 I mUmUmUmUala::: Ga l t.ZlUNidJ@dDWllDWllDSDeITCEFIpe-GGa l t.ZlUNidJ@dDWllDWllDSDeITCEFIpe-GGa l t.ZlUNidJ@dDWllDWllDSDeITCEFIpe-GGa l t.ZlUNidJ@dDWllDWu5Do1slUNidJ@dDWllDWu5Do1slUNidJ@dDWllDWu5Do1slUNidJ@ 05-27 22:40:36 CLIENT --1e: mIR7VER: This is a muXSllDWIvE hdsttbtC IT5kbxe pGJ@dDip 
...removed last few dozen lines so not to be flagged as spam...

This is the code I used to send bulk email using PHPMailer to MailGun:

        use PHPMailerPHPMailerPHPMailer;
        use PHPMailerPHPMailerSMTP;
        use PHPMailerPHPMailerException;
        //PHPMailer Ver 6.6.0
        require '../../PHPMailer/src/PHPMailer.php';
        require '../../PHPMailer/src/SMTP.php';     
        require '../../PHPMailer/src/Exception.php';

        $setFrom = utf8_encode('quote@rfq.company.com');
        $FromNam = utf8_encode('My Company');
        $ReplyTo = utf8_encode('quote@rfq.company.com');
        $ReplNam = utf8_encode('My Company');
        $Address = utf8_encode('me@gmail.com');
        $AddrNam = utf8_encode('Ryne Allen');
        $Subject = utf8_encode('#'.$count.'h-SMTP-MG-Test -> To: '.$row["mnrID"].' of '.$row["cont_email"]);

        $mail = new PHPMailer(true); 
        $mail->CharSet="utf-8";   
        $mail->Encoding = 'base64';

        $mail->isSMTP();
        $mail->SMTPKeepAlive = true; 
        $mail->Host="smtp.mailgun.org";
        $mail->Port="587";
        $mail->SMTPAuth = true; 
        $mail->Username="postmaster@company.com";  
        $mail->Password = 'xxxxxx';
        $mail->SMTPSecure="tls";
        $mail->SMTPDebug = 3;
        
        $mail->setFrom($setFrom, $FromNam);
        $mail->addReplyTo($ReplyTo, $ReplNam);
        $mail->addAddress($Address, $AddrNam);
        $mail->isHTML(true);
        $mail->Subject = $Subject; 
        
        //An HTML message body
        $mail->Body = utf8_encode('<p>5-27-22<br>Body: This is a Bulk Relay Email (SMTP) Test<br>Via PHP 7.4.28, PHPMailer 6.6.0 and Mailgun with HTML & PHPMailer CharSet = "utf-8" , MySQL $charset = "utf8mb4", and PHPMailer Encoding = "base64" and utf8_encode before each header element</p>');
        $mail->AltBody = utf8_encode("5-27-22<br>AltBody: This is a Bulk Relay Email (SMTP) Test /r/n Via PHP 7.4.28, PHPMailer 6.6.0 and Mailgun with HTML & PHPMailer CharSet="utf-8" , MySQL $charset="utf8mb4", and PHPMailer Encoding = 'base64' and utf8_encode before each header element");


       try {
           $mail->send();
           echo "<br>Message (".$row["cont_email"].") has been sent successfully<br>";
       } catch (Exception $e) {
           echo 'Message could not be sent.';
           echo "Mailer Error: " . $mail->ErrorInfo;
       }

Any help would be appreciated. I tried setting $mail->Encoding = ‘quoted-printable’ instead of ‘base64’ and the $mail->CharSet=”UTF-8″ instead of the lower case ‘utf-8’ which I read may exclude more problems.

Leave a Comment