Mail

Laravel Mail

Support driver

Configuration config/mail.php

Mailgun Driver

composer require symfony/mailgun-mailer symfony/http-client

Mailgun Pricing

https://www.mailgun.com/pricing/

Plan Pricing Description
Trial $0/month 5,000 emails/month included
Foundation $35/month 50,000 emails/month included
Growth $80/month 100,000 emails/month included
Scale $90/month 100,000 emails/month included

Setting

.env & config/mail.php
// config/mail.php
return [
    'default' => env('MAIL_MAILER', 'smtp'),
];
# .env
MAIL_MAILER=mailgun
.env & config/services.php
// config/services.php
'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],
# .env
MAILGUN_DOMAIN=
MAILGUN_SECRET=

If you are not using the United States Mailgun region, you may define your region’s endpoint in the services configuration file:

// config/services.php
'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
    'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),
],
# .env
MAILGUN_DOMAIN=
MAILGUN_SECRET=
MAILGUN_ENDPOINT=

AWS SES Driver

composer require aws/aws-sdk-php

AWS Pricing

https://aws.amazon.com/ses/pricing/

Service type Volume per month Price Additional charges
Outbound email from EC2 0-62,000 emails $0 $0.12 for each GB of attachments you send
Outbound email from EC2 >62,000 emails $0.10/1000 emails $0.12 for each GB of attachments you send
Outbound email from non-EC2 Flat $0.10/1000 emails $0.12 for each GB of attachments you send
Inbound email 0-1,000 emails $0 $0.09 for every 1,000 incoming email chunks
Inbound email >1,000 emails $0.10/1000 emails $0.09 for every 1,000 incoming email chunks

Setting

.env & config/mail.php
// config/mail.php
return [
    'default' => env('MAIL_MAILER', 'smtp'),
];
MAIL_MAILER=ses

.env & config/services.php

// config/services.php
'ses' => [
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
# .env
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=

To utilize AWS temporary credentials via a session token, you may add a token key to your application’s SES configuration:

// config/services.php
'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'),
],
# .env
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
AWS_SESSION_TOKEN=

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:

// config/services.php
'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'],
        ],
    ],
],

Setting

Custom from Address

return new Envelope(
    from: new Address('jeffrey@example.com', 'Jeffrey Way'),
    replyTo: [
        new Address('taylor@example.com', 'Taylor Otwell'),
    ],
    subject: 'Order Shipped',
);

Using A Global from Address

From user

// config/mail.php
'from' => [
    'address' => 'example@example.com', 
    'name' => 'App Name'
],

Reply to user

// config/mail.php
'reply_to' => [
    'address' => 'example@example.com', 
    'name' => 'App Name'
],

Using A Global to Address

use Illuminate\Support\Facades\Mail;
 
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    if ($this->app->environment('local')) {
        Mail::alwaysTo('taylor@example.com');
    }
}

Failover Configuration

backup mail delivery configurations

// config/mail.php
'mailers' => [
    'failover' => [
        'transport' => 'failover',
        'mailers' => [
            'postmark',
            'mailgun',
            'sendmail',
        ],
    ],
],

Sending Mail

use Illuminate\Support\Facades\Mail;

Mail::to($request->user())->send(new OrderShipped($order));

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->send(new OrderShipped($order));

Sending Mail Via A Specific Mailer

Mail::mailer('ses')
    ->to($request->user())
    ->send(new OrderShipped($order));

Queueing Mail

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->queue(new OrderShipped($order));

Delayed Message Queueing

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->later(now()->addMinutes(10), new OrderShipped($order));

Pushing To Specific Queues

$OrderShippedMailable = (new OrderShipped($order))
                ->onConnection('sqs')
                ->onQueue('emails');


Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->queue($OrderShippedMailable);

Setting the default mail queue & connection

class OrderShipped extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public function __construct()
    {
        // The name of the connection the job should be sent to.
        $this->onConnection('redis')
        // The name of the queue the job should be sent to.
            ->onQueue('email');
    }
}

Localizing Mailables

Mail::to($request->user())->locale('zh_TW')->send(
    new OrderShipped($order)
);

User Preferred Locales

use Illuminate\Contracts\Translation\HasLocalePreference;
 
class User extends Model implements HasLocalePreference
{
    /**
     * Get the user's preferred locale.
     *
     * @return string
     */
    public function preferredLocale()
    {
        return $this->locale;
    }
}
Mail::to($request->user())->send(new OrderShipped($order));

Reference

Official

Mailgun

AWS SES

Postmark


Markdown Mail

Laravel Mail: Markdown Mail

Debug

Laravel Mail: Debug