Laravel 5.3 sendgrid categories

In laravel 5.2, this worked very well, but since switching to 5.3, I had problems getting the category sent in my email.

public function build()
    {
        return $this->view('mail.enquiry')
            ->getSwiftMessage()->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))))
            ->subject('Website Enquiry')
            ->to(env('MAIL_DEFAULT_TO_EMAIL'), env('MAIL_DEFAULT_TO_NAME'))
            ->from(env('MAIL_DEFAULT_FROM_EMAIL'), env('MAIL_DEFAULT_FROM_NAME'))
            ->replyTo(\Request::get('email'), \Request::get('full_name'));
    }

i get this error

BadMethodCallException in Mailable.php line 525:
Method [getSwiftMessage] does not exist on mailable.

every thing in this code works fine, but it breaks as soon as I add this line:

    ->getSwiftMessage()->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))))
+1
source share
1 answer

To achieve what you are after, you can use the method withSwiftMessage().

This method accepts a callback to be passed to the instance SwiftMessage:

->withSwiftMessage(function ($message) {
    $message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))));
})

So your method will look something like this:

public function build()
{
    return $this->view('mail.enquiry')
        ->withSwiftMessage(function ($message) {
            $message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))));
        })
        ->subject('Website Enquiry')
        ->to(env('MAIL_DEFAULT_TO_EMAIL'), env('MAIL_DEFAULT_TO_NAME'))
        ->from(env('MAIL_DEFAULT_FROM_EMAIL'), env('MAIL_DEFAULT_FROM_NAME'))
        ->replyTo(\Request::get('email'), \Request::get('full_name'));
}

Hope this helps!

+3
source

Source: https://habr.com/ru/post/1612810/


All Articles