Mail

Configuration

Note: By default, the illuminate/mail package is not included with Lumen, so you will need to add the illuminate/mail dependency in your composer.json file.

Lumen utilizes Laravel’s mail libraries which provides a clean, simple API over the popular SwiftMailer library.

The MAIL_* options in your .env file are used to configure your mail settings. By default, a sample SMTP configuration is provided. However, you may use any SMTP server you wish.

If you wish to use the PHP mail function to send mail, you may change the MAIL_DRIVER to mail in the configuration file. A sendmail driver is also available.

API Drivers

Lumen also includes drivers for the Mailgun and Mandrill HTTP APIs. These APIs are often simpler and quicker than the SMTP servers. Both of these drivers require that the Guzzle 4 HTTP library be installed into your application. You can add Guzzle 4 to your project by adding the following line to your composer.json file:

  1. "guzzlehttp/guzzle": "~4.0"

Mailgun Driver

To use the Mailgun driver, set the MAIL_DRIVER option to mailgun. Next, create an config/services.php configuration file if one does not already exist for your project. Verify that it contains the following options:

  1. 'mailgun' => [
  2. 'domain' => 'your-mailgun-domain',
  3. 'secret' => 'your-mailgun-key',
  4. ],

Mandrill Driver

To use the Mandrill driver, set the MAIL_DRIVER option to mandrill. Next, create an config/services.php configuration file if one does not already exist for your project. Verify that it contains the following options:

  1. 'mandrill' => [
  2. 'secret' => 'your-mandrill-key',
  3. ],

Log Driver

If the MAIL_DRIVER option of your configuration file is set to log, all e-mails will be written to your log files, and will not actually be sent to any of the recipients. This is primarily useful for quick, local debugging and content verification.

Basic Usage

Note: If you intend to use the Mail facade, be sure to uncomment the $app->withFacades() call in your bootstrap/app.php file.

The Mail::send method may be used to send an e-mail message:

  1. Mail::send('emails.welcome', ['key' => 'value'], function($message) {
  2. $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
  3. });

The first argument passed to the send method is the name of the view that should be used as the e-mail body. The second is the data to be passed to the view, often as an associative array where the data items are available to the view by $key. The third is a Closure allowing you to specify various options on the e-mail message.

Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, it is best to avoid passing a message variable in your view payload.

You may also specify a plain text view to use in addition to an HTML view:

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

Or, you may specify only one type of view using the html or text keys:

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

You may specify other options on the e-mail message such as any carbon copies or attachments as well:

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

When attaching files to a message, you may also specify a MIME type and / or a display name:

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

If you just need to e-mail a simple string instead of an entire view, use the raw method:

  1. Mail::raw('Text to e-mail', function($message) {
  2. $message->from('us@example.com', 'Laravel');
  3. $message->to('foo@example.com')->cc('bar@example.com');
  4. });

Note: The message instance passed to a Mail::send Closure extends the SwiftMailer message class, allowing you to call any method on that class to build your e-mail messages.

Embedding Inline Attachments

Embedding inline images into your e-mails is typically cumbersome; however, Laravel provides a convenient way to attach images to your e-mails and retrieving the appropriate CID.

Embedding An Image In An E-Mail View

  1. <body>
  2. Here is an image:
  3. <img src="<?php echo $message->embed($pathToFile); ?>">
  4. </body>

Embedding Raw Data In An E-Mail View

  1. <body>
  2. Here is an image from raw data:
  3. <img src="<?php echo $message->embedData($data, $name); ?>">
  4. </body>

Note that the $message variable is always passed to e-mail views by the Mail facade.

Queueing Mail

Queueing A Mail Message

Note: Before queueing mail messages, you will need to add the jeremeamia/superclosure (~2.0) dependency to your composer.json file.

Since sending e-mail messages can drastically lengthen the response time of your application, many developers choose to queue e-mail messages for background sending. Lumen and Laravel makes this easy using its built-in unified queue API. To queue a mail message, simply use the queue method on the Mail facade:

  1. Mail::queue('emails.welcome', $data, function($message) {
  2. $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
  3. });

You may also specify the number of seconds you wish to delay the sending of the mail message using the later method:

  1. Mail::later(5, 'emails.welcome', $data, function($message) {
  2. $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
  3. });

If you wish to specify a specific queue or “tube” on which to push the message, you may do so using the queueOn and laterOn methods:

  1. Mail::queueOn('queue-name', 'emails.welcome', $data, function($message) {
  2. $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
  3. });