Use Mailgun Plain PHP Curl Code

Mailgun is one of some services to send e-mail. It has a nice PHP library here. But for some reason, it only works in my local server, and it does not work at all in remote server and the code returns “Mailgun server is not reachable” error (although i have tried to disable server firewall, which was not a best decision).

So i tried to search simple PHP Curl code, and i found it here.

The code works flawlessly and now i can use Mailgun to send email in my server.

Here is a snippet of the code

$array_data = [
	'from' => 'YOURNAME <[email protected]>',
	'to' => 'RECIPIENT_NAME <[email protected]>',
	'subject' => 'SUBJECT',
	'html' => 'HTML MESSAGE',
	'text' => 'PLAIN_MESSAGE',
	'o:tracking' => 'yes',
	'o:tracking-clicks' => 'yes',
	'o:tracking-opens' => 'yes',
	//'o:tag'=>$tag,
	'h:Reply-To' => '[email protected]'
];

$session = curl_init('https://api.mailgun.net/v3/YOURMAILGUNDOMAIN.COM/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:YOURMAILGUNKEY');
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);

if ('' != $response) {
	return json_decode($response, true);
} else {
	return false;
}

Leave a comment

Your email address will not be published. Required fields are marked *