🤖 Teach your AI coding agent how to send email with Postmark Skills
x
How to Send Transactional Emails in Laravel PHP Framework (2024) | Postmark

How to send transactional emails with Laravel PHP framework

In this post, we’ll go over two ways you can send Laravel emails via Postmark: 

  • We'll set up Postmark's official mail transport, built into Laravel's Symfony-powered mail layer
  • We'll also look at how you can send email via SMTP

But first, let’s walk through signing up for a free Postmark developer account. If you already have a Postmark account, you can skip this step and go straight to the Laravel setup section

Create a Postmark account and sender signature #

If you don’t have an account, head to the homepage and click “Start Free Trial” in the top right corner. Fill out your account details then click “Let’s get started.”

Postmark account creation page.
Postmark account creation page.

Once you’ve entered your account information, you’ll be taken straight to the Postmark dashboard:

Postmark server dashboard
Postmark server dashboard

Set up the Postmark transport for Laravel #

In the following code examples, we'll assume you have a Laravel project running Laravel 9 or later. Laravel's mail layer runs on Symfony Mailer, and Symfony ships a Postmark transport out of the box — so you don't need a community adapter to connect Laravel and Postmark. Just a couple of Composer packages.

  1. Open your terminal and cd into your Laravel project directory.
  2. Run:
composer require symfony/postmark-mailer symfony/http-client

3. Open config/mail.php and set your default mailer to postmark:

3. Open up your config/services.php and add:

'default' => env('MAIL_MAILER', 'postmark'),

4. Open config/services.php and add your Postmark token:

return [
    // ...
    'postmark' => [
        'token' => env('POSTMARK_TOKEN'),
    ],
];

5. Open your .env file and add your Server API token, which you'll find in the API Tokens section of your Postmark dashboard:

POSTMARK_TOKEN=<your-server-api-token-here>
Image of the Postmark Credentials page
Server API tokens are found on the Postmark API Tokens page.

That's it. Laravel now sends mail through Postmark using Symfony's built-in transport.

If you want to route different email types through separate Message Streams, add a message_stream_id to your mailer's config in config/mail.php:

'postmark' => [
    'transport' => 'postmark',
    'message_stream_id' => env('POSTMARK_MESSAGE_STREAM_ID'),
],

Send email via SMTP #

Now let’s look at what it takes to send email via SMTP through Postmark instead of through the Postmark API.

1. Log into your Postmark dashboard, and head to the SMTP tab within the “Setup Instructions” section:

Image of the SMTP section within the Postmark dashboard.
SMTP section within the Postmark dashboard.

2. Next, open up config/mail.php in your favorite text editor.

Change the MAIL_HOST to smtp.postmarkapp.com.

Change the global from name and address to match the sender signature you set up during the Postmark account creation process above. If you have an existing Postmark account and skipped the account creation step above, you can find your sender signature by clicking “Sender Signatures” from your Postmark dashboard.

Finally, change the MAIL_USERNAME and MAIL_PASSWORD to match the username and password listed in your setup instructions:

A couple of notes:

  • Yes, the username and password are intentionally the same. 😉 
  • You can ignore the sendmail driver path as this sends through SMTP, not sendmail.
  • Additional ports and authentication protocols are listed in your setup instructions (in the black box above), but if you don’t know what you’re doing, it’s best to leave them defaulted to port 587, and tls for the protocol.

Big props to Craig Paul for open sourcing his Postmark adapter for Laravel.

Postmark team

Postmark team

Hi, we’re the humans behind Postmark! When you see this signature, you’ll know the piece was written collaboratively by our remote-first team distributed across the globe.