0

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.";
}
rhtwenty1
  • 1
  • 2
  • 1
    I think it would be better to write a single function, instead of repeating the same code 3 times. Having sleep times of 10 seconds is pushing it a bit because the default maximum executing time of a PHP script is 30 seconds. After you've created the single function I would add simple logging to it that will tell you if a mail was sent to MailJet. Also check the error log. – KIKO Software Nov 18 '21 at 00:07
  • Thanks for reminding me of the maximum execution time of php. I will definitely reduce the 10 second sleep. I was failing on getting my code to send to multiple emails using a single function. Any input on how I can do this? – rhtwenty1 Nov 18 '21 at 00:11
  • Might not even need a function. Make `body` a multidimensional then iterate over it and on each loop execute the `curl` code.. put diagnostic info if it is still failing and see where/why. – user3783243 Nov 18 '21 at 00:54

0 Answers0