nyel-dev's avatar

How to send template email using sendgrid?

I would like to use my template in sendgrid as the view of my email in may laravel application. How can I possibly do this?

This is my current code:

Mail::raw('Raw string email', function($msg) { $msg->to(['[email protected]']) ->subject('Test email with sendgrid') ->embedData([ 'custom_args' => [ 'test_name' => "Test Name" ], 'template_id' => 'my_template_id' ],'sendgrid/x-smtpapi');

this mail sends the email but it just sends 'raw string email' in the body and it attaches a file 'sendgrid/x-smtpapi' but cant be viewed either to test the template

0 likes
3 replies
Snapey's avatar

@gale_destroyer You cannot send blade template to sendgrid.

If you are sending 1000's of very similar emails at one time then you can use their templated emails. Otherwise render the email as normal.

gale_destroyer's avatar

Managed to solve it thanks to the code from this package: https://github.com/s-ichikawa/laravel-sendgrid-driver

/**
 * Get the mail representation of the notification.
 *
 * @param mixed $notifiable
 * @return MailMessage
 */
public function toMail($notifiable): MailMessage
{
    $sendgridParams = [
        'mail_settings' => [
            'bypass_unsubscribe_management' => [
                'enable' => true
            ]
        ]
    ];

    return (new MailMessage)
        ->withSwiftMessage(function (Swift_Message $message) use ($sendgridParams) {
            $message->embed(new Swift_Image(json_encode($sendgridParams), 'sendgrid/x-smtpapi'));
        })
        ->subject('Reset Password Notification')
        ->view('emails.password-reset', ['url' => $this->buildResetURL($notifiable), 'user' => $notifiable]);
}

Please or to participate in this conversation.