loots-it/laravel-mail-template-channel

An alternative for the standard mail channel that uses an external template for its content. An example of such a service that provides such templates is Mailjet, for which the implementation is provided.

v1.0.0-beta 2020-10-28 15:31 UTC

This package is auto-updated.

Last update: 2024-03-29 04:18:56 UTC


README

Total Downloads Latest Stable Version License

Installation

First, include the package with composer:

composer require loots-it/laravel-mail-template-channel

The most important Provider is the MailTemplateDriverServiceProvider and it's auto discovered via the composer.json file.

The other Provider is TestMailTemplateChannelServiceProvider which adds an Artisan command to test your MailTemplateDriver configuration. If you want to use it, you will need to add it manually to config/app.php.

  • The providers array:
'providers' => [
    ...
    LootsIt\LaravelMailTemplateChannel\Providers\TestMailTemplateChannelServiceProvider::class,
    LootsIt\LaravelMailTemplateChannel\Providers\MailTemplateDriverServiceProvider::class, // This one is auto discovered
    ...
],

Configuration

Add the Mailjet API key/secret in your .env file:

MAILJET_APIKEY=YOUR_APIKEY
MAILJET_APISECRET=YOUR_APISECRET

Your API key / secret can be found here.

Add the following section to the config/services.php file:

'mailjet' => [
    'key' => env('MAILJET_APIKEY'),
    'secret' => env('MAILJET_APISECRET'),
],

If you want to send emails via notifications without specifying the sender email/name in the notification, you will have to add a default sender. To do this, you will first have to publish the config file:

php artisan vendor:publish --provider="LootsIt\LaravelMailTemplateChannel\Providers\MailTemplateDriverServiceProvider"

Then you have to add a default sender email address and default sender name:

return [
    'from' => [
        'email' => 'info@yourdomain.com',
        'name' => 'yourdomain.com',
    ],
];

Test configuration

You can test your configuration of the mail template Driver/Channel using the command provided by the TestMailTemplateChannelServiceProvider. You will need a template that doesn't need any variables and pass the id (in this example 1) to the command:

php artisan mailTemplateDriver:test 1

This won't actually send an email, it will only test the configuration.

Usage

You can use this Channel for any Notification. As an example, I will create a minimal Notification here:

php artisan make:notification VerifyEmailNotification

You can delete the standard toMail($notifiable) method if you want. You should change the via($notifiable) method and implement the toExternalMailTemplate($notifiable) like below:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use LootsIt\LaravelMailTemplateChannel\ExternalMailTemplateChannel;
use LootsIt\LaravelMailTemplateChannel\MailTemplateMessage;

class VerifyEmailNotification extends Notification
{
    use Queueable;

    private int $templateID;
    private string $verificationLink;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($templateID, $verificationLink)
    {
        $this->templateID = $templateID;
        $this->verificationLink = $verificationLink;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [ExternalMailTemplateChannel::class];
    }

    public function toExternalMailTemplate($notifiable) {

        $variables = [
            "verification_link" => $this->verificationLink,
        ];

        $message = new MailTemplateMessage($this->templateID, $variables);
        $message->subject = "Verify email address";

        return $message;
    }
}

You can use this notification like this:

$user->notify(New VerifyEmailNotification($templateID, $verificationLink));