LARAVEL 5 使用 GMAIL 寄信

http://laravel.com/

http://kejyun.github.io/Laravel-4-Docum ... roduction/
回覆文章
yehlu
Site Admin
文章: 3244
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

LARAVEL 5 使用 GMAIL 寄信

文章 yehlu »

https://kckct.wordpress.com/2016/03/19/ ... ing_gmail/

config/mail.php 參數
driver: Mail Driver,支援 “smtp", “mail", “sendmail", “mailgun", “mandrill", “ses", “log",這邊選擇 smtp

host: SMTP Host Address,使用 smtp.gmail.com
port: SMTP Host Port,使用 587

from: Global “From" Address,指定由應用程式寄出信件的寄件者及名字

encryption: E-Mail Encryption Protocol,使用 tls

username: SMTP Server Username,使用 Gmail address

password: SMTP Server Password,使用 Gmail password

sendmail: Sendmail System Path

pretend: 設定為true時,郵件訊息將會寫到應用程式的 log 檔案中,而不會發送給使用者,設定為false時,郵件才會真得寄出

* encryption 可選擇使用 tls 或 ssl,但使用port不同,tls port: 587, ssl port: 465



修改 config/mail.php 以及 .env 設定
// config/mail.php

代碼: 選擇全部

return [

 'driver' => env('MAIL_DRIVER', 'smtp'),
 'host' => env('MAIL_HOST', 'smtp.gmail.com'),
 'port' => env('MAIL_PORT', 587),
 'from' => ['address' => 'your@gmail.com', 'name' => 'yourname'],
 'encryption' => env('MAIL_ENCRYPTION', 'tls'),
 'username' => env('MAIL_USERNAME'),
 'password' => env('MAIL_PASSWORD'),
 'sendmail' => '/usr/sbin/sendmail -bs',
 'pretend' => false,

];
// .env

代碼: 選擇全部

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your@gmail.com
MAIL_PASSWORD=yourgmailpassword
MAIL_ENCRYPTION=tls
設定完成,信件應該還是無法寄出。



修改 Gmail 安全性設定
找到 [允許安全性較低的應用程式] 設定,開啟此網頁 https://www.google.com/settings/securit ... ps登入後設為「啟用」。



測試寄信
編輯 routes.php
// app/Http/routes.php
Route::get('sendmail', function() {
$data = ['name' => 'Test'];
Mail::send('email.welcome', $data, function($message) {
$message->to('your@email')->subject('This is test email');
});
return 'Your email has been sent successfully!';
});
新增 view,welcome.blade.php
// resources/views/email/welcome.blade.php
<p>Hi {{ $name }}!</p>
即可開始使用 Gmail 寄信!
回覆文章

回到「laravel」