We will deprecate this version on February 10, 2025. To access all the latest features and upcoming developments, please see our v3 API. For assistance with transitioning, refer to our migration guide.
We recommend using SendGrid PHP, our client library, available on GitHub, with full documentation.
The library does not officially support the V2 API, but you can use V2 with an older version of the library. For more information, see Continue Using V2 in PHP.
1// using SendGrid's PHP Library2// https://github.com/sendgrid/sendgrid-php3require 'vendor/autoload.php';4$sendgrid = new SendGrid("SENDGRID_APIKEY");5$email = new SendGrid\Email();67$email->addTo("test@sendgrid.com")8->setFrom("you@youremail.com")9->setSubject("Sending with SendGrid is Fun")10->setHtml("and fast with the PHP helper library.");1112$sendgrid->send($email);
If you choose not to use SendGrid's client library you may use PHP's cURL function to query the web API.
1<?php23require 'vendor/autoload.php';4Dotenv::load(__DIR__);5$sendgrid_apikey = getenv('YOUR_SENDGRID_APIKEY');6$sendgrid = new SendGrid($sendgrid_apikey);7$url = 'https://api.sendgrid.com/';8$pass = $sendgrid_apikey;9$template_id = '<your_template_id>';10$js = array(11'sub' => array(':name' => array('Elmer')),12'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))13);1415$params = array(16'to' => "test@example.com",17'toname' => "Example User",18'from' => "you@youremail.com",19'fromname' => "Your Name",20'subject' => "PHP Test",21'text' => "I'm text!",22'html' => "<strong>I'm HTML!</strong>",23'x-smtpapi' => json_encode($js),24);2526$request = $url.'api/mail.send.json';2728// Generate curl request29$session = curl_init($request);30// Tell PHP not to use SSLv3 (instead opting for TLS)31curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);32curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $sendgrid_apikey));33// Tell curl to use HTTP POST34curl_setopt ($session, CURLOPT_POST, true);35// Tell curl that this is the body of the POST36curl_setopt ($session, CURLOPT_POSTFIELDS, $params);37// Tell curl not to return headers, but do return the response38curl_setopt($session, CURLOPT_HEADER, false);39curl_setopt($session, CURLOPT_RETURNTRANSFER, true);4041// obtain response42$response = curl_exec($session);43curl_close($session);4445// print everything out46print_r($response);4748?>
This example takes the previous example a step further by adding our SMTPAPI header to set a category and send out to multiple recipients. The category is called test_category, and the email will go out to both example1@sendgrid.com and example2@sendgrid.com. The normal to address, example3@sendgrid.com, will not receive an email.
1<?php23$url = 'https://api.sendgrid.com/';4$user = 'USERNAME';5$pass = 'APIKEY';67$json_string = array(89'to' => array(10'example1@sendgrid.com', 'example2@sendgrid.com'11),12'category' => 'test_category'13);141516$params = array(17'api_user' => $user,18'api_key' => $pass,19'x-smtpapi' => json_encode($json_string),20'to' => 'example3@sendgrid.com',21'subject' => 'testing from curl',22'html' => 'testing body',23'text' => 'testing body',24'from' => 'example@sendgrid.com',25);262728$request = $url.'api/mail.send.json';2930// Generate curl request31$session = curl_init($request);32// Tell curl to use HTTP POST33curl_setopt ($session, CURLOPT_POST, true);34// Tell curl that this is the body of the POST35curl_setopt ($session, CURLOPT_POSTFIELDS, $params);36// Tell curl not to return headers, but do return the response37curl_setopt($session, CURLOPT_HEADER, false);38// Tell PHP not to use SSLv3 (instead opting for TLS)39curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);40curl_setopt($session, CURLOPT_RETURNTRANSFER, true);4142// obtain response43$response = curl_exec($session);44curl_close($session);4546// print everything out47print_r($response);4849?>
This example adds the additional attachment parameter to attach a file called myfile. This example assumes the file is in the same directory as your code otherwise you need to specify the full path of the file in the $filePath
variable.
1<?php23$url = 'https://api.sendgrid.com/';4$user = 'USERNAME';5$pass = 'PASSWORD';67$fileName = 'myfile';8$filePath = dirname(__FILE__);910$params = array(11'api_user' => $user,12'api_key' => $pass,13'to' => 'example@sendgrid.com',14'subject' => 'test of file sends',15'html' => '<p> the HTML </p>',16'text' => 'the plain text',17'from' => 'example@sendgrid.com',18'files['.$fileName.']' => '@'.$filePath.'/'.$fileName19);2021print_r($params);2223$request = $url.'api/mail.send.json';2425// Generate curl request26$session = curl_init($request);2728// Tell curl to use HTTP POST29curl_setopt ($session, CURLOPT_POST, true);3031// Tell curl that this is the body of the POST32curl_setopt ($session, CURLOPT_POSTFIELDS, $params);3334// Tell curl not to return headers, but do return the response35curl_setopt($session, CURLOPT_HEADER, false);36// Tell PHP not to use SSLv3 (instead opting for TLS)37curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);38curl_setopt($session, CURLOPT_RETURNTRANSFER, true);3940// obtain response41$response = curl_exec($session);42curl_close($session);4344// print everything out45print_r($response);4647?>