Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send email using PHP/cURL + Mailgun
<?php
// [TODO] Update below parameters
define('MAILGUN_KEY', 'XXX-XXX...XXX');
define('MAILGUN_URL', 'https://api.mailgun.net/v3/mg.domain.tld');
define('MAIL_FROM', 'postmaster@mg.domain.tld');
define('MAIL_REPLY', 'replies@domain.tld');
function mailgun($to, $subject, $body, array $params = []) {
$data = array_merge([
'from' => MAIL_FROM,
'to' => $to,
'subject' => $subject,
'html' => $body,
'h:Reply-To' => MAIL_REPLY,
], $params);
$ch = curl_init(MAILGUN_URL . '/messages');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment