Skip to content

Latest commit

 

History

History
162 lines (124 loc) · 4.47 KB

File metadata and controls

162 lines (124 loc) · 4.47 KB
layout page
weight 0
title PHP
seo
override title description
true
Send Email with PHP and SendGrid - SendGrid Documentation | SendGrid
View code examples showing how to easily send email with PHP using cURL and SendGrid. Further explore SendGrid's email sending PHP library.
navigation
show
true

{% github sendgrid/sendgrid-php#usage PHP %} We recommend using SendGrid PHP, our client library, available on Github, with full documentation. {% endgithub %} {% anchor h2 %} Using PHP with cURL {% endanchor %} If you choose not to use SendGrid's client library you may use PHP's cURL function to query the web API.

{% codeblock lang:php %}

$user, 'api_key' => $pass, 'to' => '[email protected]', 'subject' => 'testing from curl', 'html' => 'testing body', 'text' => 'testing body', 'from' => '[email protected]', ); $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); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // obtain response $response = curl_exec($session); curl_close($session); // print everything out print_r($response); ?>

{% endcodeblock %}

An Email Sent Using the SMTPAPI Header

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 [email protected] and [email protected]. The normal to address, [email protected], will not receive an email.

{% codeblock lang:php %}

array( '[email protected]', '[email protected]' ), 'category' => 'test_category' ); $params = array( 'api_user' => $user, 'api_key' => $pass, 'x-smtpapi' => json_encode($json_string), 'to' => '[email protected]', 'subject' => 'testing from curl', 'html' => 'testing body', 'text' => 'testing body', 'from' => '[email protected]', ); $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); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // obtain response $response = curl_exec($session); curl_close($session); // print everything out print_r($response); ?>

{% endcodeblock %}

An Email Sent Including a File Attachment

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.

{% codeblock lang:php %}

$user, 'api_key' => $pass, 'to' => '[email protected]', 'subject' => 'test of file sends', 'html' => '

the HTML

', 'text' => 'the plain text', 'from' => '[email protected]', 'files['.$fileName.']' => '@'.$filePath.'/'.$fileName ); print_r($params); $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); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // obtain response $response = curl_exec($session); curl_close($session); // print everything out print_r($response); ?>

{% endcodeblock %}