- Notifications
You must be signed in to change notification settings - Fork6
Provides a notification channel for Mailgun's message templates to Laravel applications.
License
matchory/laravel-mailgun-templates-channel
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Provides a notification channel for Mailgun's message templates to Laravel applications.
This library adds a new notification channel to your app that moves email templates from Laravel to Mailgun. This isuseful if, for example, you need to send emails from multiple applications, or need a simple way for non-developers tomanage email templates.
Install the library from composer:
composer require matchory/laravel-mailgun-templates-channel
Configuration follows the instructions outlined in the Laravel documentation: You should put your Mailgun credentials intheconfig/services.php file:
// ...'mailgun' => [// Add your mailing domain as registered on Mailgun'domain' =>env('MAILGUN_DOMAIN','mailing.example.com'),// Add your Mailgun secret'secret' =>env('MAILGUN_SECRET'),// Optional: Specify the endpoint of Mailgun's EU API if you're a EU// customer and need to comply to the GDPR'endpoint' =>env('MAILGUN_ENDPOINT','https://api.eu.mailgun.net'), ],// ...
To send message templates, you should first create a template on Mailgun (navigate to "Sending" > "Templates" to manageyour templates in the Mailgun web app). Then, create a new notification with atoMailgun method:
useIlluminate\Bus\Queueable;useIlluminate\Notifications\Notification;useMatchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;class TestNotificationextends Notification{use Queueable;publicfunction__construct(privatereadonlyint$randomNumber) {}publicfunctiontoMailgun(mixed$notifiable):MailgunTemplatedMessage {return (newMailgunTemplatedMessage('your_template_name')) ->from('noreply@example.com') ->subject('Test Subject') ->param('foo','bar') ->params(['some' =>'more data','available' =>'in your template','name' =>$notifiable->name,'number' =>$this->randomNumber ]); }}
Send that notification, and you'll receive an email with the rendered template:
useIlluminate\Support\Facades\Auth;useIlluminate\Support\Facades\Notification;// number chosen by fair dice rollNotification::sendNow(Auth::user(),newTestNotification(4));
That's it - you're able to use message templates now!
TheMailgunTemplatedMessage instance exposes several message building methods to add more metadata to your message.This includes the usual stuff like subject, CC, BCC, recipient, and sender, and alsooptions andparams.
By setting additional options, you can control Mailgun features like delayed delivery and tracking; by setting params,you can add template variables to be used while Mailgun renders the template.
Refer to theMailgun documentation tolearn about the specifics.
The following methods can be leveraged to handle message options:
useMatchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;$message =newMailgunTemplatedMessage();// Add an option.// Note that the value can be anything that can be converted to JSON!$message->addOption(name:'skip-verification', value:false);// Use the fluent methods for chaining several operations together. They all// have an equivalent getter and setter.$message->option(name:'skip-verification', value:false) ->option('require-tls',true)// Set multiple options at once$message->options(['skip-verification' =>false,'require-tls' =>true,]);// Check whether options are set$message->hasOption('require-tls');// true// Retrieve all options$options =$message->getOptions();// Remove a previously set option. If the option isn't set, this does nothing$message->removeOption('require-tls');// Equivalent to the above removeOption() call$message =$message->withoutOption('require-tls');
The following methods can be leveraged to handle template rendering parameters:
useMatchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;$message =newMailgunTemplatedMessage();// Add an param.// Note that the value can be anything that can be converted to JSON!$message->addParam(name:'foo', value:'bar');// Use the fluent methods for chaining several operations together. They all// have an equivalent getter and setter.$message->param(name:'foo', value:'bar') ->param('baz',true)// Set multiple params at once$message->params(['foo' =>false,'bar' =>true,]);// Check whether params are set$message->hasParam('foo');// true// Retrieve all params$params =$message->getParams();// Remove a previously set param. If the param isn't set, this does nothing$message->removeParam('foo');// Equivalent to the above removeParam() call$message =$message->withoutParam('foo');
Unfortunately, the Mailgun SDK currently has no facilities to manage message templates, although the API endpoints existon the Mailgun servers (Seethis issue for reference).
As soon as the templates API is implemented, we will add managing capabilities to this library - including automaticallyupdating your message templates from local blade files.
Contributions are what make the open source community such an amazing place to learn, inspire, and create.Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can alsosimply open an issue. Don't forget to give the project a star! Thanks again!
Distributed under the MIT License. SeeLICENSE for more information.
About
Provides a notification channel for Mailgun's message templates to Laravel applications.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.