Dorokhov.codes

10. Working with mail

Drivers for sending email:

  • SMTP
  • Mailgun
  • Postmark
  • Resend
  • Amazon SES
  • sendmail

Configuration

Laravel’s email services may be configured via your application’s config/mail.php configuration file.

These settings may be specified in .env:

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

Driver / Transport

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:

composer require symfony/mailgun-mailer symfony/http-client
  • symfony/http-client: general package, Symfony Mailer bridge.
  • symfony/mailgun-mailer: Mailgun mailer.

Emails

Each email template should have a separate class extended from Mailable.

How to create a class using Artisan:

php artisan make:mail OrderConfirmed

Sending Mail

Example:

Mail::to('andrew@dorokhov.dev')->send(new MailableExample());

Pre-built templates with MD

Docs on the Laravel websites.

Customizing Emails

Quite interesting repo.

Testing Emails

Route::get('email-testing', function () {
    return new App\Mail\ExampleMailable();
});