Skip to content

Instantly share code, notes, and snippets.

@marchildmann
Last active November 18, 2018 11:00
Show Gist options
  • Save marchildmann/a9888101f1e672999488388b1aafb123 to your computer and use it in GitHub Desktop.
Save marchildmann/a9888101f1e672999488388b1aafb123 to your computer and use it in GitHub Desktop.
PHP Mailgun CURL example
<?php
define('MAILGUN_URL', 'https://api.mailgun.net/v3/DOMAIN');
define('MAILGUN_KEY', 'key-123341442922');
function sendmailByMailGun($to,$toname,$mailfromname,$mailfrom,$subject,$html,$text,$tag,$replyto){
$array_data = array(
'from'=> $mailfromname .'<'.$mailfrom.'>',
'to'=>$toname.'<'.$to.'>',
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto
);
$session = curl_init(MAILGUN_URL.'/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
$results = json_decode($response, true);
print_r($results['message']);
}
$to = "[email protected]";
$toname = "Joe";
$mailfromname = "Acme Inc.";
$mailfrom = "[email protected]";
$subject = "Notification for machine XYZ";
$html = "<p>This is just a sample message.</p>";
$text = "This is just a sample message.";
$tag = "Acme Inc.";
$replyto = "[email protected]";
sendmailByMailGun($to,$toname,$mailfromname,$mailfrom,$subject,$html,$text,$tag,$replyto);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment