s-ichikawa / laravel-sendgrid-driver

This library can add sendgrid driver into the laravel's mail configure.
MIT License
391 stars 91 forks source link

How to send SendGrid templates cleanly in Mailables #78

Closed ftrudeau closed 6 years ago

ftrudeau commented 6 years ago

Hello,

Thanks for this package.

I am using SendGrid templates exclusively, so I do not need views to be stores locally. If I do not pass a view to a Mailable class, I get a InvalidArgumentException. What I did is create a view with a HTML comment in it, but it feels like a hack.

Questions :

1) Any cleaner what to do this ? 2) According to Laravel's documentation, any public property defined in a Mailable will automatically be made available to the view. Would it be possible to automatically apply this to personalizations as well ?

For reference, here's some sample code I have in the build method :

$template_data = [
    'template_id' => $this->template_id,
    'personalizations' => [[
        'dynamic_template_data' => [
            'first_name' => $this->user->getFirstName(),
            'action_url' => route('password.reset', [
                'email' => $this->user->email,
                'token' => $this->token
            ]),
            'support_url' => route('support')
        ]
    ]]
];
return $this->view('emails.empty')->sendgrid($template_data);
s-ichikawa commented 6 years ago

Thank you for your issue.

first: I made PR for using template id without dummy view file. and I testing now. https://github.com/s-ichikawa/laravel-sendgrid-driver/pull/79/files

If this PR merged, when using a template-id, all you need to do is set an empty array in view().

second: I will create PR for this demand in the course of this month.

NHuebner1983 commented 6 years ago

Thanks to both of you!

I just started a project 2 days ago using this which needs to send the Template ID and personalization dynamic_template_data. I almost forked this, until I saw your PR. Thank you again!

NHuebner1983 commented 6 years ago

Also just letting you know I pulled your dev branch so we could get going through composer:

composer require s-ichikawa/laravel-sendgrid-driver:dev-dev

Everything worked great, your example worked awesome!

Thank you so much, can't wait to see this merge <3

NHuebner1983 commented 6 years ago

In case this helps anyone, I created our own Mail wrapper so we could easily just pass in $to array, $template_id and $data (personalizations/dynamic_template_data):

    public static function send($to, $template_id, $data)
    {
        Mail::send([], [], function (Message $message) use ($to, $template_id, $data) {
            $message
                ->to($to)
                ->from(config('mail.from.address'), config('mail.from.name'))
                ->embedData([
                    'personalizations' => [
                        [
                            'dynamic_template_data' => $data,
                        ],
                    ],
                    'template_id' => $template_id,
                ], SendgridTransport::SMTP_API_NAME);
        });
    }
NHuebner1983 commented 6 years ago

Also you can add after template_id a new array for unsubscribe groups.

                    /**
                     * Unsubscribe Group ID
                     */
                    'asm' => [
                        'group_id' => $unsubscribe_id,
                    ],

This is configured in SendGrid here: https://app.sendgrid.com/suppressions/advanced_suppression_manager The ID shows up in big text in the row towards the right of it.

ftrudeau commented 6 years ago

Thanks a lot guys

valehasadli commented 5 years ago

In case this helps anyone, I created our own Mail wrapper so we could easily just pass in $to array, $template_id and $data (personalizations/dynamic_template_data):

    public static function send($to, $template_id, $data)
    {
        Mail::send([], [], function (Message $message) use ($to, $template_id, $data) {
            $message
                ->to($to)
                ->from(config('mail.from.address'), config('mail.from.name'))
                ->embedData([
                    'personalizations' => [
                        [
                            'dynamic_template_data' => $data,
                        ],
                    ],
                    'template_id' => $template_id,
                ], SendgridTransport::SMTP_API_NAME);
        });
    }

embedData method's first parameter type is array when I remove json_encode, It doesn't work.