Skip to content

Instantly share code, notes, and snippets.

@stevenmc
Created September 29, 2015 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenmc/b8691ffd94b43da130d8 to your computer and use it in GitHub Desktop.
Save stevenmc/b8691ffd94b43da130d8 to your computer and use it in GitHub Desktop.
Send email with Mailgun via cURL
function mailgun($message = null, $to = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-abc');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$plain = strip_tags($message);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/DOMAIN-abc/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('from' => 'Joe Bloggs <person@example.com>',
'to' => $to,
'subject' => 'Subject line',
'html' => $message,
'text' => $plain));
$j = json_decode(curl_exec($ch));
$info = curl_getinfo($ch);
if ($info['http_code'] != 200){
echo "Email could not be sent";
exit;
}
curl_close($ch);
return $j;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment