I have used PHPMailer for several years now with no issues but I’m now seeing issues on a few emails going out completely blank.
My code looks like this:
<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'x';
$mail->Port =1;
$mail->SMTPAuth = true;
$mail->Username = 'y';
$mail->Password = 'x';
$mail->SMTPKeepAlive = true;
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
$mail->setFrom('[email protected]', 'Me Notifications');
$mail->addAddress('[email protected]', 'Me Notifications');
$addresses = explode(',', $customerlist);
foreach ($addresses as $address) {
$mail->addBCC($address);
}
$mail->Subject = "REMINDER: Test";
ob_start(); ?>
<html>
**Loads of HTML there with PHP Echo's**
</html>
<?php $body = ob_get_clean();
$mail->Body = $body;
$mail->IsHTML(true);
if (!$mail->send()) {
echo 'Mail did not send';
} else {
echo 'Mail Sent - Sending 2nd email internally';
$mail->ClearAllRecipients();
$mail->setFrom('[email protected]', 'Me Notifications');
$mail->addAddress('[email protected]', 'Me Notifications');
$mail->addBCC('[email protected]', 'Internal Comms');
$mail->send();
}
?>
Overall, the code works fine.
I have obviously hidden some creds, host info, emails, etc but no concerns there.
You can see that I send one email to everyone in an Addresses variable as a BCC.
I then follow it up and send a second email to just a single email address.
The issue is with the first email being blank, not the second one.
Whether it is the cause or not in a recent example, the number of email addresses in the BCC field was over 5000 which is recent for us so could be caused somehow?
But I can’t work out why the email would still send fine, no errors in the logs (I used SMTPDebug = 2) to show there is an issue, yet the content of the email, is blank.
This is some of the logs showing the content blank:
04-12-2024 13:16:50.665160 CLIENT -> SERVER: RCPT TO:<[email protected]>
04-12-2024 13:16:50.666264 SERVER -> CLIENT: 250 2.1.5 Ok
04-12-2024 13:16:50.666309 CLIENT -> SERVER: RCPT TO:<[email protected]>
04-12-2024 13:16:50.667385 SERVER -> CLIENT: 250 2.1.5 Ok
04-12-2024 13:16:50.667431 CLIENT -> SERVER: DATA
04-12-2024 13:16:50.668255 SERVER -> CLIENT: 354 End data with <CR><LF>.<CR><LF>
04-12-2024 13:16:50.668356 CLIENT -> SERVER: Date: Wed, 4 Dec 2024 13:16:44 +0000
04-12-2024 13:16:50.668529 CLIENT -> SERVER: To: Me Notifications <[email protected]>
04-12-2024 13:16:50.668581 CLIENT -> SERVER: From: Me Notifications <[email protected]>
04-12-2024 13:16:50.668625 CLIENT -> SERVER: Subject: REMINDER: Change Ref: 1 - Test Comms
04-12-2024 13:16:50.668668 CLIENT -> SERVER: Message-ID: <[email protected]>
04-12-2024 13:16:50.668710 CLIENT -> SERVER: X-Mailer: PHPMailer 6.2.0 (https://github.com/PHPMailer/PHPMailer)
04-12-2024 13:16:50.668752 CLIENT -> SERVER: MIME-Version: 1.0
04-12-2024 13:16:50.668795 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1
04-12-2024 13:16:50.668838 CLIENT -> SERVER: Content-Transfer-Encoding: quoted-printable
04-12-2024 13:16:50.668881 CLIENT -> SERVER:
04-12-2024 13:16:50.668923 CLIENT -> SERVER:
04-12-2024 13:16:50.668971 CLIENT -> SERVER: .
04-12-2024 13:16:50.717486 SERVER -> CLIENT: 250 2.0.0 Ok: queued as A4A8C83E7A
04-12-2024 13:16:50.717819 CLIENT -> SERVER: RSET
04-12-2024 13:16:50.718576 SERVER -> CLIENT: 250 2.0.0 Ok
Can see from 04-12-2024 13:16:50.668881 that it’s blank, this is where it should show the email content.
The only PHP error I get is:
PHP Warning: Undefined variable $descpull
I don’t believe this would be causing it though, although I can see why that error exists
Within the content:
$description_pull=($row['description']);
$htmlspecial = htmlspecialchars($descpull);
$description = nl2br($htmlspecial);
$description = nl2br($description_pull);
So don’t know why the middle 2 lines are there to be honest, I’ll delete
But I don’t think an undefined variable, would cause this behaviour would it?
Wherever the variable is used, it would just be blank
Can anyone see a reason why this would be happening at all?
Could there be some limit above 5000 that somehow still allows it to send but just wipes the content?
Any help would be appreciated
2
Answers
We looked into this further and confirmed two things. The PHP code was definitely passing on the email body content correctly to the PHPMailer elements.
The Mail Relay server was not receiving the content.
Few things spotted:
I have just re-sent the comms with:
This worked fine For anyone else's benefit, here is the piece of code used to split it out:
So haven't worked out why over 5000 is causing issues, or whether its another element, but at least works
Here’s an improved version of your code with corrections and additional debugging: