[Laravel5.2 documentation] service-email

Source: Internet
Author: User
Tags valid email address all mail
[Laravel5.2 documentation] service-email 1. Introduction

Laravel provides a clean and refreshing mail API based on the SwiftMailer Library. Laravel provides the mail functions for SMTP, Mailgun, Mandrill, Amazon SES, PHP, and sendmail, allowing you to quickly send emails through local or cloud services.

E-mail-driven prerequisites

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

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

Mailgun driver

To use the Mailgun driver (the first 10000 emails in Mailgun are free of charge and will be charged later), first install Guzzle and set the driver option to mailgun in config/mail. php. Next, verify that the configuration file config/services. php contains the following options:

'mailgun' => [    'domain' => 'your-mailgun-domain',    'secret' => 'your-mailgun-key',],

Mandrill driver

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

'mandrill' => [    'secret' => 'your-mandrill-key',],

SES Driver

To use the Amazon SES Driver (charged) to install the php sdk of Amazon AWS, 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 config/mail. php to ses. 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 an email

Laravel allows you to store mail information in the view. for example, to organize your email, you can create an emails directory under the resources/views directory.

To send a message, use the send method on the Mail facade. The send method receives three parameters. The first parameter is the view name that contains the Mail information; the second parameter is the array data that you want to pass to the view; the third parameter is the closure callback of the receiving message instance, which allows you to customize the recipient, subject, and other information of the email:

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

As we passed an array containing the user key in the above example, we can display the user name in the email as follows:

 name; ?>

Note: The $ message variable is always passed to the Mail view and can be embedded with attachments. Therefore, you should avoid passing in message variables in the view load.

Construct a message

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. You can use this closure to specify other attributes of a message, such as CC and group sending:

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

The following are available methods for interviewing $ message builder instances:

$ 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 a file from the $ data string... $ message-> attachData ($ data, $ name, array $ options = []); // gets the underlying SwiftMailer message instance... $ message-> getSwiftMessage ();

Note: The message instance passed to the Mail: send closure inherits from the SwiftMailer message class, which allows you to call any method on this class to build your own email message.

Plain text mail

By default, the view passed to the send method contains HTML. However, by passing an array as the first parameter to the send method, you can specify to send a plain text view other than the HTML view:

Mail::send(['html.view', 'text.view'], $data, $callback);

Alternatively, if you only need to send plain text emails, you can specify the text key in the array:

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

Native string Mail

If you want to directly send a native string mail, you can use the raw method:

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

2.1 Attachment

To add attachments to emails, use the attach method on the $ message object passed to the closure. This method takes the absolute path of the received 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 an array as the second parameter to the attach method:

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

2.2 Inline attachment

Embed an image in the mail View

Nesting inline images into emails is often cumbersome. However, Laravel provides a convenient way to attach images to emails and obtain the corresponding CID. inline images must be embedded, use the embed method on the $ message variable in the mail view. Remember, Laravel automatically enters the $ message variable in all mail views to make it valid:

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

Embed native data in the mail View

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

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

2.3 mail queue

Message Queue

Sending Mail messages may greatly extend the application response time. many developers choose to put the emails in the queue and then execute them in the background. Laravel can use the built-in unified queue API. To put the Mail message in the queue, use the queue method on the Mail facade:

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.

Delayed Message Queue

If you want to delay the sending of emails that have been placed in the queue, you can use the later method. You only need to pass the number of seconds you want to delay sending as the first parameter to this method:

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

Push to specified queue

If you want to push mail messages to a specified queue, you can use the queueOn and lateon methods:

Mail::queueOn('queue-name', 'emails.welcome', $data, function ($message) {    //});Mail::laterOn('queue-name', 5, 'emails.welcome', $data, function ($message) {    //});
3. email & Local Development

When developing an application for sending emails, you may not want to actually send emails to a valid email address, but just want to perform a test. Laravel provides several methods to disable the actual sending of emails.

Log-driven

One solution is to use the log Mail driver during local development. This driver writes all mail information to the log file for viewing.

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.