Skip to content

Instantly share code, notes, and snippets.

@lox
Created July 18, 2010 05:58
Show Gist options
  • Save lox/480170 to your computer and use it in GitHub Desktop.
Save lox/480170 to your computer and use it in GitHub Desktop.
<?php
// call looks like this:
sendgrid_send(array(
'from'=>'no-reply@justasktom.com',
'fromname'=>'JustAskTom.com',
'to'=>'lachlan@ljd.cc',
'subject'=>'Blargh.',
'text'=>'test',
'html'=>'<div><h2>Email Test</h2><p>Testing email</p></div>',
));
/**
* Send an email via sendgrid.
*
* Supported params:
* to => an array of users to send mail to
* toname => an array of users to send mail to
* subject => a subject line
* from => a from address
* fromname => the name of the from address
* text => the text body of the email
* html => the html body of the email
* category => a category to use in sendgrid
*/
function sendgrid_send($params=array())
{
$request = array(
'api_user'=>SENDGRID_USER,
'api_key'=>SENDGRID_PASSWORD,
);
// copy specific params into the request
foreach(array('to','toname','subject','from','fromname','text','html','category') as $k)
{
if(isset($params[$k]))
$request[$k] = $params[$k];
}
// generate curl request
$curl = curl_init('http://sendgrid.com/api/mail.send.json');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// obtain json response from sendgrid.com
if(!($response = curl_exec($curl)))
throw new Exception("Sendgrid call failed: ". curl_error($curl));
//$json = json_decode($response);
curl_close($curl);
var_dump($request);
var_dump($response);
// handle errors
//if(isset($response->error))
// throw new Exception(
// 'SendGrid.com returned: '.$json->error->message,
// $json->error->code);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment