'Using PHP mail() function twice in a row is causing second use to be missing content
Problem: The sendEmail() function in the second ob_start works EXCEPT that the $emailContents are not being received in the Email.
NOTES:
- The first email is sent containing everything as expected.
- The second email is also sent with the subject but no contents
- If I take the second instance and test it by itself, the email is sent containing everything as expected.
- The file paths are correct and the files are where they are supposed to be.
I know you may need to see more, but from my perspective the following code are the only parts involved that could possibly contain the cause of the problem, but let me know if there's something else you need to see to help me.
function sendEmail($mailTo, $emailSubject, $emailContents)
{
$headers = "From: registrations@" . DOMAIN . "\r\n";
$headers .= "Reply-To: registrations@" . DOMAIN . "\r\n";
$headers .= "Return-Path: registrations@" . DOMAIN . "\r\n";
$headers .= "MIME-Version:1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($mailTo, $emailSubject, $emailContents, $headers);
}
The above function and the problem area are in two separate files:
ob_start();
require_once __DIR__ . '/../../Emails/registration.php';
$emailContents = ob_get_clean();
sendEmail($user['email'], 'Account Registration at ' . FULL_SITE_NAME, $emailContents);
ob_start();
require_once __DIR__ . '/../../Emails/registrationAlert.php';
$emailContents = ob_get_clean();
sendEmail(ALERT_EMAIL, 'Registration by ' . $user['firstName'] . ' ' . $user['lastName'], $emailContents);
The following is the /Emails/registrationAlert.php but it works when by itself, exactly as expected! It is ONLY when the two emails are being sent side by side like shown above that the second ones contents are missing.
ob_start();
require_once __DIR__ . '/Sections/logoHeader.php';
?>
<tr>
<td style="padding:30px;background-color:#ffffff;">
<h1 style="margin-top:0;margin-bottom:16px;font-size:26px;line-height:32px;font-weight:bold;letter-spacing:-0.02em;">New Registered Member!</h1>
<h2 style="margin-top:0;margin-bottom:16px;font-size:18px;line-height:32px;font-weight:bold;letter-spacing:-0.02em;"><?=$user['firstName'] . ' ' . $user['lastName']?> Registration</h2>
<p style="margin:0;"><?=$user['firstName'] . ' ' . $user['lastName']?> has registered using <?=$user['email']?></p>
</td>
</tr>
<?php
require_once __DIR__ . '/Sections/regularFooter.php';
$thisEmailContents = ob_get_clean();
require_once __DIR__ . '/emailTemplate.php'
What am I missing? I've even changed the second $emailContents var to $emailContents2 out of desperation, and oddly then the email subject header was missing. Even some hints as to where you would look for problems if you can't see clearly what I've done wrong would be helpful.
Is there a timing issue with mail? I've used mail to submit dozens of emails back to back in loops, maybe the loop provides enough time for the previous email to complete? I don't know, I'm literally just guessing at this point.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
