skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    • More examples of blank emails where BCC count was greater than 5k
    • Noticed the $customerlist on the examples given had spaces, tabs, new lines, unclear if it caused issues but changed how this variable is set to remove those
    • I need to do a re-write on the code overall, I won't pain you showing you it but I haven't properly handled special characters and I'm not confident quotation marks within $description which was an element of the HTML code could be causing it, so I removed these

    I have just re-sent the comms with:

    • Splitting the comms into bunches of 1000 BCC
    • Removing any spaces, new lines and tabs from the $customerlist
    • Removed special characters from $description as a precaution

    This worked fine For anyone else's benefit, here is the piece of code used to split it out:

    $addresses = explode(',', $customerlist);
    $chunks = array_chunk($addresses,1000);
        
    foreach ($chunks as $index => $chunk) {
        $mail->ClearAllRecipients();
        $mail->addAddress('[email protected]', 'Notifications');
     
        foreach ($chunk as $address) {
            $mail->addBCC($address);
        }
    
        **Original code here containing the ob_start and HTML content**
        
        if ($mail->send()) {
            echo "Mail Sent";
        } else {
            echo "Mail NOT Sent";
        }
    }
    

    So haven't worked out why over 5000 is causing issues, or whether its another element, but at least works


  2. Here’s an improved version of your code with corrections and additional debugging:

    use PHPMailerPHPMailerPHPMailer;
    use PHPMailerPHPMailerSMTP;
    use PHPMailerPHPMailerException;
    
    $mail = new PHPMailer(true); // Enable exceptions
    try {
        $mail->isSMTP();
        $mail->Host = 'your.smtp.host'; // Correct SMTP host
        $mail->Port = 587; // Valid SMTP port
        $mail->SMTPAuth = true;
        $mail->Username = 'your_username';
        $mail->Password = 'your_password';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // or ENCRYPTION_SMTPS
        $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) {
            if (filter_var(trim($address), FILTER_VALIDATE_EMAIL)) { // Validate each email
                $mail->addBCC(trim($address));
            }
        }
        
        $mail->Subject = "REMINDER: Test";
        ob_start();
        ?>
        <html>
        <!-- Loads of HTML here -->
        </html>
        <?php
        $body = ob_get_clean();
        $mail->Body = $body;
        $mail->AltBody = 'This is the plain-text version of the email.';
        $mail->isHTML(true);
    
        if (!$mail->send()) {
            echo 'Mail did not send. Error: ' . $mail->ErrorInfo;
        } 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();
        }
    } catch (Exception $e) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search