Introduction
Sending email doesn't have to be complicated. Laravel provides a clean, simple email API powered by the popular Symfony Mailer component. Laravel and Symfony Mailer provide drivers for sending email via SMTP, Mailgun, Postmark, Resend, 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 your application's config/mail.php configuration file. Each mailer configured within this file may have its own unique configuration 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 emails while using Amazon SES to send bulk emails.
Within your mail configuration file, you will find a mailers configuration array. This array contains a sample configuration entry for each of the major mail drivers / transports supported by Laravel, while the default configuration value determines which mailer will be used by default when your application needs to send an email message.
Driver / Transport Prerequisites
The API based drivers such as Mailgun, Postmark, Resend, and MailerSend are often simpler and faster than sending mail via SMTP servers. Whenever possible, we recommend that you use one of these drivers.
Mailgun Driver
To use the Mailgun driver, install Symfony's Mailgun Mailer transport via Composer:
composer require symfony/mailgun-mailer symfony/http-client
Next, you will need to make two changes in your application's config/mail.php configuration file. First, set your default mailer to mailgun:
'default' => env('MAIL_MAILER', 'mailgun'),
Second, add the following configuration array to your array of mailers:
'mailgun' => [
'transport' => 'mailgun',
// 'client' => [
// 'timeout' => 5,
// ],
],
After configuring your application's default mailer, add the following options to your config/services.php configuration file:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
If you are not using the United States Mailgun region, you may define your region's endpoint in the services configuration file:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),
'scheme' => 'https',
],
Postmark Driver
To use the Postmark driver, install Symfony's Postmark Mailer transport via Composer:
composer require symfony/postmark-mailer symfony/http-client
Next, set the default option in your application's config/mail.php configuration file to postmark. After configuring your application's default mailer, ensure that your config/services.php configuration file contains the following options:
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],
If you would like to specify the Postmark message stream that should be used by a given mailer, you may add the message_stream_id configuration option to the mailer's configuration array. This configuration array can be found in your application's config/mail.php configuration file:
'postmark' => [
'transport' => 'postmark',
'message_stream_id' => env('POSTMARK_MESSAGE_STREAM_ID'),
// 'client' => [
// 'timeout' => 5,
// ],
],
This way you are also able to set up multiple Postmark mailers with different message streams.
Resend Driver
To use the Resend driver, install Resend's PHP SDK via Composer:
composer require resend/resend-php
Next, set the default option in your application's config/mail.php configuration file to resend. After configuring your application's default mailer, ensure that your config/services.php configuration file contains the following options:
'resend' => [
'key' => env('RESEND_KEY'),
],
SES Driver
To use the Amazon SES driver you must first install the Amazon AWS SDK for PHP. You may install this library via the Composer package manager:
composer require aws/aws-sdk-php
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' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
To utilize AWS temporary credentials via a session token, you may add a token key to your application's SES configuration:
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'token' => env('AWS_SESSION_TOKEN'),
],
To interact with SES's subscription management features, you may return the X-Ses-List-Management-Options header in the array returned by the headers method of a mail message:
/**
* Get the message headers.
*/
public function headers(): Headers
{
return new Headers(
text: [
'X-Ses-List-Management-Options' => 'contactListName=MyContactList;topicName=MyTopic',
],
);
}
If you would like to define additional options that Laravel should pass to the AWS SDK's SendEmail method when sending an email, you may define an options array within your ses configuration:
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'options' => [
'ConfigurationSetName' => 'MyConfigurationSet',
'EmailTags' => [
['Name' => 'foo', 'Value' => 'bar'],
],
],
],
MailerSend Driver
MailerSend, a transactional email and SMS service, maintains their own API based mail driver for Laravel. The package containing the driver may be installed via the Composer package manager:
composer require mailersend/laravel-driver