Send mail using Laravel's own mail class

Source: Internet
Author: User

  Since the project often uses the ability to detect alerts when it is running, I used a shell script to send messages. The advantage of shell script is that it can directly handle the server instruction, but its disadvantage is that the ability to process the data is too low, so many data of the database should be written to the file first, then each line read the file and split to take out the data you want, relatively trouble too much. So I am ready to directly use the Laravel framework package mail directly sent Mail, and the PHP language relative to the shell processing ability is simply too strong, so decisively abandoned the pit.

First create a new Laravel project, find the mail related value in its. env file, and configure the mail sending account (depending on the driver and host, such as 163 or other).

Secondly The config directory under the mail.php configuration of the address and name, I was not equipped with this for half a day to send out, this is similar to the configuration item Lavarel if all is written in. Env It's not only used in the. Env after you can use the mail after a click, it is more convenient, this place to a Poor reviews. mail.php add these two places:

Finally, I quote the Mail class in my controller.

Then, in the call to send the message method send ():

//send e-mail$flag=Mail:: Send (' emails.test ', [' data ' = =$data],function($message){      $to= ' [email protected] ';      $message->to ($to)->subject (' Test mail '); }); if($flag) {      Echo' Mail sent successfully '; } Else {      Echo' Mail send failed '; }

The first parameter in the Send () method is any page under Resource/views (This page is the contents of the mailbox to display), the second parameter is passed back from the background to the page parameters, $to for the message recipients, subject the subject of the message, This sends out an e-mail, which is much more convenient than before.

In the end, I put a crontab on the Linux schedule to my Project Curl request can be scheduled to run tasks, long ago should abandon the shell.

Send mail using Laravel's own mail class

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.