You are currently reading the documentation for v6, while the latest version is v8.

Logging transactional mails

On this page:

Mailcoach can store transactional mails, record any opens and clicks, and even resend them.

Getting started

To work with transactional mails in Mailcoach, you should use
Spatie\Mailcoach\Domain\TransactionalMail\Mails\Concerns\StoresMail trait on your Mailable class.

Storing mails

In the
content function of your mailable, you should call the store method provided by the trait.

use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Spatie\Mailcoach\Domain\TransactionalMail\Mails\Concerns\StoresMail;

class YourMailable extends Mailable
{
    use StoresMail;

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Your order has been shipped!'
        );
    }
    
    public function content(): Content
    {
        $this->store();
    
        return new Content(
            markdown: 'mails.orderShipped'
        );
    }
}

If you’re using build in your mailable, you can also the store method provided by the trait like this.

class YourMailable extends Mailable
{
    use StoresMail;

    public function build()
    {
        $this
            ->store()
            ->view('mails.your-mailable')
    }
}

Whenever this mailable is sent, Mailcoach will store and display it in the UI.

Tracking opens and clicks

When enabled with your email provider, transactional mails will track opens & clicks as well.

Tracking opens and/or clicks requires Mailcoach to store the mail, so you have to call store separately.

class YourMailable extends Mailable
{
    public function content(): Content
    {
        $this->store();
    
        return new Content(
            markdown: 'mails.orderShipped'
        );
    }
}

Resending stored mails

You can resend stored transactional mails via the UI or by calling resend on the Spatie\Mailcoach\Domain\TransactionalMail\Models\TransactionalMail model

TransactionalMail::find($id)->resend();
Introduction