项目作者: dansmaculotte

项目描述 :
A laravel mail template driver to send emails with
高级语言: PHP
项目地址: git://github.com/dansmaculotte/laravel-mail-template.git
创建时间: 2019-06-08T12:38:00Z
项目社区:https://github.com/dansmaculotte/laravel-mail-template

开源协议:MIT License

下载


A laravel mail template driver to send emails with

Latest Version
Total Downloads
Build Status
Quality Score
Code Coverage

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 8.1

You can install the package via composer:

  1. composer require dansmaculotte/laravel-mail-template

The package will automatically register itself.

To publish the config file to config/mail-template.php run:

  1. php artisan vendor:publish --provider="DansMaCulotte\MailTemplate\MailTemplateServiceProvider"

Finally, install the email service package needed:

  • Mailjet
  1. composer require mailjet/mailjet-apiv3-php
  • Mailchimp
  1. composer require mailchimp/transactional
  • SendGrid
  1. composer require sendgrid/sendgrid
  • Mailgun
  1. composer require mailgun/mailgun-php
  • SendinBlue
  1. 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.

  1. use MailTemplate;
  1. $mailTemplate = MailTemplate::make()
  2. ->setSubject('Welcome aboard')
  3. ->setFrom(config('mail.name'), config('mail.email'))
  4. ->setRecipient('Recipient Name', 'recipient@email.com')
  5. ->setLanguage('en')
  6. ->setTemplate('welcome-aboard')
  7. ->setVariables([
  8. 'first_name' => 'Recipient',
  9. ]);
  10. $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:

  1. php artisan make:notification WelcomeNotification

Set via to MailTemplateChannel:

  1. /**
  2. * Get the notification's delivery channels.
  3. *
  4. * @param mixed $notifiable
  5. * @return array
  6. */
  7. public function via($notifiable)
  8. {
  9. return [MailTemplateChannel::class];
  10. }

Implement toMailTemplate method and prepare your template:

  1. public function toMailTemplate($notifiable)
  2. {
  3. return MailTemplate::prepare(
  4. 'Welcome aboard',
  5. [
  6. 'name' => config('mail.from.name'),
  7. 'email' => config('mail.from.email'),
  8. ],
  9. [
  10. 'name' => $notifiable->full_name,
  11. 'email' => $notifiable->email,
  12. ],
  13. $notifiable->preferredLocale(),
  14. 'welcome-aboard',
  15. [
  16. 'first_name' => $notifiable->first_name
  17. ]
  18. );
  19. }

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

  1. 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.