I already have the function where I am customizing Laravel default's verify email with my own blade template.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class VerifyEmail extends \Illuminate\Auth\Notifications\VerifyEmail
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$actionUrl = $this->verificationUrl($notifiable);
$explodedLinkSlash = explode('/', $actionUrl);
$explodedLinkEquals = explode('=', $actionUrl);
$queryNumber = strtok($explodedLinkSlash[7], '?');
$expires = strtok($explodedLinkEquals[1], '&');
$hash = strtok($explodedLinkEquals[2], '&');
$signature = $explodedLinkEquals[3];
return (new MailMessage)
->from('[email protected]', 'Frederik from Walor')
->subject('Verify Email Address')
->markdown(
'emails.regular.validation-email',
[
'url' => config('app.frontend_url').'verify-email/'.$queryNumber.'/'.$expires.'/'.$hash.'/'.$signature,
]
);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Now that we are switching to SendGrid I want to change to not return a new MailMessage but just call a action that handles Sending a dynamic template with sendgrid
<?php
namespace App\Notifications;
use App\Actions\SendGridEmailAction;
use App\Helpers\SenGridHelper;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class VerifyEmail extends \Illuminate\Auth\Notifications\VerifyEmail
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$actionUrl = $this->verificationUrl($notifiable);
$explodedLinkSlash = explode('/', $actionUrl);
$explodedLinkEquals = explode('=', $actionUrl);
$queryNumber = strtok($explodedLinkSlash[7], '?');
$expires = strtok($explodedLinkEquals[1], '&');
$hash = strtok($explodedLinkEquals[2], '&');
$signature = $explodedLinkEquals[3];
(new SendGridEmailAction())->execute(
SenGridHelper::MAILS['email_verification']['id'],
'[email protected]',
['weblink' => config('app.frontend_url').'verify-email/'.$queryNumber.'/'.$expires.'/'.$hash.'/'.$signature],
);
}
}
However now I keep getting the error
"Attempt to read property \"view\" on null"
You can see my action here
<?php
namespace App\Actions;
use SendGrid;
use SendGrid\Mail\Mail;
use SendGrid\Mail\TypeException;
class SendGridEmailAction
{
private SendGrid $client;
public function __construct()
{
$this->client = new SendGrid(config('services.sendgrid.api_key'));
}
/**
* @throws TypeException
*/
public function execute(
string $templateId,
string $to,
array $data = [],
): void
{
$email = new Mail();
$email->setTemplateId($templateId);
$email->addTo($to);
$email->setFrom('[email protected]', 'Walor - No reply');
$email->addDynamicTemplateDatas($data);
$this->client->send($email);
}
}