Jason D'Silva
Jason D'Silva

Reputation: 1

SendGrid v3 API endpoint slowing down after 24-25 emails sent in a single script run using cURL

I have a PHP script that sends out emails to customers once a week using the SendGrid v3 API endpoint. Currently, there are approximately anywhere between 90 - 120 emails to be sent per week.

The script is using cURL to send the request to SendGrid, which in turn uses Dynamic Templates to tailor the emails being sent to customers. However, I noticed that the first 24-25 emails get sent very quickly. The subsequent emails take longer to send until it reaches a point where they stop getting sent out. I am able to send only about 53-56 emails per script run.

The script reuses the curl handle and as such, only the first email should take longer to send since cURL will take time to establish a connection. However, I am not sure why cURL is taking time to send emails after the count of 25.

At first, I was sending out the emails using just 1 cURL connection. However, after this issue, I tried to send out emails in batches of 20 i.e. after 20 emails being sent out, I closed the cURL connection and reinitialized it (I am currently restricted to using PHP 5.6)

I reached out to SendGrid regarding this. However, as per their logs, they are receiving the request late for the emails being sent out after the count of 25. Please note that emails are being sent successfully. However, the time to send the emails keeps increasing beyond the count of 25 emails. Below is the code being used to send out emails.

Would appreciate if I could get some eyes on this as to why it is taking and increasing amount of time to send emails after 24-25 emails have been sent out.

Note: I also checked CPU Utilization and Memory usage on the server on which the script was run as well as on my local machine when testing. In both cases, there was no marked increase when the script was being run.

$url = 'https://api.sendgrid.com/v3/mail/send';
$curl_headers = array();
$curl_headers[] = 'Content-Type:application/json';
$curl_headers[] = 'Authorization:Bearer [AddBearerTokenHere]';

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_VERBOSE => FALSE,
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_IPRESOLVE       => CURL_IPRESOLVE_V4,
    CURLOPT_HTTPHEADER      => $curl_headers,
    CURLOPT_SSL_VERIFYPEER => FALSE
));

$mails_sent_count = 0;
$batch_size = 20;

foreach($mail_data_to_send AS $key=>$val)
{
    if($mails_sent_count == $batch_size)
    {
        //Re-instantiate the curl connection for the new batch
        if($ch)
        {
            curl_close($ch);
            sleep(1);
        }
        
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_VERBOSE => FALSE,
            CURLOPT_POST => TRUE,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_IPRESOLVE       => CURL_IPRESOLVE_V4,
            CURLOPT_HTTPHEADER      => $curl_headers,
            CURLOPT_SSL_VERIFYPEER => FALSE
        ));
        
        $mails_sent_count = 0;
    }
    
    $data = [];
    $data['from']['email'] = '[From Email Address]';
    $data['from']['name'] = '[From Name]';
    $data['personalizations'] = array();
    $data['personalizations'][] = array(
        'to'    =>  array(array(
            'email' =>  $val['EmailID']
        )),
        'dynamic_template_data' =>  array(
            'name'  =>  $val['name'],
            'data1' =>  $val['data1'],
            'data2' =>  $val['data2'],
            'data3' =>  $val['data3'],
            'data4' =>  $val['data4'],
            'data5' =>  $val['data5'],
        )
    );
    
    $data['asm']['group_id'] = [Group ID];
    
    if($val['data6'] == 'val1')
    {
        $data['template_id'] = '[Template ID 1]';
    }
    elseif($val['data6'] == 'val2')
    {
        $data['template_id'] = '[Template ID 2]';
    }
    elseif($val['data7'] == 'val3')
    {
        $data['template_id'] = '[Template ID 3]';
    }
    elseif($val['data8'] == 'val4')
    {
        $data['template_id'] = '[Template ID 4]';
    }
    
    $postfields = json_encode($data);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if(curl_errno($ch))
    {
        $curl_error = curl_error($ch);
        
        //Need to log the exception and attempt sending other emails
        //As the error could be only for one particular email
        
        continue;
    }
    elseif(($httpcode == 200) || ($httpcode == 202))
    {
        //Update Email Sent Flag
    }
    else
    {
        /**
         * All other error codes are errors
         */
        //Log error entry
    }
    
    $mails_sent_count++;
}

if($ch)
{
    curl_close($ch);
}

Upvotes: 0

Views: 225

Answers (1)

Ritesh Mishra
Ritesh Mishra

Reputation: 142

We were facing the same issue with javascript sdks and direct api calls too. We raised a ticket with Sendgrid team and this is the response.. Will update further if something comes back.

Response from Sendgrid

https://www.twilio.com/docs/sendgrid/for-developers/sending-email/v3-mail-send-faq#are-there-limits-on-how-often-i-can-send-email-and-how-many-recipients-i-can-send-to

Upvotes: 0

Related Questions