Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send mail with Mailgun API version 2 and PHP. Should also work with version 3 of the Mailgun API
define("DOMAIN", "test.se");
define("MAILGUN_API", "XXX123"); // Mailgun Private API Key
function br2nl($string) {
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}
function mg_send($to, $subject, $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);
$plain = 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' => $to,
'subject' => $subject,
'html' => $message,
'text' => $plain));
$j = json_decode(curl_exec($ch));
$info = curl_getinfo($ch);
if($info['http_code'] != 200)
return false;
curl_close($ch);
return $j;
}
@fdcore

This comment has been minimized.

Copy link

fdcore commented Feb 7, 2016

Function br2nl not found.

@chriswitty

This comment has been minimized.

Copy link

chriswitty commented Feb 25, 2016

It's a typo, change line 9 from,
$plain = strip_tags(br2nl($message)); to

$plain = strip_tags(nl2br($message));

@Punchlinern

This comment has been minimized.

Copy link

Punchlinern commented Sep 29, 2016

I might be mistaken, but I believe the author intended for it to be br2nl, since it's used when convert the HTML-message to plaintext. Maybe he has written such a function himself, something like this.

@abdulmateen1

This comment has been minimized.

Copy link

abdulmateen1 commented Oct 14, 2016

sir... how to attach file with parameter can you please help me?
im using this :
'attachment' => '@'.$filepath
));

and its giving me response error...

@qexon-ankit

This comment has been minimized.

Copy link

qexon-ankit commented Dec 15, 2016

i am facing problem when i use your code it shows


Fatal error: Call to undefined function error() in

is there any solution when i print array

Array
(
[url] => https://api.mailgun.net/v2/sandbox0935d6bba68e493983fc30026115dd92.mailgun.org/messages
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 1
[redirect_count] => 0
[total_time] => 0.594
[namelookup_time] => 0
[connect_time] => 0.297
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 104.130.177.23
[certinfo] => Array
(
)

[primary_port] => 443
[local_ip] => 192.168.1.7
[local_port] => 58575

)

@viniciusbo

This comment has been minimized.

Copy link

viniciusbo commented Jan 30, 2017

@qexon-ankit just replace the error() function call (line 24) with return false.

@viniciusbo

This comment has been minimized.

Copy link

viniciusbo commented Jan 30, 2017

@jonaslejon any specific reason you chose to work with Mailgun API v2?

@BenMic

This comment has been minimized.

Copy link

BenMic commented Jun 1, 2017

Very useful. Works with Mailgun API v3.

@jonaslejon

This comment has been minimized.

Copy link
Owner Author

jonaslejon commented May 20, 2018

Thanks for all the comments! The code has been updated to work better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.