Description: This article mainly describes the ardisan Command, Task schedcommand, and Mail related knowledge of Laravel. Make a simple demo to regularly send emails .. It takes up to one hour to complete the entire process. At the same time, the author will stick some code in the development process to improve reading efficiency. The development environment of the author is the MAMP integration software of the local machine, PHP7.0, Laravel5.2 .*.
For details about the Artisan Command in Laravel, see the service-Artisan Console. for details about the Mail service, see service-Mail and Task-Scheduler Task timer. for details, see service-Task scheduling.
Artisan Command
Create an artisan command:
php artisan make:console SendEmails --command=emails:send
Add the code to the AppConsoleCommandsSendEmails. php file:
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'); }}
Write the $ description and handle () methods, the $ description variable is used to display the command description, handle () is used to process the command, and then register the command in AppConsoleCommandsKernel. php:
protected $commands = [ // Commands\Inspire::class, Commands\SendEmails::class, ];
Okay. now you can enter php artisan on the terminal to view and execute the command:
Mail
The mail service API driver needs to install the guzzlehttp/guzzle package, under the Project root directory:
composer require guzzlehttp/guzzle
Then configure the email driver and user name and password in the. env file:
Then modify the handle () method:
/*** Execute the console command. ** @ return mixed */public function handle () {// $ this-> info ('I am Handsome '); // $ this-> error ('I am not ugg'); $ user = ['email' => 'XXX @ XXX.com ', // a valid email address 'name' => 'liuxiang ',]; $ status = Mail: send ('emails. send', ['user' => $ user], function ($ msg) use ($ user) {$ msg-> from ('XXX @ XXX.com ', 'liuxiang email '); // a valid email sending address $ msg-> to ($ user ['email'], $ user ['name'])- > Subject ('This is a demo about sending emails to myself-service ');}); if (! $ Status) {$ this-> error ('fail to send email '); exit;} $ this-> info ('success to send email'); exit ;}
In view emails. send, create the resources/views/emails/send. blade. php file:
Bootstrap Template
This is a email by Laravel Artisan Command
Script
Everything is ready. run the Mail sending command in the project root directory. then, the Mail is successfully sent and printed:
Then the received email will receive the following email:
It is working !!!
Task-Scheduler
After all, it is not refreshing to send emails manually each time. you can use the crontab timer of the system to send emails at regular intervals. there is a task timer in Laravel to play. Modify the app/Console/Kernel. php file:
/** * 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(); }
Enter crontab-e on the terminal to add a cron entry:
* * * * * php /Applications/MAMP/htdocs/laravelemail/artisan schedule:run 1>> /dev/null 2>&1
Then the program sends an email every minute:
Summary: This article mainly uses Laravel's Artisan Command, Mail, and Task-Scheduler as a fun demo to regularly send harassing emails. haha. It's quite fun. you can try it .. Well, I 'd like to talk about Laravel in a few days based on the design model.