Last active
January 5, 2020 08:47
-
-
Save vaibhavpandeyvpz/214625cc2aeda6537ec7441a92ea2126 to your computer and use it in GitHub Desktop.
Send email using PHP/cURL + Mailgun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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', '[email protected]'); | |
| define('MAIL_REPLY', '[email protected]'); | |
| 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