If you are building Laravel apps that send transactional emails, perhaps you've run into situations where the client or the marketing department asks for simple changes in those email templates. If you're using SendGrid to send out your emails, there's another way.
You can use SendGrid's dynamic templates feature to create and maintain your transactional email templates using a drag & drop UI. Dynamic templates support handlebars, so you can still insert variables or run if/else checks and even for loops.
How to set it up?
If you're normally using Laravel's notifications feature, and wonder how you can send notifications using Sendgrid dynamic templates, then we have a package for you 📦 (view it on Github)
Simply require the package:
swiftmade/laravel-sendgrid-notification-channel
Then in your notification class, make the following changes:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use NotificationChannels\SendGrid\SendGridChannel; // <-- import this
class ExampleNotification extends Notification
{
public function via($notifiable)
{
return [
SendGridChannel::class, // <-- add this
];
}
// And finally this...
public function toSendGrid($notifiable)
{
return (new SendGridMessage('Your SendGrid template ID'))
/**
* optionally set the from address.
* by default this comes from config/mail.from
* ->from('[email protected]', 'App name')
*/
/**
* optionally set the recipient.
* by default it's $notifiable->email:
* ->to('[email protected]', 'Mr. Smith')
*/
->payload([
"variable" => "value"
]);
}
}