本文實例講述了Laravel框架實現的使用smtp發送郵件功能。分享給大家供大家參考,具體如下:
1、.env文件中配置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.郵箱后綴
MAIL_PORT=郵件服務器發送端口
MAIL_USERNAME=發送方郵件地址
MAIL_PASSWORD=發送方郵箱生成的第三方登陸碼
MAIL_FROM_ADDRESS=發送郵箱地址
MAIL_FROM_NAME=發送方名稱
2、config目錄下mail.php文件配置
可以不配置,因為會被.env文件覆蓋掉。(只有在.env中沒有的時候才會去該文件中取值)
3、app/console/commonds/sendMail.php
namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Mail; class SendMailCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'demo:SendMail'; /** * The console command description. * * @var string */ protected $description = '測試腳本SendMail'; /** * constructor */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $content = '這是一封的測試郵件.'; $toMail = '目標郵箱'; Mail::raw($content, function ($message) use ($toMail) { $message->subject('[ 測試 ] 測試郵件SendMail - ' .date('Y-m-d H:i:s')); $message->to($toMail); }); } }
4、測試
cmd切換到項目根目錄下,執行
php artisan demo:SendMail
希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。