tightr/laravel-mail-template

A mail template driver to send emails with

v1.1.2 2020-03-13 16:18 UTC

This package is auto-updated.

Last update: 2024-11-14 03:05:47 UTC


README

Latest Version Total Downloads Build Status

This package is a fork from the original package by DansMaCulotte.

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.

Installation

Requirements

  • PHP 7.2

You can install the package via composer:

composer require tightr/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="Tightr\MailTemplate\MailTemplateServiceProvider"

Finally, install the email service package needed:

  • Mailjet
composer require mailjet/mailjet-apiv3-php
  • Mandrill
composer require mandrill/mandrill
  • SendGrid
composer require sendgrid/sendgrid
  • Mailgun
composer require mailgun/mailgun-php
  • SendinBlue
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.

use MailTemplate;
$mailTemplate = MailTemplate::setSubject('Welcome aboard')
    ->setFrom(config('mail.name'), config('mail.email'))
    ->setRecipient('Recipient Name', 'recipient@email.com')
    ->setLanguage('en')
    ->setTemplate('welcome-aboard')
    ->addAttachment(storage_path('pdf/invoice.pdf'), 'invoice-42.pdf')
    ->trackClicks(true)
    ->trackOpens(true)
    ->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:

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::setSubject('Welcome aboard')
        ->setFrom(config('mail.name'), config('mail.email'))
        ->setRecipient('Recipient Name', 'recipient@email.com')
        ->setLanguage('en')
        ->setTemplate('welcome-aboard')
        ->addAttachment(storage_path('pdf/invoice.pdf'), 'invoice-42.pdf')
        ->trackClicks(true)
        ->trackOpens(true)
        ->setVariables([
           'first_name' => 'Recipient',
        ]);
}

And that's it. When MailTemplateChannel will receive the notification it will automatically call send method from MailTemplate facade.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.