Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
?>
@peter279k

This comment has been minimized.

Copy link

@peter279k peter279k commented Oct 31, 2017

Hi Sir, thank you provide the great sample code.
But You have add the ; on line 31.

The variable $mailfromnane has been changed to $mailfromname on line 5.

Thanks.

@MalikClaveau

This comment has been minimized.

Copy link

@MalikClaveau MalikClaveau commented Feb 16, 2018

For me, I got this and don't know why.
"Free accounts are for test purposes only. Please upgrade or add the address to authorized recipients in Account Settings."

@davidfavor

This comment has been minimized.

Copy link

@davidfavor davidfavor commented Apr 21, 2018

You'll get the "Free accounts are for test purposes only" message, until you add a credit card to your account.

@yakubu234

This comment has been minimized.

Copy link

@yakubu234 yakubu234 commented Apr 2, 2019

please how can i identify the mailgun key

@jordan26

This comment has been minimized.

@beatty12

This comment has been minimized.

Copy link

@beatty12 beatty12 commented Mar 6, 2020

Thanks for posting!

@cwicaksono

This comment has been minimized.

Copy link

@cwicaksono cwicaksono commented Mar 9, 2020

Great code, thank you for sharing

@mblangsak

This comment has been minimized.

Copy link

@mblangsak mblangsak commented May 6, 2020

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

This comment has been minimized.

Copy link

@Prabhu0610 Prabhu0610 commented Sep 25, 2020

great code.

but why email go to spam.

please help

@priyachetwani

This comment has been minimized.

Copy link

@priyachetwani priyachetwani commented Dec 2, 2020

Thanks. This saved my time

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