Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Forked from YugalXD/function.php
Created April 26, 2016 10:08
  • Star 24 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save swapnilshrikhande/d4c315b4a9590f4f91baba43a793f734 to your computer and use it in GitHub Desktop.
Send mail with mailgun api by PHP CURL.
<?php
define('MAILGUN_URL', 'https://api.mailgun.net/v3/DOMAIN_NAME');
define('MAILGUN_KEY', 'KEY');
function sendmailbymailgun($to,$toname,$mailfromnane,$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);
return $results
}
?>
@beatty12
Copy link

beatty12 commented Mar 6, 2020

Thanks for posting!

@cwicaksono
Copy link

Great code, thank you for sharing

@mblangsak
Copy link

hi,

I've got error :
{ "message": "'from' parameter is not a valid address. please check documentation" }

But if remove the string inside array, it works.

any help?

@Prabhu0610
Copy link

great code.

but why email go to spam.

please help

@priyachetwani
Copy link

Thanks. This saved my time

@will83
Copy link

will83 commented Jun 23, 2022

Great code, just some typos :

`<?php
define('MAILGUN_URL', 'https://api.mailgun.net/v3/DOMAIN_NAME');
define('MAILGUN_KEY', 'KEY');

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);
return $results;
}
?>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment