Reputation: 731
I'm sending an email in Laravel via SendGrid using the configuration suggested in SendGrid's docs.
Just to provide an example of how it looks now:
Mail::send('emails.demo', $data, function($message)
{
$message->to('[email protected]', 'Jane Doe')->subject('This is a demo!');
});
The email itself works fine, but I'd like to add a SendGrid category. I've accomplished this in past non-Laravel projects using the addCategory()
method in this repo.
My question: Is there an easy way to add a SendGrid category just using the Laravel mail library, or does it make more sense to just use the SendGrid PHP library?
Upvotes: 4
Views: 2155
Reputation: 131
There are two different ways of doing this depending on your MAIL_MAILER
(or MAIL_DRIVER
in Laravel <7).
MAIL_DRIVER=smtp
: public function build()
{
return $this->view('mail')
->from('')
->subject('')
->withSwiftMessage(function($message) {
$message->getHeaders()->addTextHeader(
'X-SMTPAPI', json_encode(['category' => 'testing category'])
);
});
}
Read more about the smtp
driver: https://docs.sendgrid.com/for-developers/sending-email/laravel#adding-a-category-or-custom-field
MAIL_DRIVER=sendgrid
: use Sichikawa\LaravelSendgridDriver\SendGrid;
public function build()
{
return $this->view('mail')
->from('')
->subject('')
->sendgrid(['category' => 'testing category']);
}
Read more about the sendgrid
driver: https://github.com/s-ichikawa/laravel-sendgrid-driver
Upvotes: 2
Reputation: 9854
You can do this by adding an X-SMTPAPI header that contains the categories, but Laravel doesn't expose custom headers so you have to drop down to SwiftMailer directly. For an example, take a look at this thread
Upvotes: 0
Reputation: 125
I would just use the library, even though it isn't that pretty.
Upvotes: 2