我們在開發 Web 系統時常會需要做寄信的功能,但要自己架 Mail Server 又有點麻煩,這個時候我們可以用自己的 Gmail 帳號來寄信。

操作步驟如下:
1. 申請 gmail 應用程式密碼
    說明網頁:https://support.google.com/accounts/answer/185833?hl=zh-Hant&ctx=ch_DisplayUnlockCaptcha

2. 修改 .env 檔
MAIL_DRIVER=smtp
MAIL_HOST=ssl://smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME={帳號}@gmail.com
MAIL_PASSWORD={你的應用程式密碼}
MAIL_ENCRYPTION=null

3. PHP 程式
 

    public function mail()
    {
        $data = [];
        Mail::send('{信件內容的 template}', [], function($mail)
        {
            $mail->from('{寄件者}', '{你的應用程式名稱}');
            $mail->to("{收件者}", "Leo")->subject("{信件標題}");
        });
        return "OK!";
    }
arrow
arrow
    全站熱搜

    wbkuo 發表在 痞客邦 留言(0) 人氣()