I have been using the following code to send daily emails to 3 different addresses using curl and PHP on POST via MailJet. The emails appeared to be sending successfully and contain accurate data. However, I find that there are occasions when not all three emails go through. For example, for the past three days, emails were being sent to 2 out of 3 email addresses. However, today the email went to all 3 addresses successfully. I have checked my MailJet reports as well to verify that the emails weren't being blocked on their end, and am not seeing the missing emails there either.
Any ideas on why this could be happening? I attempted to add a "sleep" delay of 10 second in an attempt to keep the emails from being sent too quickly, but was still experiencing the same issue.
Also, this was my first time using curl... and I was only successful at getting multiple emails to send by repeating my curl code altogether. Any insight on how to reduce the amount of code would be greatly appreciated.
//Send Email Notification Using
//First Email Address
$body = [
'Messages' => [
[
'From' => [
'Email' => "email@gmail.com",
'Name' => "IT Dept"
],
'To' => [
[
'Email' => "email1@gmail.com",
'Name' => "Recipiant1"
],
],
'Subject' => "Your Daily Data is Here",
'HTMLPart' => "<center><h2>Your Daily Work Totals are Below:</h2></center><br /> " . " <h2>Entry Date/Time:</h2> " . date("F j, Y, g:i a") . " <h2>Order:</h2> " . $_POST['Order_Quantity'] . " <h2>Product:</h2> " . $_POST['Product_Quantity'] . "<h2>Employee Count:</h2> " . $_POST['Crew_Count'] . " <h2>Daily Total:</h2> " . $sum = array_sum($_SESSION) . " <center><h2>Link to Report:</h2></center> <h3>google.com</h3> "
]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mailjet.com/v3.1/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
curl_setopt($ch, CURLOPT_USERPWD, "gjirhhighighihihighrihihrihgirghihihihihihih");
$server_output = curl_exec($ch);
curl_close ($ch);
sleep(10);
}
//Fetch Results
$response = json_decode($server_output);
if ($response->Messages[0]->Status == 'success' ) {
echo "Email sent successfully.";
}
//Second Email Address
$body = [
'Messages' => [
[
'From' => [
'Email' => "fromemail@gmail.com",
'Name' => "IT Dept"
],
'To' => [
[
'Email' => "email2@gmail.com",
'Name' => "Recipiant2"
],
],
'Subject' => "Your Daily Data is Here",
'HTMLPart' => "<center><h2>Your Daily Work Totals are Below:</h2></center><br /> " . " <h2>Entry Date/Time:</h2> " . date("F j, Y, g:i a") . " <h2>Order:</h2> " . $_POST['Order_Quantity'] . " <h2>Product:</h2> " . $_POST['Product_Quantity'] . "<h2>Employee Count:</h2> " . $_POST['Crew_Count'] . " <h2>Daily Total:</h2> " . $sum = array_sum($_SESSION) . " <center><h2>Link to Report:</h2></center> <h3>google.com</h3> "
]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mailjet.com/v3.1/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
curl_setopt($ch, CURLOPT_USERPWD, "gjirhhighighihihighrihihrihgirghihihihihihih");
$server_output = curl_exec($ch);
curl_close ($ch);
sleep(10);
}
//Fetch Results
$response = json_decode($server_output);
if ($response->Messages[0]->Status == 'success' ) {
echo "Email sent successfully.";
}
//Third Email Address
$body = [
'Messages' => [
[
'From' => [
'Email' => "fromemail@gmail.com",
'Name' => "IT Dept"
],
'To' => [
[
'Email' => "email3@gmail.com",
'Name' => "Recipiant3"
],
],
'Subject' => "Your Daily Data is Here",
'HTMLPart' => "<center><h2>Your Daily Work Totals are Below:</h2></center><br /> " . " <h2>Entry Date/Time:</h2> " . date("F j, Y, g:i a") . " <h2>Order:</h2> " . $_POST['Order_Quantity'] . " <h2>Product:</h2> " . $_POST['Product_Quantity'] . "<h2>Employee Count:</h2> " . $_POST['Crew_Count'] . " <h2>Daily Total:</h2> " . $sum = array_sum($_SESSION) . " <center><h2>Link to Report:</h2></center> <h3>google.com</h3> "
]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mailjet.com/v3.1/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
curl_setopt($ch, CURLOPT_USERPWD, "gjirhhighighihihighrihihrihgirghihihihihihih");
$server_output = curl_exec($ch);
curl_close ($ch);
sleep(10);
}
//Fetch Results
$response = json_decode($server_output);
if ($response->Messages[0]->Status == 'success' ) {
echo "Email sent successfully.";
}