s-ichikawa / laravel-sendgrid-driver

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

SendGrid template_id not working. #98

Closed valehasadli closed 5 years ago

valehasadli commented 5 years ago

Hi, thanks for the great package,

I have some issues about using SendGrid templates. In another client, everything works fine.

{ "personalizations": [ { "to" : [ { "email" : "valehasadli@gmail.com", "name": "Valeh Asadli" } ], "dynamic_template_data": { "branch_name": "Galiano" } } ], "from": { "email" : "noreply@thecompass.travel", "name" : "Compass Travel" }, "template_id" : "temp_idxxx" }

this JSON totally working fine postman client, variable passed and I get an email. But I facing this problem LARAVEL code.

Mail::send([], [], function (Message $message) { $message ->to($this->data['to']) ->from('noreply@thecompass.travel') ->subject('Invoice') ->embedData( json_encode([ 'personalizations' => [ [ 'dynamic_template_data' => [ 'branch_name' => 'amk', ], ], ], 'template_id' => 'xxxx', ]), SendgridTransport::SMTP_API_NAME ); });

I got an email but SMTP type file and JSON structure. How can I fix

JOB FILE:

`<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable; use Illuminate\Mail\Message; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Facades\Mail; //use App\Mail\SendReservationInvoice as SendReservationInvoiceMail; use Sichikawa\LaravelSendgridDriver\SendGrid; use Sichikawa\LaravelSendgridDriver\Transport\SendgridTransport;

class SendReservationInvoice implements ShouldQueue { private $data;

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, SendGrid;

/**
 * SendReservationInvoice constructor.
 * @param array $data
 */
public function __construct(array $data)
{
    $this->data = $data;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle(): void
{

// Mail::to($this->data['to']) // ->send(new SendReservationInvoiceMail($this->data));

    Mail::send([], [], function (Message $message) {
        $message
            ->to($this->data['to'])
            ->from('noreply@thecompass.travel')
            ->subject('Invoice')
            ->embedData(
                json_encode([
                    'personalizations' => [
                        [
                            'dynamic_template_data' => [
                                'branch_name' => 'amk',
                            ],
                        ],
                    ],
                    'template_id' => 'xxxx',
                ]),
                SendgridTransport::SMTP_API_NAME
            );
    });
}

} `

s-ichikawa commented 5 years ago

@nanophp Hi, What is your laravel version? My laravel 5.7 code is working with the dynamic template. like following: https://github.com/s-ichikawa/laravel57/blob/master/app/Console/Commands/Mail/ApiSimple.php#L43

valehasadli commented 5 years ago

@nanophp Hi, What is your laravel version? My laravel 5.7 code is working with the dynamic template. like following: https://github.com/s-ichikawa/laravel57/blob/master/app/Console/Commands/Mail/ApiSimple.php#L43

Hi, my laravel app version is 5.8.16.

valehasadli commented 5 years ago

@s-ichikawa I'm wanna using template id without the laravel view.

valehasadli commented 5 years ago

@s-ichikawa I got such email Screen Shot 2019-05-13 at 6 22 12 AM

valehasadli commented 5 years ago

@s-ichikawa I got such email Screen Shot 2019-05-13 at 6 22 12 AM

app('mailer')->send([], [], function (Message $message) { $message ->subject('Invoice.') ->to($this->data['to']) ->from('noreply@thecompass.travel') ->embedData(self::sgEncode([ 'personalizations' => [ [ 'to' => [ 'email' => $this->data['to'], 'name' => 'TheCompass Digital', ], 'dynamic_template_data' => [ 'branch_name' => 'Shingo Ichikawa', ], ], ], 'template_id' => 'eeeee' ]), SendgridTransport::SMTP_API_NAME); });

s-ichikawa commented 5 years ago

I tried with 5.8, but worked well. https://github.com/s-ichikawa/laravel58/blob/master/app/Console/Commands/Mail/ApiSimple.php#L45

スクリーンショット 2019-05-13 20 03 35

Is your MAIL_DRIVER value in .env file seted smtp? I got the same case in this way.

スクリーンショット 2019-05-13 20 33 16

You must edit to sendgrid for using this library. https://github.com/s-ichikawa/laravel-sendgrid-driver#configure

valehasadli commented 5 years ago

https://github.com/s-ichikawa/laravel58/blob/master/app/Console/Commands/Mail/ApiSimple.php#L45

@s-ichikawa Yes, MAIL_DRIVER setted as smtp

`MAIL_DRIVER=smtp MAIL_HOST=smtp.sendgrid.net MAIL_PORT=587 MAIL_USERNAME=my_username MAIL_PASSWORD=my_pass MAIL_ENCRYPTION=tls MAIL_FROM_NAME="Compass Digital" MAIL_FROM_ADDRESS=noreply@thecompass.travel

SENDGRID_ENDPOINT=https://api.sendgrid.com/v3/mail/send`

Screen Shot 2019-05-13 at 3 33 30 PM

Also, my current code looks like this

Screen Shot 2019-05-13 at 3 39 22 PM
valehasadli commented 5 years ago

I tried with 5.8, but worked well. https://github.com/s-ichikawa/laravel58/blob/master/app/Console/Commands/Mail/ApiSimple.php#L45

スクリーンショット 2019-05-13 20 03 35

Is your MAIL_DRIVER value in .env file seted smtp? I got the same case in this way.

スクリーンショット 2019-05-13 20 33 16

You must edit to sendgrid for using this library. https://github.com/s-ichikawa/laravel-sendgrid-driver#configure

I need to receive sendgrid template as an email

s-ichikawa commented 5 years ago

Please, change MAIL_DRIVER value to sendgrid from smtp. In setted smtp, you are not able to call this library.

valehasadli commented 5 years ago

@s-ichikawa Thankssssss!!! :)))))) It works!!!!

Diksha95-et commented 5 years ago

@s-ichikawa hey i get an email like this image and my error looks like this please give me any solution image my sendgrid dynamic template id is not receive as an email in laravel