Skip to content

Commit

Permalink
Use SendGrid's SDK without the Laravel SendGrid driver...
Browse files Browse the repository at this point in the history
  • Loading branch information
amcsi committed Mar 30, 2020
1 parent 34bc96a commit dc730b3
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 64 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# Laravel SendGrid Example

## Laravel Versions

### Laravel 7
For Laravel 7, install v3 of `s-ichikawa/laravel-sendgrid-driver`.

### Laravel 6
For Laravel 6, install v2. Also you use `MAIL_DRIVER` instead of of `MAIL_MAILER`.

## Etc

### Command
Expand Down
42 changes: 18 additions & 24 deletions app/Console/Commands/EmailSendTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Mail\Message;
use Sichikawa\LaravelSendgridDriver\Transport\SendgridTransport;
use SendGrid;
use SendGrid\Mail\Mail;

class EmailSendTestCommand extends Command
{
Expand Down Expand Up @@ -34,34 +34,28 @@ public function __construct()

/**
* Execute the console command.
* @throws SendGrid\Mail\TypeException
*/
public function handle()
{
if (!config('mail.from.address')) {
throw new \LogicException('You must have the MAIL_FROM_ADDRESS env var set.');
}

\Mail::send([], [], function (Message $message) {
/** @noinspection PhpParamsInspection */
$message
->to($this->option('to'))
->embedData([
'personalizations' => [
[
'dynamic_template_data' => [
'title' => 'Subject',
'name' => 's-ichikawa',
],
],
],
'asm' => [
'group_id' => (int) config('services.sendgrid.unsubscribe_group_id')
],
'template_id' => config('services.sendgrid.template_id'),
'dynamic_template_data' => [
'titleText' => 'Dynamic Title Text!',
],
], SendgridTransport::SMTP_API_NAME);
});
$sg = new SendGrid(config('services.sendgrid.api_key'));
$email = new Mail();
$email->setFrom(config('mail.from.address'), config('mail.from.name'));
$email->addTo($this->option('to'), 'Test user');
$email->setAsm((int) config('services.sendgrid.unsubscribe_group_id'));
$email->setTemplateId(config('services.sendgrid.template_id'));
$email->addDynamicTemplateDatas([
'titleText' => 'Dynamic Title Text!',
]);
$emailResponse = $sg->send($email);
if (($statusCode = $emailResponse->statusCode()) !== 202) {
throw new \RuntimeException("Could not send email. Status code $statusCode:\n{$emailResponse->body()}");
}

echo "Email sent.\n";
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.0",
"laravel/tinker": "^2.0",
"s-ichikawa/laravel-sendgrid-driver": "^3"
"sendgrid/sendgrid": "^7.4"
},
"require-dev": {
"facade/ignition": "^2.0",
Expand Down
111 changes: 80 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit dc730b3

@amcsi
Copy link
Owner Author

@amcsi amcsi commented on dc730b3 Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nazariy-tertyshnyi are you asking why I didn't implement this using Laravel's contract?

I did use a library that used Laravel's contract as you can see, but I removed it, because it didn't support multiple "to" fields.
Then I realized that in my example of what you can do with SendGrid that I would need to do a lot of custom stuff, I didn't want to reimplement this as a mailable, and I opted to just use SendGrid's SDK directly.
It's still possible to use the library with the mailable and to use it alongside the SendGrid SDK, but the aim of my examples here were intended to focus on the custom SendGrid functionality.

Please sign in to comment.