Send mail applet based on Laravel Task-scheduler timing

Source: Internet
Author: User
Description: This article mainly studies Laravel's artisan Command, Task Scheduler and mail related knowledge. Do a simple demo to send emails on a regular basis. It takes up to an hour to walk through the process. At the same time, the author will be in the development process of some and code to stick up, improve reading efficiency. The author's development environment is the MAMP integrated software of this machine, php7.0,laravel5.2.*.

Laravel Artisan command content can be see: Service--artisan Console, mail mail Service content can be see: service-mail, and Task-scheduler task timer can see: Service-Task scheduling.

Artisan Command

Create a new Artisan command:

PHP Artisan make:console sendemails--command=emails:send

and add code in 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 was 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, $description variables to display a description of the command, handle () to process the command, and then register the command in appconsolecommandskernel.php:

protected $commands = [        //Commands\inspire::class,        Commands\sendemails::class,    ];

OK, this can be done by entering PHP artisan at the terminal to view and execute the command:

Mail

The Mail Service API driver needs to install Guzzlehttp/guzzle This package, under the project root directory:

Composer require Guzzlehttp/guzzle

Then configure the mail-driven and user-name passwords in the. env file:

Then modify the next handle () method:

/**     * 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 ',//a valid mailbox receive address            ' name ' = '  Liuxiang ',        ];        $status = Mail::send (' emails.send ', [' user ' = ' $user], function ($msg) use ($user) {            $msg->from (' XXX@XXX.com ' , ' Liuxiang email ');//A valid mailbox send address            $msg->to ($user [' email '], $user [' name '])->subject (' This was a demo about Sending emails to myself ');        });        if (! $status) {            $this->error (' Fail to send email '); exit;        }        $this->info (' Success to send email '); exit;    }

Send content in view emails.send, create a new resources/views/emails/send.blade.php file:

            
 
          
 
          
 
          
 
          Bootstrap Template                                                                        

This is a mail by Laravel Artisan Command

All ready OK, run the Mail Send command in the project root directory, and then receive the message sent successfully to print:

Then receive the email message:

IT is working!!!

Task-scheduler

Each time the manual email is not very good ah, you can use the system timer crontab timed send, Laravel have a task timer can play a play. To 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 ();    }

Add a cron entry to the terminal input CRONTAB-E:

* * * * * * Php/applications/mamp/htdocs/laravelemail/artisan schedule:run 1>>/dev/null 2>&1

Then the program sends a message every other minute:

Summary: This article mainly to Laravel artisan Command, mail and Task-scheduler do a fun little demo, to regularly send a harassment mail, haha. It's kind of fun, you can try it. Well, after a few days want to combine design mode to talk about Laravel, until then.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.