Skip to content

Instantly share code, notes, and snippets.

@drewgates
Last active November 30, 2016 09:52
Show Gist options
  • Save drewgates/06b15950a4175a7b0b017a2b4d408498 to your computer and use it in GitHub Desktop.
Save drewgates/06b15950a4175a7b0b017a2b4d408498 to your computer and use it in GitHub Desktop.
Send email with mailgun.
<?php
//from email can be set in the relevant curl_setopt below.
//for my purposes, I was always using the same from email and didn't need one more variable to pass into this function.
//
function send_mail($email,$subject,$msg,$mailDomain,$api) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, 'api:'.$api_key);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$mailDomain.'/messages');
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'from' => "no-reply <no-reply@$mailDomain>",
'to' => $email,
'subject' => $subject,
'html' => $msg
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//uncomment (and complete) the lines below to test the above function with your settings.
//$email="user@example.com";
//$subject="Test of email configuration.";
//$msg="This message should be considered a success";
//$mailDomain="example.com"
//$mailgunAPI="ENTER YOUR API KEY HERE"
//$result = send_mail($email,$subject,$msg,$mailDomain,$mailgunAPI);
//echo $result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment