Skip to contentSkip to navigationSkip to topbar
On this page

v2 API PHP Code Example


(warning)

This documentation is for the SendGrid Web API v2

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.

(information)
(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
1
// using SendGrid's PHP Library
2
// https://github.com/sendgrid/sendgrid-php
3
require 'vendor/autoload.php';
4
$sendgrid = new SendGrid("SENDGRID_APIKEY");
5
$email = new SendGrid\Email();
6
7
$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.");
11
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.

1
<?php
2
3
require 'vendor/autoload.php';
4
Dotenv::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
);
14
15
$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
);
25
26
$request = $url.'api/mail.send.json';
27
28
// Generate curl request
29
$session = curl_init($request);
30
// Tell PHP not to use SSLv3 (instead opting for TLS)
31
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
32
curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $sendgrid_apikey));
33
// Tell curl to use HTTP POST
34
curl_setopt ($session, CURLOPT_POST, true);
35
// Tell curl that this is the body of the POST
36
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
37
// Tell curl not to return headers, but do return the response
38
curl_setopt($session, CURLOPT_HEADER, false);
39
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
40
41
// obtain response
42
$response = curl_exec($session);
43
curl_close($session);
44
45
// print everything out
46
print_r($response);
47
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.

1
<?php
2
3
$url = 'https://api.sendgrid.com/';
4
$user = 'USERNAME';
5
$pass = 'APIKEY';
6
7
$json_string = array(
8
9
'to' => array(
10
'example1@sendgrid.com', 'example2@sendgrid.com'
11
),
12
'category' => 'test_category'
13
);
14
15
16
$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
);
26
27
28
$request = $url.'api/mail.send.json';
29
30
// Generate curl request
31
$session = curl_init($request);
32
// Tell curl to use HTTP POST
33
curl_setopt ($session, CURLOPT_POST, true);
34
// Tell curl that this is the body of the POST
35
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
36
// Tell curl not to return headers, but do return the response
37
curl_setopt($session, CURLOPT_HEADER, false);
38
// Tell PHP not to use SSLv3 (instead opting for TLS)
39
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
40
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
41
42
// obtain response
43
$response = curl_exec($session);
44
curl_close($session);
45
46
// print everything out
47
print_r($response);
48
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.

1
<?php
2
3
$url = 'https://api.sendgrid.com/';
4
$user = 'USERNAME';
5
$pass = 'PASSWORD';
6
7
$fileName = 'myfile';
8
$filePath = dirname(__FILE__);
9
10
$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.'/'.$fileName
19
);
20
21
print_r($params);
22
23
$request = $url.'api/mail.send.json';
24
25
// Generate curl request
26
$session = curl_init($request);
27
28
// Tell curl to use HTTP POST
29
curl_setopt ($session, CURLOPT_POST, true);
30
31
// Tell curl that this is the body of the POST
32
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
33
34
// Tell curl not to return headers, but do return the response
35
curl_setopt($session, CURLOPT_HEADER, false);
36
// Tell PHP not to use SSLv3 (instead opting for TLS)
37
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
38
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
39
40
// obtain response
41
$response = curl_exec($session);
42
curl_close($session);
43
44
// print everything out
45
print_r($response);
46
47
?>

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.