A Mail Driver with support for Sendgrid Web API, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.
To use this package required your Sendgrid Api Key. Please make it Here.
If your project using guzzlehttp/guzzle 6.2.0 or less, you can use version 1.0.0 But the old version has security issues,
Add the package to your composer.json and run composer update.
"require": {
"s-ichikawa/laravel-sendgrid-driver": "~2.0"
},
or installed with composer
$ composer require s-ichikawa/laravel-sendgrid-driver
Add the package to your composer.json and run composer update.
"require": {
"s-ichikawa/laravel-sendgrid-driver": ~2.0
},
or installed with composer
$ composer require s-ichikawa/laravel-sendgrid-driver
Add the sendgrid service provider in bootstrap/app.php
$app->configure('mail');
$app->configure('services');
$app->register(Sichikawa\LaravelSendgridDriver\MailServiceProvider::class);
unset($app->availableBindings['mailer']);
Create mail config files. config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'sendgrid'),
];
.env
MAIL_DRIVER=sendgrid
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'
SENDGRID_SANDBOX_MODE=false
MAIL_TESTING_IS_ENABLED=false
[email protected]
config/services.php (In using lumen, require creating config directory and file.)
'sendgrid' => [
'api_key' => env('SENDGRID_API_KEY', ''),
'sandbox_mode' => env('SENDGRID_SANDBOX_MODE', false),
],
config/mail.php
'testing' => [
'is_enabled' => env('MAIL_TESTING_IS_ENABLED', false),
'address' => env('MAIL_TESTING_ADDRESS', '[email protected]')
]
Every request made to /v3/mail/send will require a request body formatted in JSON containing your email’s content and metadata. Required parameters are set by Laravel's usually mail sending, but you can also use useful features like "categories" and "send_at".
\Mail::send('view', $data, function (Message $message) {
$message
->to('[email protected]', 'foo_name')
->from('[email protected]', 'bar_name')
->embedData([
'categories' => ['user_group1'],
'send_at' => $send_at->getTimestamp(),
], 'sendgrid/x-smtpapi');
});
more info https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html#-Request-Body-Parameters
\Mail::send('view', $data, function (Message $message) {
$message
->to('[email protected]', 'foo_name')
->from('[email protected]', 'bar_name')
->replyTo('[email protected]', 'foobar');
->embedData([
'personalizations' => [
[
'to' => [
'email' => '[email protected]',
'name' => 'user1',
],
'substitutions' => [
'-email-' => '[email protected]',
],
],
[
'to' => [
'email' => '[email protected]',
'name' => 'user2',
],
'substitutions' => [
'-email-' => '[email protected]',
],
],
],
'categories' => ['user_group1'],
'custom_args' => [
'user_id' => "123" // Make sure this is a string value
]
], 'sendgrid/x-smtpapi');
});
- custom_args values have to be strings. Sendgrid API gives a non-descriptive error message when you enter non-string values.
<?
use Sichikawa\LaravelSendgridDriver\Helpers\MailExtender;
use Sichikawa\LaravelSendgridDriver\Helpers\MailParams;
class SendGridSample extends Mailable
{
use MailExtender;
public function build()
{
$mailParams = new MailParams();
$mailParams
->setSendAt(time())
->addCategory('Test')
->addCategory('Test2')
;
return $this
->view('template name')
->subject('subject')
->from('[email protected]')
->to(['[email protected]'])
->withParams($mailParams);
}
}