A mail template driver to send emails with
dansmaculotte/laravel-mail-template is a Laravel package for a mail template driver to send emails with.
It currently has 35 GitHub stars and 3.033 downloads on Packagist (latest version v4.0.1).
Install it with composer require dansmaculotte/laravel-mail-template.
Discover more Laravel packages by dansmaculotte
or browse all Laravel packages to compare alternatives.
Last updated
This package allows you to send emails via mail service providers template's engine.
There are 5 drivers available:
There is also and log and null driver for testing and debug purpose.
You can install the package via composer:
composer require dansmaculotte/laravel-mail-template
The package will automatically register itself.
To publish the config file to config/mail-template.php run:
php artisan vendor:publish --provider="DansMaCulotte\MailTemplate\MailTemplateServiceProvider"
Finally, install the email service package needed:
composer require mailjet/mailjet-apiv3-php
composer require mailchimp/transactional
composer require sendgrid/sendgrid
composer require mailgun/mailgun-php
composer require sendinblue/api-v3-sdk
Configure your mail template driver and credentials in config/mail-template.php.
After you've installed the package and filled in the values in the config-file working with this package will be a breeze. All the following examples use the facade. Don't forget to import it at the top of your file.
use MailTemplate;
$mailTemplate = MailTemplate::make()
->setSubject('Welcome aboard')
->setFrom(config('mail.name'), config('mail.email'))
->setRecipient('Recipient Name', '[email protected]')
->setLanguage('en')
->setTemplate('welcome-aboard')
->setVariables([
'first_name' => 'Recipient',
]);
$response = $mailTemplate->send();
If an error occurs in the send method it will throw a SendError::responseError exception.
Create a new notification via php artisan:
php artisan make:notification WelcomeNotification
Set via to MailTemplateChannel:
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [MailTemplateChannel::class];
}
Implement toMailTemplate method and prepare your template:
public function toMailTemplate($notifiable)
{
return MailTemplate::prepare(
'Welcome aboard',
[
'name' => config('mail.from.name'),
'email' => config('mail.from.email'),
],
[
'name' => $notifiable->full_name,
'email' => $notifiable->email,
],
$notifiable->preferredLocale(),
'welcome-aboard',
[
'first_name' => $notifiable->first_name
]
);
}
And that's it.
When MailTemplateChannel will receive the notification it will automatically call send method from MailTemplate facade.
Mailjet API allows to set an email to debug templates. When a template error is
encountered on email sending, Mailjet sends an error report to this mailbox. To
do so, set the email in config/mail-template.php, in key
mailjet.debug_email.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.