Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Last active January 5, 2020 08:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vaibhavpandeyvpz/214625cc2aeda6537ec7441a92ea2126 to your computer and use it in GitHub Desktop.
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