Introduction
Laravel provides a clean, simple API over the popular SwiftMailer library with drivers for SMTP, Mailgun, Postmark, Amazon SES, and sendmail
, allowing you to quickly get started sending mail through a local or cloud based service of your choice.
Configuration
Laravel's email services may be configured via the mail
configuration file. Each mailer configured within this file may have its own options and even its own unique "transport", allowing your application to use different email services to send certain email messages. For example, your application might use Postmark to send transactional mail while using Amazon SES to send bulk mail.
Driver Prerequisites
The API based drivers such as Mailgun and Postmark are often simpler and faster than SMTP servers. If possible, you should use one of these drivers. All of the API drivers require the Guzzle HTTP library, which may be installed via the Composer package manager:
composer require guzzlehttp/guzzle
Mailgun Driver
To use the Mailgun driver, first install Guzzle, then set the default
option in your config/mail.php
configuration file to mailgun
. Next, verify that your config/services.php
configuration file contains the following options:
'mailgun' => [
'domain' => 'your-mailgun-domain',
'secret' => 'your-mailgun-key',
],
If you are not using the "US" Mailgun region, you may define your region's endpoint in the services
configuration file:
'mailgun' => [
'domain' => 'your-mailgun-domain',
'secret' => 'your-mailgun-key',
'endpoint' => 'api.eu.mailgun.net',
],
Postmark Driver
To use the Postmark driver, install Postmark's SwiftMailer transport via Composer:
composer require wildbit/swiftmailer-postmark
Next, install Guzzle and set the default
option in your config/mail.php
configuration file to postmark
. Finally, verify that your config/services.php
configuration file contains the following options:
'postmark' => [
'token' => 'your-postmark-token',
],
SES Driver
To use the Amazon SES driver you must first install the Amazon AWS SDK for PHP. You may install this library by adding the following line to your composer.json
file's require
section and running the composer update
command:
"aws/aws-sdk-php": "~3.0"
Next, set the default
option in your config/mail.php
configuration file to ses
and verify that your config/services.php
configuration file contains the following options:
'ses' => [
'key' => 'your-ses-key',
'secret' => 'your-ses-secret',
'region' => 'ses-region', // e.g. us-east-1
],
If you need to include additional options when executing the SES SendRawEmail
request, you may define an options
array within your ses
configuration:
'ses' => [
'key' => 'your-ses-key',
'secret' => 'your-ses-secret',
'region' => 'ses-region', // e.g. us-east-1
'options' => [
'ConfigurationSetName' => 'MyConfigurationSet',
'Tags' => [
[
'Name' => 'foo',
'Value' => 'bar',
],
],
],
],
Generating Mailables
In Laravel, each type of email sent by your application is represented as a "mailable" class. These classes are stored in the app/Mail
directory. Don't worry if you don't see this directory in your application, since it will be generated for you when you create your first mailable class using the make:mail
command:
php artisan make:mail OrderShipped