Skip to content

Instantly share code, notes, and snippets.

@seanslater
Created October 22, 2015 21:00
Show Gist options
  • Save seanslater/6e38570713d9edeb85d2 to your computer and use it in GitHub Desktop.
Save seanslater/6e38570713d9edeb85d2 to your computer and use it in GitHub Desktop.
Send test email using SendGrid API with PHP / cURL
<?php
$url = 'https://api.sendgrid.com/';
$user = 'sendgridapiusername';
$pass = 'sendgridapipassword';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => '[email protected]',
'subject' => 'Testing using PHP/Curl/SendGrid',
'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);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment