Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 29 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Laravel SendGrid Driver
====

[![SymfonyInsight](https://insight.symfony.com/projects/4232643f-006c-473b-97ff-d0f67fa497ee/mini.svg)](https://insight.symfony.com/projects/4232643f-006c-473b-97ff-d0f67fa497ee)
[![SymfonyInsight](https://insight.symfony.com/projects/8955bc55-16f6-4ac9-8203-1cdce3d209a8/mini.svg)](https://insight.symfony.com/projects/8955bc55-16f6-4ac9-8203-1cdce3d209a8)
[![Build Status](https://scrutinizer-ci.com/g/s-ichikawa/laravel-sendgrid-driver/badges/build.png?b=master)](https://scrutinizer-ci.com/g/s-ichikawa/laravel-sendgrid-driver/build-status/master)

A Mail Driver with support for Sendgrid Web API, using the original Laravel API.
Expand All @@ -10,17 +10,12 @@ This library extends the original Laravel classes, so it uses exactly the same m
To use this package required your [Sendgrid Api Key](https://sendgrid.com/docs/User_Guide/Settings/api_keys.html).
Please make it [Here](https://app.sendgrid.com/settings/api_keys).

# Notification

If your project using guzzlehttp/guzzle 6.2.0 or less, you can use version [1.0.0](https://github.com/s-ichikawa/laravel-sendgrid-driver/tree/1.0.0)
But the old version has [security issues](https://github.com/guzzle/guzzle/releases/tag/6.2.1),

# Install (Laravel)
# Install (for [Laravel](https://laravel.com/))

Add the package to your composer.json and run composer update.
```json
"require": {
"s-ichikawa/laravel-sendgrid-driver": "~3.0"
"s-ichikawa/laravel-sendgrid-driver": "^4.0"
},
```

Expand All @@ -37,18 +32,18 @@ Add the sendgrid service provider in config/app.php:
];
```

# Install (Lumen)
# Install (for [Lumen](https://lumen.laravel.com/))

Add the package to your composer.json and run composer update.
```json
"require": {
"s-ichikawa/laravel-sendgrid-driver": "~2.0"
"s-ichikawa/laravel-sendgrid-driver": "^4.0"
},
```

or installed with composer
```bash
$ composer require "s-ichikawa/laravel-sendgrid-driver:~2.0"
$ composer require "s-ichikawa/laravel-sendgrid-driver:^4.0"
```

Add the sendgrid service provider in bootstrap/app.php
Expand Down Expand Up @@ -105,69 +100,14 @@ For example, calls to SendGrid API through a proxy, call endpoint for confirming
],
```

## Request Body Parameters
## How to use

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".

```php
\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


## API v3

```php
\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 in Mailable

```php
<?
use Sichikawa\LaravelSendgridDriver\SendGrid;
Expand All @@ -186,11 +126,21 @@ class SendGridSample extends Mailable
->sendgrid([
'personalizations' => [
[
'substitutions' => [
':myname' => 's-ichikawa',
'to' => [
['email' => '[email protected]', 'name' => 'to1'],
['email' => '[email protected]', 'name' => 'to2'],
],
'cc' => [
['email' => '[email protected]', 'name' => 'cc1'],
['email' => '[email protected]', 'name' => 'cc2'],
],
'bcc' => [
['email' => '[email protected]', 'name' => 'bcc1'],
['email' => '[email protected]', 'name' => 'bcc2'],
],
],
],
'categories' => ['user_group1'],
]);
}
}
Expand All @@ -202,10 +152,14 @@ Illuminate\Mailer has generally required a view file.
But in case of using template id, set an empty array at view function.
```php
<?
\Mail::send([], [], function (Message $message) {
$message
->to('[email protected]')
->embedData([
public function build()
{
return $this
->view('template name')
->subject('subject')
->from('[email protected]')
->to(['[email protected]'])
->sendgrid([
'personalizations' => [
[
'dynamic_template_data' => [
Expand All @@ -215,24 +169,6 @@ But in case of using template id, set an empty array at view function.
],
],
'template_id' => config('services.sendgrid.templates.dynamic_template_id'),
], SendgridTransport::SMTP_API_NAME);
});
```

## Using with Telescope

In case [telescope](https://laravel.com/docs/5.7/telescope) is active and set array to first variable in embedData, telescope's watcher happen error in encoding.
In ordar to avoid this probrem, you can use sgEncode function.
```php
<?
use Sichikawa\LaravelSendgridDriver\SendGrid;

\Mail::send('view', $data, function (Message $message) {
$message
->to('[email protected]', 'foo_name')
->from('[email protected]', 'bar_name')
->embedData(sgEncode([
'categories' => ['user_group1'],
]), 'sendgrid/x-smtpapi');
});
]);
}
```
18 changes: 10 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"keywords": ["laravel", "sendgrid"],
"license": "MIT",
"require": {
"illuminate/mail": ">=7.0",
"illuminate/support": ">=7.0",
"guzzlehttp/guzzle": "^6.3 || ^7.0"
"illuminate/mail": "^9.0",
"illuminate/support": "^9.0",
"guzzlehttp/guzzle": "^7.2"
},
"require-dev": {
"illuminate/container": ">=7.0",
"illuminate/filesystem": ">=7.0",
"phpunit/phpunit": "^8.5",
"illuminate/container": "^9.0",
"illuminate/filesystem": "^9.0",
"phpunit/phpunit": "^9.5.8",
"laravel/helpers": "^1.2",
"vlucas/phpdotenv": "^4.1 || ^5.2"
"vlucas/phpdotenv": "^5.4.1"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -43,5 +43,7 @@
"Sichikawa\\LaravelSendgridDriver\\SendgridTransportServiceProvider"
]
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
11 changes: 5 additions & 6 deletions src/SendGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Sichikawa\LaravelSendgridDriver;

use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Messages\MailMessage;
use Sichikawa\LaravelSendgridDriver\Transport\SendgridTransport;
use Swift_Message;
use Symfony\Component\Mime\Email;

trait SendGrid
{
Expand All @@ -16,9 +15,9 @@ trait SendGrid
*/
public function sendgrid($params)
{
if (($this instanceof Mailable || $this instanceof MailMessage) && $this->mailDriver() == "sendgrid") {
$this->withSwiftMessage(function (Swift_Message $message) use ($params) {
$message->embed(new \Swift_Image(static::sgEncode($params), SendgridTransport::SMTP_API_NAME));
if (($this instanceof Mailable) && $this->mailDriver() == "sendgrid") {
$this->withSymfonyMessage(function (Email $email) use ($params) {
$email->embed(static::sgEncode($params), SendgridTransport::REQUEST_BODY_PARAMETER);
});
}
return $this;
Expand Down Expand Up @@ -51,7 +50,7 @@ public static function sgEncode($params)
public static function sgDecode($strParams)
{
if (!is_string($strParams)) {
return (array) $strParams;
return (array)$strParams;
}
$params = json_decode($strParams, true);
return is_array($params) ? $params : [];
Expand Down
4 changes: 2 additions & 2 deletions src/SendgridTransportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class SendgridTransportServiceProvider extends ServiceProvider
{
/**
* Register the Swift Transport instance.
* Register the Sendgrid Transport instance.
*
* @return void
*/
Expand All @@ -23,7 +23,7 @@ public function register()
$config = $this->app['config']->get('services.sendgrid', []);
}
$client = new HttpClient(Arr::get($config, 'guzzle', []));
$endpoint = isset($config['endpoint']) ? $config['endpoint'] : null;
$endpoint = $config['endpoint'] ?? null;

return new SendgridTransport($client, $config['api_key'], $endpoint);
});
Expand Down
Loading