How to Send Bulk Emails Via SendGrid From PHP cURL

By | June 13, 2016

SendGrid is an Email delivery platform which can be used for sending bulk emails. Every Business needs to send Transactional or Marketing email to their users, sendgrid is one of the best platform to send bulk emails since it allows us to use their flexible API’s which further makes the Integration process easier.

In this post we will use curl with php to send bulk emails via SendGrid, before moving to tutorial lets check out some of the features offered by SendGrid.

send bulk emails via sendgrid

Features of SendGrid

  • Real-time metrics about user’s engagement.
  • Flexible API
  • Report generation
  • Autoresponders.

realtime stats

Send Bulk Emails Via SendGrid From PHP cURL

First and foremost create an account on sendgrid it is not a free service. After signing up to your account Login to your account. Once you log in on the left side you will find API keys under settings. This can be used for sending emails, so copy it and save it.

Here is the code for sending Emails using Sendgrid in PHP

<?php  
 $user = $this->config->item('username');
 $pass = $this->config->item('password');
 $url = 'https://api.sendgrid.com/';
 $params = array(
 'api_user' => $user,
 'api_key' => 'Your sendgrid API key',
 'to' => $email,
 'subject' => $subject,
 'html' => $message,
 'from' => 'info@techplugg.com',
 );

 $request = $url.'api/mail.send.json';
 // Generate curl request
 
 $session = curl_init($request);
 // Tell curl to use HTTP POST
 curl_setopt ($session, CURLOPT_POST, true);
 
 // Tell curl that this is the body of the POST
 curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
 
 // Tell curl not to return headers, but do return the response
 curl_setopt($session, CURLOPT_HEADER, false);
 // Tell PHP not to use SSLv3 (instead opting for TLS)
 //curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
 //Turn off SSL
 curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);//New line
 curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false);//New line
 curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
 
 // obtain response
 $response = curl_exec($session);
 // print everything out
 var_dump($response,curl_error($session),curl_getinfo($session));
 curl_close($session);
?>

Some of the prominent companies like Uber, Spotify, Glassdoor & Airnub are already using SendGrid, so what are you waiting for? if you have good number of users then start using SendGrid.

Also check Tutorial on Login System with PHP

PS- I am not affiliated to SendGrid. After using sendgrid we found it useful therefore we decided to share tutorial related to sendgrid.

  • Sumit Joshi

    How to send mass emails? Why to use cURL or any other things I don’t want to use Sendgrid?

    • sangram

      It’s a paid service if you need to send bulk emails then it gives better functionality