Transactional
Mailcoach can send transactional mails for your application. Any transactional mails sent through Mailcoach will be displayed in the transactional mail log.
On this page:
Optionally, you can enable open- and/or click tracking, so you know how effective your transactional mails are.
On the Mailcoach UI you can also resend any transactional mail that was sent previously.
Using Laravel
You can use Mailcoach to send all mails for your Laravel application. Behind the scenes, we’ll use separate servers to send your bulk campaigns and transactional mails.
Sending transactional mails using Mailcoach in a Laravel app
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 simply looking at any URL of the Mailcoach UI. You can create an API token on the API token screen in the settings.
You can run this artisan command to 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());
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 > 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 you would 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.
Faking mails
When you use the faking
method, your mail be sent to Mailcoach and it will be viewable in the UI. However, Mailcoach will not actually send to mail to recepient. This way you can inspect mails without actually sending them.
$this ->mailcoachMail('order-confirmation', [ 'productName' => $this->productName, 'shippingDate' => $this->shippingDate->format('Y-m-d'), ]) ->faking();
You can pass a boolean to faking
. Here’s an example where we ensure that mails will only be sent in a production environment.
->faking(! app()->environment('production'));
Using Symfony Mailer
To send transactional mails from your PHP based app using Symfony Mailer, install our Mailcoach mailer package.
composer require spatie/mailcoach-mailer
Refer to the docs of Symfony mailer on how to configure this custom mailer.
Using other languages and frameworks
If you’re not using Laravel or PHP, you can still use Mailcoach to send transactional mails from your app.
Take a look at the docs of the transactional mails API endpoint.