Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I fail to pass custom vars with laravel-sendgrid-driver #155

Open
sergeynilov opened this issue Jun 23, 2021 · 0 comments
Open

I fail to pass custom vars with laravel-sendgrid-driver #155

sergeynilov opened this issue Jun 23, 2021 · 0 comments

Comments

@sergeynilov
Copy link

Hello,
I add sendgrid support with https://github.com/s-ichikawa/laravel-sendgrid-driver
in my laravel 8 but I have problems with adding custom perameters. I do in control :

    public function banUser(Request $request)
    {
        $user_id = $request->user_id;
        $bannedUser = User::find($user_id);
        if ($bannedUser === null) {
            return response()->json(['message' => 'User # "' . $request->user_id . '" not found!'],
                HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
        }
        $site_home_url          = config('app.url');
        $bannedUser->ban_reason = $request->ban_reason;
        if(empty($bannedUser->ban_reason)) {
            $bannedUser->ban_reason= 'Ban em all';
        }
        $bannedUser->status     = false;
        $bannedUser->updated_at = \Carbon\Carbon::now(config('app.timezone'));
        $support_signature                          = config('app.support_signature', '');

        try {
            $bannedUser->save();
            $additiveVars= [ // Additive Vars I want to see in my email view
                'site_home_url'            => $site_home_url,
                'to_user_email'            => $bannedUser->email,
                'to_username'              => $bannedUser->full_name,
                'ban_reason'               => $bannedUser->ban_reason,
                'support_signature'        => $support_signature,
            ];
            $site_name= config('app.APP_NAME');
            $subject= 'Your account at ' . $site_name . ' was banned ';

            \Mail
                ::to($bannedUser->email)
                ->send( new SendgridMail( 'emails/user_was_baned', $bannedUser->email, [], $subject , $additiveVars ) )
//                ->embedData( // if to uncomment these lines it does not work anyway
//                    ['custom_args'=>$additiveVars]
//                );

    }

I created custom email objects : /app/Mail/SendgridMail.php :

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Sichikawa\LaravelSendgridDriver\SendGrid;

class SendgridMail extends Mailable
{
    use Queueable, SerializesModels;
    use SendGrid;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    private  $m_view_name;
    private  $m_to_email_address;
    private  $m_ccEmailAddressList;
    private  $m_subject_text;
    private  $m_additiveVars;
    private  $m_attachFiles;

    public function __construct( string $view_name, string $to, array $cc= [], string $subject= '', array $additiveVars= [], array $attachFiles= [] )
    {
        $this->m_view_name= $view_name;
        $this->m_to_email_address= $to;
        $this->m_ccEmailAddressList= $cc;
        }
        $this->m_subject_text= $subject;


        $additiveVars['site_home_url']      = \Config::get('app.url');
        $additiveVars['site_name']          = \Config::get('app.name');
        $additiveVars['mail_from_address']  = \Config::get('app.MAIL_FROM_ADDRESS');
        $additiveVars['mail_from_name']     = \Config::get('app.MAIL_FROM_NAME');
        if ( $this->m_is_debug ) {
            echo '<pre>$additiveVars::' . print_r($additiveVars, true) . '</pre>';
        }
        $this->m_additiveVars= $additiveVars;
        $this->m_attachFiles= $attachFiles;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build( )
    {
        \Log::info(  varDump($this->m_additiveVars, ' -1 build $this->m_additiveVars::') );  // I check list of Additive Vars as I expect 
        $mailObject= $this
            ->view( $this->m_view_name )
            ->subject( $this->m_subject_text )
            ->to( [$this->m_to_email_address] )
            ->cc( $this->m_ccEmailAddressList )
            ->with( $this->m_additiveVars )  // I send list of Additive Vars as into view
            ->sendgrid( $this->m_additiveVars );

        foreach( $this->m_attachFiles as $next_attach_file) {
            if ( file_exists($next_attach_file) ) {
                $mailObject->attach($next_attach_file);
            }
        }
        return $mailObject;

    }
}

and resources/views/emails/user_was_baned.blade.php:

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
</head>
<body>
<div class="wrapper">

	<h4 class="email_title">
		Hello, {{ $to_username }}
	</h4>

	<p class="email_title">You were baned at <a href="{{ $site_home_url }}" target="_blank" class="a_link">{{ $site_name }}</a> site !</p>

	<p class="email_subtitle">Now you can not login under your credentials :<br>
		User's email : {{ $to_user_email }}<br>
        Ban reason : {{ $ban_reason }}
	</p>

	<p class="email_footer">{!! $support_signature !!}</p>

</div>
</body>
</html>

But I got errors like:

{"message":"User \"29\" not banned : Client error: POST https://api.sendgrid.com/v3/mail/sendresulted in a400 Bad Requestresponse:\n{\"errors\":[{\"message\":\"Additional property site_name is not allowed.\",\"field\":\"site_name\",\"help\":null},{\"message\":\"Addit (truncated...)\n"}
OR:
{"message":"User \"29\" not banned : Client error: POST https://api.sendgrid.com/v3/mail/sendresulted in a400 Bad Requestresponse:\n{\"errors\":[{\"message\":\"Additional property mail_from_address is not allowed.\",\"field\":\"mail_from_address\",\"help\":null},{ (truncated...)\n"}

OR:
{"message":"User \"29\" not banned : Client error: POST https://api.sendgrid.com/v3/mail/sendresulted in a400 Bad Requestresponse:\n{\"errors\":[{\"message\":\"Additional property to_user_email is not allowed.\",\"field\":\"to_user_email\",\"help\":null},{\"message (truncated...)\n"}
OR:
{"message":"User \"29\" not banned : Client error: POST https://api.sendgrid.com/v3/mail/sendresulted in a400 Bad Requestresponse:\n{\"errors\":[{\"message\":\"Additional property site_home_url is not allowed.\",\"field\":\"site_home_url\",\"help\":null},{\"message (truncated...)\n"}

OR:
{"message":"User \"29\" not banned : Client error: POST https://api.sendgrid.com/v3/mail/sendresulted in a400 Bad Requestresponse:\n{\"errors\":[{\"message\":\"Additional property to_user_email is not allowed.\",\"field\":\"to_user_email\",\"help\":null},{\"message (truncated...)\n"}
But any of these eorrors is different and looks like any next request raise error with different(next)
var in template.

Why error and how can it be fixed ?

        "laravel/framework": "^8.12",
        "s-ichikawa/laravel-sendgrid-driver": "^3.0",

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant