Integrations

Sending transactional emails from your Laravel application through Mailcoach

By configuring Laravel to send transactional mail through Mailcoach non-technical people can administer the content of your emails, and you gain valuable insights on how your sent emails are performing.

Using Mailcoach, you can send campaigns to lists of any size. It also provides flexible email automation to set up drip campaigns and more.

You can also use Mailcoach to send transactional emails. For Laravel apps, we’ve even created a mail driver that makes it a cinch to use Mailcoach to send emails from your app.

The big benefits of using Mailcoach for sending transactional emails are:

  • you can create templates that are easily editable by non-coders on your team

  • you can see which emails are opened and which links in the mail are clicked

  • you have a searchable log of all emails sent and can easily resend mails.

In this blog post, I’ll guide you through the installation process and showcase all the above benefits.

Configuring Laravel

To send transactional emails from your Laravel through Mailcoach, install our Laravel Mailcoach Mailer package.

In your mail.php config file, you must add a mailer in the mailers key that uses the mailcoach transport. You should also specify your Mailcoach domain (this is the first part) and a Mailcoach API token. In many cases, you also want to make Mailcoach the default mailer in your app.

Here’s an example:

// in config/mail.php
'default' => 'mailcoach',
'mailers' => [
    'mailcoach' => [
        'transport' => 'mailcoach',
        'domain' => '<your-mailcoach-subdomain>.mailcoach.app',
        'token' => '<your-api-token>',
    ],
],
php artisan mailcoach-mailer:send-test

This above command will try to send a transactional mail through Mailcoach using your configuration.

Look in your mailbox for the mail sent.

With this setup out of the way, you can send emails like you’re used to.

Editing the content of your emails on Mailcoach

Instead of saving the content of your emails in Mailable classes in your app, you can administer them in the Mailcoach UI. This way, non-technical people, such as marketeers, can edit the content of emails without a developer having to push code changes.

To get started, first create a template. A template is used to store the basic layout of your mail. Typically, you would only set this up just once.

Next, you can create an e-mail on the Transactional > Email screen.

On the created email, which we named order-confirmation, you can pick the template you want to use and specify the subject and content of the mail.

As you can see in the screenshot above, you can add placeholders, such as ::productName:: to your mail.

In your Laravel app, here’s how to create a mailable that uses that order-confirmation mail. Don’t forget to apply that UsesMailcoachMail on your mailable.

namespace App\Mails;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Spatie\MailcoachMailer\Concerns\UsesMailcoachMail;
class OrderConfirmationMail extends Mailable
{
    use Queueable; 
    use SerializesModels;
    use UsesMailcoachMail;

    public function __construct(
        protected Product $product,
        protected Carbon $shippingDate,
    ) {}

    public function build()
    {
        $this
            ->mailcoachMail('order-confirmation', [
                'productName' => $this->productName,
                'shippingDate' => $this->shippingDate->format('Y-m-d'),
            ]);
    }
}

Now you can send that order confirmation like you’re used to.

Mail::to('happy@customer.com')
   ->send(new OrderConfirmation($product, $shippingDate));

Should any changes be needed to the copy of the order confirmation, your marketeer can edit the content of the mail on Mailcoach. No code changes are needed in your app.

Viewing the log of sent mails

In the Mailcoach UI, you can see a searchable list of all emails sent. If a user of yours lets you know that they didn’t receive a particular mail, you can look up when it was sent and even resend it.

Here’ what the list of sent emails looks like:

When clicking one of the emails, you can see the sent content.

If you enabled open- and click-tracking, you can see when the mail was opened and which links were clicked.

In closing

Configuring Laravel to send transactional mail through Mailcoach is very easy. By doing so, non-technical people can administer the content of your emails, and you gain valuable insights on how your sent emails are performing.

Ready to get started?