Reputation: 1
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 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 ([email protected]) 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:<[email protected]>
2022-05-27 22:40:36 SERVER -> CLIENT: 250 Sender address accepted
2022-05-27 22:40:36 CLIENT -> SERVER: RCPT TO:<[email protected]>
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 <[email protected]>
2022-05-27 22:40:36 CLIENT -> SERVER: From: My Company <[email protected]>
2022-05-27 22:40:36 CLIENT -> SERVER: Reply-To: My Company <[email protected]>
2022-05-27 22:40:36 CLIENT -> SERVER: Subject: #146g-SMTP-MG-Test -> To: 10591 of [email protected]
2022-05-27 22:40:36 CLIENT -> SERVER: Message-ID: <[email protected]>
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 PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//PHPMailer Ver 6.6.0
require '../../PHPMailer/src/PHPMailer.php';
require '../../PHPMailer/src/SMTP.php';
require '../../PHPMailer/src/Exception.php';
$setFrom = utf8_encode('[email protected]');
$FromNam = utf8_encode('My Company');
$ReplyTo = utf8_encode('[email protected]');
$ReplNam = utf8_encode('My Company');
$Address = utf8_encode('[email protected]');
$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 = '[email protected]';
$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.
Upvotes: 0
Views: 1097
Reputation: 1
I tried multiple email relay services and had similar results with MailJet, SendGrid, and SendinBlue.
Synchro's response included, "You're setting SMTPKeepAlive, but you have no sending loop, so it will not achieve anything." This made me question the logic of the operation and instead of creating the loop around the PHPMailer code, I created it within the PHPMailer code, keeping the SMTP session alive until all [bulk] emails were sent out.
I was able to fix this issue by changing the PHPMailer logic when sending out multiple (hundreds) of emails at once.
The new code looks like this:
$cxn=mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM Recipients_tbl";
$result = mysqli_query($cxn, $query);
$count=0;
//Include Required PHPMailer files:
require_once '../../PHPMailer/src/PHPMailer.php';
require_once '../../PHPMailer/src/SMTP.php';
require_once '../../PHPMailer/src/Exception.php';
//Define Namespaces.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPKeepAlive = true;
$mail->SMTPDebug = false;
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
//SMTP via MailGun
$mail->Host = 'smtp.mailgun.org';
$mail->Username = '[email protected]';
$mail->Password = 'mypass';
$mail->setFrom('[email protected]', 'My Company');
$mail->addReplyTo('[email protected]', 'My Company');
$mail->isHTML(true);
foreach($result as $row)
{
$count++;
echo "<br>".$count."<br>";
$Subject = '#'.$count.'MailGun-SMTP-Loop-Test -> To: '.$row["mnrID"];
$HTMBody = '<p>5-31-22<br>Body: This is a Bulk (loop) Test<br>Via MailGun</p>';
$AltBody = "5-31-22<br>AltBody: This is a Bulk (loop) Test /r/n Via MailGun";
$mail->addAddress('[email protected]', 'Me'); //sent to me for test
$mail->Subject = $Subject;
$mail->Body = $HTMBody;
$mail->AltBody = $AltBody;
try {
$mail->send();
$mail->ClearAllRecipients();
} catch (Exception $e) {
echo 'Mailer Error (' . htmlspecialchars($row["cont_email"]) . ') ' . $mail->ErrorInfo . '<br>';
//Reset the connection to abort sending this message
//The loop will continue trying to send to the rest of the list
$mail->getSMTPInstance()->reset();
}
sleep(1);
}
$mail->SmtpClose();
Upvotes: 0
Reputation: 37700
Nothing obvious, but you're doing several things not quite right.
You shouldn't be doing any new development on PHP 7.4. It's out of active support, and will stop getting security patches soon.
Avoid the utf8_encode
function; your code and data should be in UTF-8 anyway, and if it is, that function will corrupt your text. It's sufficiently dangerous that it's been deprecated for PHP 8.2 and will be removed in PHP 9.
Port
expects an integer, not a string; use $mail->Port = 587
.
Don't use base64 encoding for HTML parts; it makes you look like a spammer, just let PHPMailer use its default encoding (8-bit, which MailGun supports).
You're setting SMTPKeepAlive
, but you have no sending loop, so it will not achieve anything. See the PHPMailer mailing list example for how to use that correctly. This should make your send go quite a bit faster.
Now onto your data:
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.
There is something very wrong here. It looks like memory corruption or something (not network, as it's local at that point). The CLIENT -> SERVER
string is simple static text in PHPMailer, and there is no opportunity for it to contain anything else. I'd suggest applying the other changes above, and see how you go.
Upvotes: 1