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

Variables not getting passed to Mail? #92

Closed sukhcha-in closed 5 years ago

sukhcha-in commented 5 years ago

Hello, Why my variable $name and $email is not getting passed to Mail? It throws: Undefined variable: name Here is my code:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Sichikawa\LaravelSendgridDriver\SendGrid;
use Mail;

class MyController extends Controller
{
    public function testMail(Request $request) {
        $name = $request->name;
        $email = $request->email;

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

If i try to hardcode Email and Name variable it throws:

Class 'App\Http\Controllers\SendgridTransport' not found
s-ichikawa commented 5 years ago

You should use use language construct. see: https://www.php.net/manual/en/functions.anonymous.php

As following:

Mail::send([], [], function (Message $message) use ($name, $email) {
    ...
}
sukhcha-in commented 5 years ago

@s-ichikawa thanks for that info, SendgridTransport gives error:

"Class 'App\Http\Controllers\SendgridTransport' not found"

Do i need to create SendgridTransport Controller? What needs to be there in?

sukhcha-in commented 5 years ago

@s-ichikawa I fixed that by using 'sendgrid/x-smtpapi' instead of SendgridTransport Is this a good way to do this?

s-ichikawa commented 5 years ago

@sukhcha-in Yes, it is no problem too. Or you can solve by correct use namespace.

s-ichikawa commented 5 years ago

Like a following: https://github.com/s-ichikawa/laravel57/blob/master/app/Console/Commands/Mail/ApiSimple.php#L7

sukhcha-in commented 5 years ago

@s-ichikawa Great 😄 Thanks for this awesome library! Is there any way to check JSON response that we get from sendgrid?

s-ichikawa commented 5 years ago

Sorry, there isn't. Because the API response is abandoned here... https://github.com/laravel/framework/blob/5.8/src/Illuminate/Mail/Mailer.php#L259 https://github.com/laravel/framework/blob/5.8/src/Illuminate/Mail/Mailer.php#L484