Skip to content

Instantly share code, notes, and snippets.

@Ahmedlag
Forked from jonaslejon/Mailgun PHP API with curl
Last active May 15, 2018 22:02
Show Gist options
  • Save Ahmedlag/bdd9da40bec36d8946abe6c8be10ecac to your computer and use it in GitHub Desktop.
Save Ahmedlag/bdd9da40bec36d8946abe6c8be10ecac to your computer and use it in GitHub Desktop.
function br2nl($string){
return preg_replace('#<br\s*/?-->#i', "\n", $string);
}
define('MAILGUN_API',"apikeygoeshere");
define('DOMAIN',"www.website.com");
function mg_send($Receiver, $Title, $Message) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.MAILGUN_API);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// If you are not using SSL set this to 0 or false
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$Text = strip_tags(br2nl($Message));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.DOMAIN.'/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('from' => 'support@'.DOMAIN,
'to' => $Receiver,
'subject' => $Title,
'html' => $Message,
'text' => $Text));
$Response = json_decode(curl_exec($ch),true);
if(isset($Response['id']))
{
curl_close($ch);
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment