Yii 2.0 Use MailGun API Send Email
{ Send Email in Yii 2.0 Framwork By MailGun API }
{ Step 1. Send Function }
<?php
namespace eeTools;
class eeMail{
/**
* send email by mailgun api v3
* @return string
*/
public static function sendMailGunAPI($to, $subject, $body){
$config = array();
$config['api_key'] = 'key-8da18724c8aae3******************'; //key from MailGun account
//api url from MailGun Account,
//the "yiilib.com" part will change
$config['api_url'] = 'https://api.mailgun.net/v3/yiilib.com/messages';
$message = array();
$message['from'] = 'yiilib.com@gmail.com';
$message['to'] = $to;
$message['subject'] = $subject;
$message['html'] = $body;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['api_url']);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "api:{$config['api_key']}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$message);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
{ Step 2. Send }
<?php
use eeTools/eeMail;
....
eeMail::sendMailGunAPI($to, $subject, $body);
{ Yii 2.0 Framework Normal way send Email }
Link : MAILING IN YII2 Demo Code
Leave Comment