Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

v2 API PHP Code Example


(information)

Info

We recommend using SendGrid PHP, our client library, available on GitHub(link takes you to an external page), with full documentation.

(information)

Info

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(link takes you to an external page).


Using SendGrid's PHP Library

using-sendgrids-php-library page anchor

_12
// using SendGrid's PHP Library
_12
// https://github.com/sendgrid/sendgrid-php
_12
require '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
_48
require 'vendor/autoload.php';
_48
Dotenv::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)
_48
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
_48
curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $sendgrid_apikey));
_48
// Tell curl to use HTTP POST
_48
curl_setopt ($session, CURLOPT_POST, true);
_48
// Tell curl that this is the body of the POST
_48
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
_48
// Tell curl not to return headers, but do return the response
_48
curl_setopt($session, CURLOPT_HEADER, false);
_48
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
_48
_48
// obtain response
_48
$response = curl_exec($session);
_48
curl_close($session);
_48
_48
// print everything out
_48
print_r($response);
_48
_48
?>


An Email Sent Using the SMTPAPI Header

an-email-sent-using-the-smtpapi-header page anchor

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
_49
curl_setopt ($session, CURLOPT_POST, true);
_49
// Tell curl that this is the body of the POST
_49
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
_49
// Tell curl not to return headers, but do return the response
_49
curl_setopt($session, CURLOPT_HEADER, false);
_49
// Tell PHP not to use SSLv3 (instead opting for TLS)
_49
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
_49
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
_49
_49
// obtain response
_49
$response = curl_exec($session);
_49
curl_close($session);
_49
_49
// print everything out
_49
print_r($response);
_49
_49
?>


An Email Sent Including a File Attachment

an-email-sent-including-a-file-attachment page anchor

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
_47
print_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
_47
curl_setopt ($session, CURLOPT_POST, true);
_47
_47
// Tell curl that this is the body of the POST
_47
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
_47
_47
// Tell curl not to return headers, but do return the response
_47
curl_setopt($session, CURLOPT_HEADER, false);
_47
// Tell PHP not to use SSLv3 (instead opting for TLS)
_47
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
_47
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
_47
_47
// obtain response
_47
$response = curl_exec($session);
_47
curl_close($session);
_47
_47
// print everything out
_47
print_r($response);
_47
_47
?>


Rate this page: