說明:本文主要學習Laravel的Artisan Command、Task Scheduler和Mail相關知識。作一個簡單的小demo,用來定時發郵件。。走完整個流程最多隻需一小時。同時,會將開發過程當中的一些截圖和代碼黏上去,提升閱讀效率。php
開發環境是本機的MAMP集成軟件,PHP7.0,Laravel5.2.*。html
Laravel中Artisan Command內容能夠參看:服務 —— Artisan Console,Mail郵件服務內容能夠參看:服務 —— 郵件,以及Task-Scheduler任務定時器能夠參看:服務 —— 任務調度。laravel
新建一個artisan command:設計模式
php artisan make:console SendEmails --command=emails:send
並在AppConsoleCommandsSendEmails.php文件中添加代碼:app
class SendEmails extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'emails:send'; /** * The console command description. * * @var string */ protected $description = 'This is a demo about sending emails to myself'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $this->info('I am handsome'); $this->error('I am not ugly'); } }
寫上$description和handle()方法,$description變量用來顯示命令的說明,handle()用來處理命令,而後在AppConsoleCommandsKernel.php中註冊命令:composer
protected $commands = [ // Commands\Inspire::class, Commands\SendEmails::class, ];
好,這下能夠在終端輸入php artisan查看並執行命令了:學習
郵件服務API驅動須要安裝guzzlehttp/guzzle這個包,在項目根目錄下:ui
composer require guzzlehttp/guzzle
而後在.env文件中配置下郵件驅動和用戶名密碼:
this
而後修改下handle()方法:url
/** * Execute the console command. * * @return mixed */ public function handle() { // $this->info('I am handsome'); // $this->error('I am not ugly'); $user = [ 'email' => 'XXX@XXX.com',//一個有效的郵箱接收地址 'name' => 'liuxiang', ]; $status = Mail::send('emails.send', ['user'=>$user], function($msg) use ($user){ $msg->from('XXX@XXX.com', 'liuxiang email');//一個有效的郵箱發送地址 $msg->to($user['email'], $user['name'])->subject('This is a demo about sending emails to myself'); }); if(!$status){ $this->error('Fail to send email');exit; } $this->info('Success to send email');exit; }
發送的內容在視圖emails.send裏,新建resources/views/emails/send.blade.php文件:
<html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! --> <title>Bootstrap Template</title> <style> html,body{ width: 100%; height: 100%; } *{ margin: 0; border: 0; } .content{ text-align: center; margin: 50px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-xs-12 col-md-12"> <p class="content">This is a email by Laravel Artisan Command</p> </div> </div> </div> <script> </script> </body> </html>
一切準備OK,在項目根目錄運行郵件發送命令吧,而後會收到郵件發送成功打印:
而後接收的郵箱會收到郵件:
It is working!!!
每次手動發郵件畢竟不太爽啊,能夠利用系統的定時器crontab定時發送,Laravel裏有任務定時器能夠玩一玩。修改app/Console/Kernel.php文件:
/** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->command('inspire')->hourly(); //$schedule->command('emails:send')->everyFiveMinutes(); $schedule->command('emails:send')->everyMinutes(); }
在終端輸入crontab -e
添加一個cron條目:
* * * * * php /Applications/MAMP/htdocs/laravelemail/artisan schedule:run 1>> /dev/null 2>&1
而後程序每隔一分鐘發個郵件過來:
總結:本文主要以Laravel的Artisan Command、Mail和Task-Scheduler作一個好玩的小demo,來定時發發騷擾郵件,哈哈。還挺好玩的,能夠試一試。。嘛,下次想結合設計模式來聊聊Laravel,到時見。