Skip to content

Instantly share code, notes, and snippets.

@ryanseys
Created March 15, 2015 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanseys/060e21cd50fd351c1125 to your computer and use it in GitHub Desktop.
Save ryanseys/060e21cd50fd351c1125 to your computer and use it in GitHub Desktop.
Sending a message with Mailgun API using PHP (no dependencies needed)
<?php
function send_mailgun($email, $body) {
$domain = "YOUR_DOMAIN.COM";
$config = array();
$config['api_key'] = "YOUR_KEY_HERE";
$config['api_url'] = "https://api.mailgun.net/v2/" . $domain . "/messages";
$message = array();
$message['from'] = "Mailgun <YOU@YOUR_ADDRESS_HERE.com>";
$message['to'] = $email;
$message['h:Reply-To'] = "<YOU@YOUR_REPLY_ADDRESS_HERE.com>";
$message['subject'] = "Eye-Catching Subject Line";
$message['html'] = $body;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['api_url']);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "api:{$config['api_key']}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo send_mailgun("THE@RECIPIENT_EMAIL_HERE.com", "Body of the message here!");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment