https://github.com/dansmaculotte/laravel-mail-template
A laravel mail template driver to send emails with
https://github.com/dansmaculotte/laravel-mail-template
facade laravel mail template
Last synced: 4 months ago
JSON representation
A laravel mail template driver to send emails with
- Host: GitHub
- URL: https://github.com/dansmaculotte/laravel-mail-template
- Owner: dansmaculotte
- License: mit
- Created: 2019-06-08T12:38:00.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-11-18T14:41:12.000Z (over 3 years ago)
- Last Synced: 2025-09-28T19:24:13.649Z (9 months ago)
- Topics: facade, laravel, mail, template
- Language: PHP
- Homepage:
- Size: 85 KB
- Stars: 35
- Watchers: 5
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# A laravel mail template driver to send emails with
[](https://packagist.org/packages/dansmaculotte/laravel-mail-template)
[](https://packagist.org/packages/dansmaculotte/laravel-mail-template)
[](https://travis-ci.org/dansmaculotte/laravel-mail-template)
[](https://scrutinizer-ci.com/g/dansmaculotte/laravel-mail-template)
[](https://coveralls.io/github/dansmaculotte/laravel-mail-template)
> This package allows you to send emails via mail service providers template's engine.
There are 5 drivers available:
- [Mailchimp](https://mailchimp.com/developer/transactional/api/)
- [Mailjet](https://dev.mailjet.com/guides/#about-the-mailjet-api)
- [Sendgrid](https://sendgrid.com/docs/api-reference/)
- [Mailgun](https://documentation.mailgun.com/en/latest/api_reference.html)
- [SendinBlue](https://developers.sendinblue.com/docs)
There is also and `log` and `null` driver for testing and debug purpose.
## Installation
### Requirements
- PHP 8.1
You can install the package via composer:
```bash
composer require dansmaculotte/laravel-mail-template
```
The package will automatically register itself.
To publish the config file to config/mail-template.php run:
```php
php artisan vendor:publish --provider="DansMaCulotte\MailTemplate\MailTemplateServiceProvider"
```
Finally, install the email service package needed:
- Mailjet
```bash
composer require mailjet/mailjet-apiv3-php
```
- Mailchimp
```bash
composer require mailchimp/transactional
```
- SendGrid
```bash
composer require sendgrid/sendgrid
```
- Mailgun
```bash
composer require mailgun/mailgun-php
```
- SendinBlue
```bash
composer require sendinblue/api-v3-sdk
```
## Usage
Configure your mail template driver and credentials in `config/mail-template.php`.
### Basic
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.
```php
use MailTemplate;
```
```php
$mailTemplate = MailTemplate::make()
->setSubject('Welcome aboard')
->setFrom(config('mail.name'), config('mail.email'))
->setRecipient('Recipient Name', 'recipient@email.com')
->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.
### Via Notification
Create a new notification via php artisan:
```bash
php artisan make:notification WelcomeNotification
```
Set `via` to `MailTemplateChannel`:
```php
/**
* 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:
```php
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 Specifics
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`.
### Testing
```bash
composer test
```
### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.