使用Laravel Mail 阿里雲企業郵箱發送郵件

很多郵箱使用SMTP協議發送郵件,都需要開啓SMTP協議,獲取授權碼。

我用的是阿里雲企業郵箱,不需要授權碼,只需要開啓SMTP協議後,使用該賬號的密碼就等於其他郵箱的授權碼。

在阿里雲企業郵箱管理後臺分發一個賬號

新增賬號,主要用於發送郵件

郵箱賬號列表

這樣郵箱服務器這方面已經做好了,接下來有一個在網上找到的可以使用的腳本來測試一下是否可以發送郵件

 

需要準備一個接收郵件地址。

代碼如下:

class Mailer
{
    private $host;
    private $port = 25;
    private $user;
    private $pass;
    private $debug = false;
    private $sock;

    public function __construct($host,$port,$user,$pass,$debug = false)
    {
        $this->host = $host;
        $this->port = $port;
        $this->user = base64_encode($user); //用戶名密碼一定要使用base64編碼纔行
        $this->pass = base64_encode($pass);
        $this->debug = $debug;
        //socket連接
        $this->sock = fsockopen($this->host,$this->port);
        if(!$this->sock){
            exit('出錯啦');
        }
        //讀取smtp服務返回給我們的數據
        $response = fgets($this->sock);
        $this->debug($response);
        //如果響應中有220返回碼,說明我們連接成功了
        if(strstr($response,'220') === false){
            exit('出錯啦');
        }
    }

    //發送SMTP指令,不同指令的返回碼可能不同
    public function execCommand($cmd,$return_code)
    {
        fwrite($this->sock,$cmd);

        $response = fgets($this->sock);
        //輸出調試信息
        $this->debug('cmd:'.$cmd .';response:'.$response);
        if(strstr($response,$return_code) === false){
            return false;
        }
        return true;
    }

    public function sendMail($from,$to,$subject,$body)
    {
        //detail是郵件的內容,一定要嚴格按照下面的格式,這是協議規定的
        $detail = 'From:'.$from."\r\n";
        $detail .= 'To:'.$to."\r\n";
        $detail .= 'Subject:'.$subject."\r\n";
        $detail .= 'Content-Type: Text/html;'."\r\n";
        $detail .= 'charset=gb2312'."\r\n\r\n";
        $detail .= $body;
        $this->execCommand("HELO ".$this->host."\r\n",250);
        $this->execCommand("AUTH LOGIN\r\n",334);
        $this->execCommand($this->user."\r\n",334);
        $this->execCommand($this->pass."\r\n",235);
        $this->execCommand("MAIL FROM:<".$from.">\r\n",250);
        $this->execCommand("RCPT TO:<".$to.">\r\n",250);
        $this->execCommand("DATA\r\n",354);
        $this->execCommand($detail."\r\n.\r\n",250);
        $this->execCommand("QUIT\r\n",221);
    }

    public function debug($message)
    {
        if($this->debug){
            echo '<p>Debug:'.$message . PHP_EOL .'</p>';
        }
    }

    public function __destruct()
    {
        fclose($this->sock);
    }

}

//調用示例
$port = 25;
$user = '[email protected]'; //請替換成你自己的smtp用戶名
$pass = 'xxxxxx'; //請替換成你自己的smtp密碼
$host = 'smtp.xxx.com';//smtp+你的域名
$from = '[email protected]';
$to = '[email protected]';//收件郵箱地址
$body = 'hello world';
$subjet = '我是測試';//主題
$mailer = new Mailer($host,$port,$user,$pass,true);
$mailer->sendMail($from,$to,$subjet,$body);die;

瀏覽器訪問:

顯示發送成功,查看一下郵箱

收件箱裏面沒有,在垃圾箱裏,有時候郵件會在垃圾箱裏被發現,應該是匹配垃圾的規則所導致的,可以更改一下主題試試

測試成功,就可以集成到laravel中了。

更改laravel 配置文件中的mail.php就可以了,不需要加載其他配置

laravel 配置文件路徑

請參考一下配置內容進行修改

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
    |            "sparkpost", "log", "array"
    |
    */

    'driver' => 'smtp',#改成smtp

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Address
    |--------------------------------------------------------------------------
    |
    | Here you may provide the host address of the SMTP server used by your
    | applications. A default option is provided that is compatible with
    | the Mailgun mail service which will provide reliable deliveries.
    |
    */

    'host' => 'smtp.xxx.com',#改成你的host

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */

    'port' => 25,#端口我這裏使用的是25 沒有使用ssl加密

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */

    'from' => [
        'address' => '[email protected]',//發送地址
        'name' => 'service',//郵件發送人名稱
    ],

    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */

    'encryption' => '',#去掉加密

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */

    'username' => '[email protected]',#發送人郵箱賬號

    'password' => 'xxxxxx',#密碼

    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -bs',

    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

配置路由

Route::match(['POST', 'GET'], '/admin/login', 'Admin\AdminLoginController@login');

控制器

Trait

瀏覽器訪問

去郵箱查看:

這樣就完成了,在封裝一下參數,改活參數,就可以使用了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章