[Laravel 5.2 Document] service--mail

Source: Internet
Author: User
Tags all mail

1. Introduction

Laravel provides a clean and refreshing email API based on the Swiftmailer library. Laravel provides drivers for SMTP, Mailgun, Mandrill, Amazon SES, PHP mail functions, and sendmail, allowing you to quickly send mail over local or cloud services.

Mail-driven Readiness knowledge

Driver-based APIs such as Mailgun and Mandrill are generally simpler and faster than SMTP servers. All API drivers require the app to have the Guzzle HTTP library installed. You can install guzzle to the project by adding the following line to the Composer.json file:

"Guzzlehttp/guzzle": "~5.3|~6.0"

Mailgun Drive

To use the Mailgun driver (Mailgun the first 10000 messages for free, follow-up charges), first install the guzzle, and then set the driver option in the configuration file config/mail.php to Mailgun. Next, verify that the configuration file config/services.php contains the following options:

' Mailgun ' = [    ' domain ' = ' your-mailgun-domain ',    ' secret ' = ' Your-mailgun-key ',],

Mandrill Drive

To use the Mandrill driver (Mandrill does not support user registration in China, Sweat!) ), first install guzzle, and then set the driver option value to mandrill in configuration file config/mail.php. Next, verify that the configuration file config/services.php contains the following options:

' Mandrill ' = [    ' secret ' = ' Your-mandrill-key ',],

SES drive

To install the Amazon AWS PHP SDK using Amazon SES drivers (Charges), you can install the library by adding the following line to the Require section of the Composer.json file:

"aws/aws-sdk-php": "~3.0"

Next, set the driver option in configuration file config/mail.php to SES. Then, verify that the configuration file config/services.php contains the following options:

' ses ' = [    ' key ' = ' your-ses-key ',    ' secret ' = ' Your-ses-secret ',    ' region ' = ' ses-region ',  //e.g. us-east-1],

2. Send mail

Laravel allows you to store message information in a view, for example, to organize your e-mail, you can create a emails directory under the Resources/views directory.

To send a message, use the Send method on the Mail façade. The Send method receives three parameters. The first parameter is the name of the view that contains the message information, the second parameter is the array data that you want to pass to the view, and the third parameter is the closure callback that receives the message instance--allows you to customize the recipients, topics, and other information about the message:

 
  $user], function ($m) use ($user) {            $m->from (' hello@app.com ', ' Your application '); $m->to ($user->email, $ User->name)->subject (' Your reminder! ');}}    

Since we passed an array with the user key in the previous example, we can display the user name in the message as follows:

 
  Name;?>

Note: $message variables are always passed to the message view and allow embedding of attachments, so you should avoid incoming message variables in the view payload.

Constructing messages

As discussed earlier, the third parameter passed to the Send method is a closure that allows you to specify multiple options for the mail message itself. Use this closure to specify other properties of the message, such as CC, Mass, and so on:

Mail::send (' Emails.welcome ', $data, function ($message) {    $message->from (' us@example.com ', ' Laravel ');    $message->to (' foo@example.com ')->cc (' bar@example.com ');});

Try $message the available methods on the Message Builder instance below:

$message->from ($address, $name = null), $message->sender ($address, $name = null), $message->to ($address, $name = null), $message->cc ($address, $name = null), $message->BCC ($address, $name = null); $message->replyto ($ address, $name = null), $message->subject ($subject); $message->priority ($level); $message->attach ($ PathToFile, array $options = []);//Append file from $data string ... $message->attachdata ($data, $name, array $options = []);//Get underlying SWI Ftmailer Message Instance ... $message->getswiftmessage ();

Note: The message instance passed to the Mail::send closure inherits from the Swiftmailer message class, which allows you to invoke any method on the class to build your own e-mail message.

Plain Text Mail

By default, the view passed to the Send method assumes that it contains HTML, however, by passing an array as the first parameter to the Send method, you can specify to send a plain text view in addition to the HTML view:

Mail::send ([' Html.view ', ' Text.view '], $data, $callback);

Or, if you only need to send plain text messages, you can specify that you want to use the text key in the array:

Mail::send ([' Text ' = ' view '], $data, $callback);

Raw String Message

If you want to send native string messages directly you can use the Raw method:

Mail::raw (' Text to e-mail ', function ($message) {    //});

2.1 Accessories

To add attachments to a message, use the Attach method on the $message object that is passed to the closure. The method receives the absolute path of the file as the first parameter:

Mail::send (' Emails.welcome ', $data, function ($message) {    //    $message->attach ($pathToFile);});

When adding a file to a message, you can also specify the file display name and MIME type by passing the array as the second parameter to the Attach method:

$message->attach ($pathToFile, [' as ' + = $display, ' mime ' = ' $mime]);

2.2 Inline Accessories

Embed a picture in the mail view

Nested inline pictures are often cumbersome in the mail, however, Laravel provides a convenient way to attach images to a message and get the appropriate CID, to embed inline images, and to use the Embed method on the $message variable in the Mail view. Remember, Laravel automatically passes in $message variable in all mail views to make it work:

    Here's an image:    embed ($pathToFile);?> ">

Embed native data in the Mail view

If you want to embed the native data string in the mail message, you can use the Embeddata method on the $message variable:

    Here is a image from raw data:    embeddata ($data, $name);?> ">

2.3 Message queues

Mail Message Queuing

Sending mail messages can significantly extend the response time of your app, and many developers choose to send mail to the queue and back in the background, and Laravel can be implemented using the built-in unified queuing API. To put mail messages in a queue, use the queue method on the mail façade:

Mail::queue (' Emails.welcome ', $data, function ($message) {    //});

This method automatically pushes the mail task to the queue for sending in the background. Of course, you need to configure the queue before using this feature.

Delay Message Queuing

If you want to delay the sending of messages that have already been placed in the queue, you can use the later method. Just pass the number of seconds you want to delay sending as the first parameter to the method:

Mail::later (5, ' emails.welcome ', $data, function ($message) {    //});

Push into the specified queue

If you want to push mail messages to the specified queue, you can use the Queueon and Lateron methods:

Mail::queueon (' Queue-name ', ' emails.welcome ', $data, function ($message) {    //}); Mail::lateron (' Queue-name ', 5, ' Emails.welcome ', $data, function ($message) {    //});

3. Mail & Local Development

When developing an app that sends mail, you might not want to really send a message to a valid e-mail address, but just want to do a test. Laravel provides several ways to "prohibit" the actual delivery of a message.

Log-driven

One solution is to use log mail driver when developing locally. The driver writes all message information to the log file for viewing, and wants to learn more about the application configuration information for each environment and view the configuration document.

General configuration

Another solution provided by Laravel is to set generic recipients for all messages sent by the framework, so that messages generated by all apps will be sent to the specified address instead of the address specified by the actual sending message. This can be done by setting the To option in the configuration file config/mail.php:

' to ' + = [    ' address ' = ' dev@domain.com ',    ' name ' = ' dev Example '],

Mailtrap

Finally, you can use the Mailtrap service and the SMTP driver to send mail messages to the "virtual" mailbox, which allows you to view the final message in the MAILTRAP Message Viewer.

4. Events

Laravel triggers an event before the message is sent, remembering that the event is triggered when the message is sent, instead of being pushed to the queue, you can register the event listener in Eventserviceprovider:

/** * The event listener mappings for the application. * * @var array */protected $listen = [    ' illuminate\mail\events\messagesending ' = = [        ' app\listeners\ Logsentmessage ',    ],];
  • 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.