mailer-laravel maintained by lshtmweb
Mailer Laravel
This package contains a new mail driver that allows laravel to send emails using the default laravel methods.
##Getting Started
Install the application via compose
composer require lshtmweb/mailer-laravel.
Run the following to get the configuration file.
php artisan vendor:publish --provider="Lshtmweb\MailerLaravel\MailerServiceProvider"
Add your application credentials to you .env file.
You will need to set up the following keys.
MAILER_URL= //URL to your instance of mailer.
MAILER_APP_ID= //Application ID generated when you register the application
MAILER_APP_KEY= //Application key generated when you register application
MAILER_QUEUE_MESSAGE= //Set to TRUE/FALSE to enable or disable email queue
MAILER_MESSAGE_DELIMITER= //Default to ';'
and run the migration
php artisan migrate
Creating a Template
To create a template, you use the Lshtmweb\MailerLaravel\MailerTemplate model. This will automatically upload the template
to mailer server provided that you .env config above are correct.
MailerTemplate::create([
'key' => 'GENERATE_EMAIL_TEMPLATE',
'description' => 'This is a general email sent to admissions',
'name' => 'General email template for admissions',
'subject' => 'General Notification',
'content' => '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<p>$subject</p>
<p>$content</p>
</body></html>',
]
);
Please note that the template key is important to give you another way of calling the template without having to remember the template UUID.
Sending an email
To send an email. You will first need to define a notification channel for mailer
<?php
namespace App\Notifications\Channels;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Lshtmweb\MailerLaravel\Contracts\MailerMessageContract;
use Lshtmweb\MailerLaravel\Contracts\MailerNotificatinoContract;
use Lshtmweb\MailerLaravel\MailerMessage;
use Lshtmweb\MailerLaravel\Services\MailerService;
class MailerNotificationChannel
{
/**
* Send the given notification.
*/
public function send(object $notifiable, Notification $notification): void
{
$message = $notification->toMailer($notifiable);
if ($message instanceof MailerMessageContract) {
if (empty($message->getTo())) {
$to = $notifiable->routeNotificationFor('mailer', $notification) ??
$notifiable->routeNotificationFor(self::class, $notification);
if (!$to) {
Log::info("No recipients found for this notification");
return;
}
$message = $message->to($to);
}
/** @var MailerService $service */
$service = App::make(MailerService::class);
$service->sendEmail($message);
}
else {
Log::error('Invalid mailer message. Please ensure message support MailerMessageContract');
}
}
}
And you need to create a laravel notification and implement the Lshtmweb\MailerLaravel\Contracts\MailerNotificationContract.
<?php
namespace App\Notifications;
use App\Constants\MailerConstants;
use App\Notifications\Channels\MailerNotificationChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Lshtmweb\MailerLaravel\Contracts\MailerMessageContract;
use Lshtmweb\MailerLaravel\Contracts\MailerNotificationContract;
use Lshtmweb\MailerLaravel\MailerMessage;
class GeneralNotification extends Notification implements MailerNotificationContract
{
use Queueable;
...
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return [MailerNotificationChannel::class];
}
...
/**
* Set to recipients for the message
*
* @param mixed $notifiable
* @return MailerMessageContract
*/
public function toMailer(mixed $notifiable): MailerMessageContract
{
$params = [
'subject' => $this->subject,
'content' => $this->message,
];
return (new MailerMessage())
->templateKey(MailerConstants::GENERATE_EMAIL_TEMPLATE)
->params($params);
}
}
If you have a notifiable model e.g User model you will need to implement Lshtmweb\MailerLaravel\Contracts\MailerNotifiableContract.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Lshtmweb\MailerLaravel\Contracts\MailerNotifiableContract;
class User extends Authenticatable implements MailerNotifiableContract
{
use HasApiTokens, HasFactory, Notifiable;
...
/**
* Get the email address that should be used for a notifiable object
*
* @param mixed $notification
*
* @return string
*/
public function routeNotificationForMailer(mixed $notification): string
{
return $this->email;
}
...
}
You now trigger a notification using one of the following
Notification::route('mailer', 'username@laravel.test')
->notify(new GeneralNotification());
// Or through notificable model e.g as
$user->notify(new GeneralNotification());
If you have MAILER_QUEUE_MESSAGE=TRUE in your .env file, you can dispatch queued emails by doing the below
// Runing the below command
php artisan mailer:dispatch_mail
//Or you can schedule it to run periodically
Schedule::command('mailer:dispatch_mail')->everyFiveMinutes()
->timezone('Europe/London')
->withoutOverlapping();
Issues and Suggestions
If you find any issues, raise a ticket!