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.
_12// using SendGrid's PHP Library_12// https://github.com/sendgrid/sendgrid-php_12require 'vendor/autoload.php';_12$sendgrid = new SendGrid("SENDGRID_APIKEY");_12$email = new SendGrid\Email();_12_12$email->addTo("test@sendgrid.com")_12 ->setFrom("you@youremail.com")_12 ->setSubject("Sending with SendGrid is Fun")_12 ->setHtml("and fast with the PHP helper library.");_12_12$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.
_48<?php_48_48require 'vendor/autoload.php';_48Dotenv::load(__DIR__);_48$sendgrid_apikey = getenv('YOUR_SENDGRID_APIKEY');_48$sendgrid = new SendGrid($sendgrid_apikey);_48$url = 'https://api.sendgrid.com/';_48$pass = $sendgrid_apikey;_48$template_id = '<your_template_id>';_48$js = array(_48 'sub' => array(':name' => array('Elmer')),_48 'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))_48);_48_48$params = array(_48 'to' => "test@example.com",_48 'toname' => "Example User",_48 'from' => "you@youremail.com",_48 'fromname' => "Your Name",_48 'subject' => "PHP Test",_48 'text' => "I'm text!",_48 'html' => "<strong>I'm HTML!</strong>",_48 'x-smtpapi' => json_encode($js),_48 );_48_48$request = $url.'api/mail.send.json';_48_48// Generate curl request_48$session = curl_init($request);_48// Tell PHP not to use SSLv3 (instead opting for TLS)_48curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);_48curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $sendgrid_apikey));_48// Tell curl to use HTTP POST_48curl_setopt ($session, CURLOPT_POST, true);_48// Tell curl that this is the body of the POST_48curl_setopt ($session, CURLOPT_POSTFIELDS, $params);_48// Tell curl not to return headers, but do return the response_48curl_setopt($session, CURLOPT_HEADER, false);_48curl_setopt($session, CURLOPT_RETURNTRANSFER, true);_48_48// obtain response_48$response = curl_exec($session);_48curl_close($session);_48_48// print everything out_48print_r($response);_48_48?>
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.
_49<?php_49_49$url = 'https://api.sendgrid.com/';_49$user = 'USERNAME';_49$pass = 'APIKEY';_49_49$json_string = array(_49_49 'to' => array(_49 'example1@sendgrid.com', 'example2@sendgrid.com'_49 ),_49 'category' => 'test_category'_49);_49_49_49$params = array(_49 'api_user' => $user,_49 'api_key' => $pass,_49 'x-smtpapi' => json_encode($json_string),_49 'to' => 'example3@sendgrid.com',_49 'subject' => 'testing from curl',_49 'html' => 'testing body',_49 'text' => 'testing body',_49 'from' => 'example@sendgrid.com',_49 );_49_49_49$request = $url.'api/mail.send.json';_49_49// Generate curl request_49$session = curl_init($request);_49// Tell curl to use HTTP POST_49curl_setopt ($session, CURLOPT_POST, true);_49// Tell curl that this is the body of the POST_49curl_setopt ($session, CURLOPT_POSTFIELDS, $params);_49// Tell curl not to return headers, but do return the response_49curl_setopt($session, CURLOPT_HEADER, false);_49// Tell PHP not to use SSLv3 (instead opting for TLS)_49curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);_49curl_setopt($session, CURLOPT_RETURNTRANSFER, true);_49_49// obtain response_49$response = curl_exec($session);_49curl_close($session);_49_49// print everything out_49print_r($response);_49_49?>
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.
_47<?php_47_47$url = 'https://api.sendgrid.com/';_47$user = 'USERNAME';_47$pass = 'PASSWORD';_47_47$fileName = 'myfile';_47$filePath = dirname(__FILE__);_47_47$params = array(_47 'api_user' => $user,_47 'api_key' => $pass,_47 'to' => 'example@sendgrid.com',_47 'subject' => 'test of file sends',_47 'html' => '<p> the HTML </p>',_47 'text' => 'the plain text',_47 'from' => 'example@sendgrid.com',_47 'files['.$fileName.']' => '@'.$filePath.'/'.$fileName_47 );_47_47print_r($params);_47_47$request = $url.'api/mail.send.json';_47_47// Generate curl request_47$session = curl_init($request);_47_47// Tell curl to use HTTP POST_47curl_setopt ($session, CURLOPT_POST, true);_47_47// Tell curl that this is the body of the POST_47curl_setopt ($session, CURLOPT_POSTFIELDS, $params);_47_47// Tell curl not to return headers, but do return the response_47curl_setopt($session, CURLOPT_HEADER, false);_47// Tell PHP not to use SSLv3 (instead opting for TLS)_47curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);_47curl_setopt($session, CURLOPT_RETURNTRANSFER, true);_47_47// obtain response_47$response = curl_exec($session);_47curl_close($session);_47_47// print everything out_47print_r($response);_47_47?>