本文實(shí)例講述了Laravel框架實(shí)現(xiàn)的使用smtp發(fā)送郵件功能。分享給大家供大家參考,具體如下:
1、.env文件中配置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.郵箱后綴
MAIL_PORT=郵件服務(wù)器發(fā)送端口
MAIL_USERNAME=發(fā)送方郵件地址
MAIL_PASSWORD=發(fā)送方郵箱生成的第三方登陸碼
MAIL_FROM_ADDRESS=發(fā)送郵箱地址
MAIL_FROM_NAME=發(fā)送方名稱
2、config目錄下mail.php文件配置
可以不配置,因?yàn)闀?huì)被.env文件覆蓋掉。(只有在.env中沒有的時(shí)候才會(huì)去該文件中取值)
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 = '測(cè)試腳本SendMail'; /** * constructor */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $content = '這是一封的測(cè)試郵件.'; $toMail = '目標(biāo)郵箱'; Mail::raw($content, function ($message) use ($toMail) { $message->subject('[ 測(cè)試 ] 測(cè)試郵件SendMail - ' .date('Y-m-d H:i:s')); $message->to($toMail); }); } }
4、測(cè)試
cmd切換到項(xiàng)目根目錄下,執(zhí)行
php artisan demo:SendMail
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。