A way to send mail via mandrills template system.
- PHP 7.2
- Laravel 6
You can install the package via composer:
composer require addgod/laravel-mandrill-template
To publish the config file run:
php artisan vendor:publish --provider="Addgod\MandrillTemplate\MandrillTemplateServiceProvider"
Add your mandrill secret key to your .env file.
MANDRILL_SECRET="YOUR SECRET KEY"
use Addgod\MandrillTemplate\Mandrill\Message;
use Addgod\MandrillTemplate\Mandrill\Template;
use Addgod\MandrillTemplate\Mandrill\Recipient;
use Addgod\MandrillTemplate\Mandrill\Attachment;
use Addgod\MandrillTemplate\Mandrill\Recipient\Type;
use Addgod\MandrillTemplate\MandrillTemplateFacade;
$template = new Template('template-name');
$message = new Message();
$message
->setSubject('Subjct')
->setFromEmail('[email protected]')
->setFromName('Example Name')
->setMergeVars(['greeting' => 'Hello to you']);
$message->addRecipient(new Recipient('[email protected]', 'Example Name', Type::TO));
$message->addAttachment(Attachment::createFromFile($file, 'name_of_file');
MandrillTemplateFacade::send($template, $message);
Create a new notification
php artisan make:notification WelcomeNotification
Set via
to MandrillTemplateChannel
:
use Addgod\MandrillTemplate\MandrillTemplateChannel;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [MandrillTemplateChannel::class];
}
Implement toMandrillTemplate
method and prepare your template:
use Addgod\MandrillTemplate\MandrillTemplateMessage;
public function toMandrillTemplate($notifiable)
{
return (new MandrillTemplateMessage)
->template('notification')
->from('[email protected]', 'Example dot com')
->to('[email protected]', 'Frank Kornell') // This is an extra to, since the notifiable is also used.
->cc('[email protected]', 'Charlott Kornell')
->bcc('[email protected]', 'Mr admin Dude')
->greeting('Hello there'),
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!')
->salutation('Regards from us.')
->attach($file, 'name_of_file);
}