Blog

Sending transactional mails using Mailcoach in a Laravel app

Besides sending newsletters and drip campaigns, Mailcoach can take care of the transactional emails that your application needs to send out.

Any transactional emails sent through Mailcoach will be displayed in the transactional mail log. Optionally, you can enable open- and/or click-tracking, so you know how effective your transactional emails are.

There’s also a way of letting non-technical people, like marketeers, edit the content of transactional emails without the need to make code changes in your app.

We recently introduced two new PHP packages to send transactional emails through Mailcoach easily:

In this post, I’d like to share how you can use the Laravel package.

Configuring Laravel to use Mailcoach to send mails

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

composer require spatie/laravel-mailcoach-mailer

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>',
],

You’ll find the Mailcoach subdomain by looking at any URL of the Mailcoach UI. You can create an API token on the API token screen in the settings.

This artisan command can test if everything is set up correctly.

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.

// will be sent through mailcoach

Mail::to('john@example.com')->send(new OrderShippedMail());

Any transactional emails sent through Mailcoach will be displayed in the transactional mail log.

On the Mailcoach UI, you can also resend any transactional mail that was sent previously.

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 email on the Transactional > Emails 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 you 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.

In closing

Sending transactional emails from your Laravel or PHP app through Mailcoach works great, and this makes Mailcoach the perfect one-stop-shop for all your mail-related needs.

If you’re not using Laravel or PHP, you can still use Mailcoach to send transactional emails from your app. Take a look at the docs of the transactional emails API endpoint.

Mailcoach is the best service for sending out email campaigns. We also offer email automation that allows you to quickly build a drip campaign. You can manage your content and templates via powerful HTML and Markdown editors. Start your free trial now.

Ready to get started?