2016-06-29 11:25:19 0 评论 Yii 2.0 Boy.Lee

Yii 2.0 使用MailGun API 发送邮件

{ Send Email in Yii 2.0 Framwork By MailGun API }

{ Step 1. 发送函数 }

<?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 可以在 MailGun 账户中得到
        
        //api url 可以在 MailGun 账户中得到, 
        //"yiilib.com"部分将会改变
        $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. 发送 }

<?php
use eeTools/eeMail;

....

eeMail::sendMailGunAPI($to, $subject, $body);

 

{ Yii 2.0 框架传统方法发送邮件 }

链接 : YII2中发送邮件示例