From f20ede12672b6e38e6d78b04c05a6175f1139441 Mon Sep 17 00:00:00 2001 From: Ahmet Ozisik Date: Fri, 12 Aug 2022 16:36:07 +0300 Subject: [PATCH 1/4] support sendgrid-php ^8.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6a3c1c8..b41a3e5 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "php": ">=7.2", "illuminate/notifications": "^7.0|^8.0|^9.0", "illuminate/support": "^7.0|^8.0|^9.0", - "sendgrid/sendgrid": "^7.11" + "sendgrid/sendgrid": "^7.11|^8.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.8", From cc8a6d6b3c2c723be94d46139d4e3480e31a8ec1 Mon Sep 17 00:00:00 2001 From: Ahmet Ozisik Date: Fri, 12 Aug 2022 16:40:45 +0300 Subject: [PATCH 2/4] update license and attribution to original work --- LICENSE.md | 3 ++- README.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 60870f7..23d4bd5 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,7 @@ # The MIT License (MIT) -Copyright (c) Ahmet Özisik +Copyright (c) 2022 Swiftmade OÜ +Copyright (c) 2019 Cuong Giang > Permission is hereby granted, free of charge, to any person obtaining a copy > of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 70fc12d..f63d085 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ## Credits - [swiftmade](https://github.com/swiftmade) -- [cuonggt](https://github.com/cuonggt/sendgrid) +- [cuonggt](https://github.com/cuonggt/laravel-sendgrid-notification-channel) - [All Contributors](../../contributors) ## License From defecfa1ab006b77cbd0f1b96bbb8edb76bcdf98 Mon Sep 17 00:00:00 2001 From: zbrody <102783598+zbrody@users.noreply.github.com> Date: Fri, 12 Aug 2022 15:54:58 +0200 Subject: [PATCH 3/4] Add Sandbox Mode onto SendGridMessage (#3) * feat: Add sandbox to SendGrideMessage * feat: Add sandbox to SendGrideMessage Test * feat: Updated read me for new implementation * feat: Updated test to include for sandboxmode of true and updated read me * feat: updated readme * refactor: updated changes to match conventions * refactor: updated changes to match conventions --- README.md | 51 +++++++++++++++++++++++++++++++++++ src/SendGridMessage.php | 24 +++++++++++++++++ tests/SendGridChannelTest.php | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) diff --git a/README.md b/README.md index f63d085..342f4df 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,57 @@ class ExampleNotification extends Notification 💡 Unless you set it explicitly, the **From** address will be set to `config('mail.from.address')` and the **To** value will be what returns from `$notifiable->routeNotificationFor('mail');` +## Sandbox Mode + +To enable sandbox mode you will need to + +1. Chain on the `enableSandboxMode(true)` to the `new SendGridMessage('template_id')` + +Example: + +```php +from('no-reply@test.com', 'App name') + */ + /** + * optionally set the recipient. + * by default it's $notifiable->email: + * ->to('hello@example.com', 'Mr. Smith') + */ + + ->enableSandboxMode(true) + ->payload([ + "template_var_1" => "template_value_1" + ]); + } +} + +``` + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/src/SendGridMessage.php b/src/SendGridMessage.php index 9c7f7b4..6ee2e45 100644 --- a/src/SendGridMessage.php +++ b/src/SendGridMessage.php @@ -42,6 +42,13 @@ class SendGridMessage */ public $payload = []; + /** + * The sandbox mode for SendGrid + * + * @var bool + */ + public $sandbox_mode = false; + /** * Create a new SendGrid channel instance. * @@ -110,10 +117,27 @@ public function build(): Mail $email->setTemplateId($this->templateId); + if($this->sandbox_mode){ + $email->enableSandBoxMode(); + } + foreach ($this->payload as $key => $value) { $email->addDynamicTemplateData((string) $key, (string) $value); } return $email; } + + /** + * Set the "sandbox_mode". + * + * @param bool $enabled + * @return $this + */ + public function enableSandboxMode($enabled) + { + $this->sandbox_mode = $enabled; + + return $this; + } } diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index b673ae8..e3dd906 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -56,6 +56,54 @@ public function toSendGrid($notifiable) $this->assertEquals($message->payload['baz'], 'foo2'); $this->assertEquals($message->replyTo->getEmail(), 'replyto@example.com'); $this->assertEquals($message->replyTo->getName(), 'Reply To'); + $this->assertEquals($message->sandbox_mode, false); + + // TODO: Verify that the Mail instance passed contains all the info from above + $sendgrid->shouldReceive('send')->once()->andReturn($response); + + $channel->send($notifiable, $notification); + } + + public function testEmailIsNotSentViaSendGridWithSandbox() + { + $notification = new class extends Notification { + public function toSendGrid($notifiable) + { + return (new SendGridMessage('sendgrid-template-id')) + ->from('test@example.com', 'Example User') + ->to('test+test1@example.com', 'Example User1') + ->replyTo('replyto@example.com', 'Reply To') + ->payload([ + 'bar' => 'foo', + 'baz' => 'foo2', + ]) + ->enableSandboxMode(true); + } + }; + + $notifiable = new class { + use Notifiable; + }; + + $channel = new SendGridChannel( + $sendgrid = Mockery::mock(new SendGrid('x')) + ); + + $response = Mockery::mock(Response::class); + $response->shouldReceive('statusCode')->andReturn(200); + + $message = $notification->toSendGrid($notifiable); + + $this->assertEquals($message->templateId, 'sendgrid-template-id'); + $this->assertEquals($message->from->getEmail(), 'test@example.com'); + $this->assertEquals($message->from->getName(), 'Example User'); + $this->assertEquals($message->tos[0]->getEmail(), 'test+test1@example.com'); + $this->assertEquals($message->tos[0]->getName(), 'Example User1'); + $this->assertEquals($message->payload['bar'], 'foo'); + $this->assertEquals($message->payload['baz'], 'foo2'); + $this->assertEquals($message->replyTo->getEmail(), 'replyto@example.com'); + $this->assertEquals($message->replyTo->getName(), 'Reply To'); + $this->assertEquals($message->sandbox_mode, true); // TODO: Verify that the Mail instance passed contains all the info from above $sendgrid->shouldReceive('send')->once()->andReturn($response); From 403a6167f15bf3f58876de17310693c70fac5f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=96z=C4=B1=C5=9F=C4=B1k?= Date: Fri, 12 Aug 2022 17:06:18 +0300 Subject: [PATCH 4/4] Sandbox mode revisions (#4) * rename sandbox_mode to sandboxMode * update readme * change method signature * run actions on PRs to dev --- .github/workflows/main.yml | 1 + README.md | 53 ++++++----------------------------- src/SendGridMessage.php | 14 +++++---- tests/SendGridChannelTest.php | 6 ++-- 4 files changed, 21 insertions(+), 53 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1ca1c7a..ea0d9f7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,6 +7,7 @@ on: pull_request: branches: - master + - dev jobs: test: diff --git a/README.md b/README.md index 342f4df..4c534a5 100644 --- a/README.md +++ b/README.md @@ -99,57 +99,22 @@ class ExampleNotification extends Notification 💡 Unless you set it explicitly, the **From** address will be set to `config('mail.from.address')` and the **To** value will be what returns from `$notifiable->routeNotificationFor('mail');` -## Sandbox Mode +### Enabling Sandbox Mode -To enable sandbox mode you will need to - -1. Chain on the `enableSandboxMode(true)` to the `new SendGridMessage('template_id')` +To enable sandbox mode you will need to chain on the `enableSandboxMode()` to the message object. Example: ```php -from('no-reply@test.com', 'App name') - */ - /** - * optionally set the recipient. - * by default it's $notifiable->email: - * ->to('hello@example.com', 'Mr. Smith') - */ - - ->enableSandboxMode(true) - ->payload([ - "template_var_1" => "template_value_1" - ]); - } -} - +return (new SendGridMessage('Your SendGrid template ID')) + ->enableSandboxMode() + ->payload([ + "template_var_1" => "template_value_1" + ]); ``` +When making a request with sandbox mode enabled, Sendgrid will validate the form, type, and shape of your request. No email will be sent. You can read more about the sandbox mode [here](https://docs.sendgrid.com/for-developers/sending-email/sandbox-mode). + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/src/SendGridMessage.php b/src/SendGridMessage.php index 6ee2e45..188e013 100644 --- a/src/SendGridMessage.php +++ b/src/SendGridMessage.php @@ -47,7 +47,7 @@ class SendGridMessage * * @var bool */ - public $sandbox_mode = false; + public $sandboxMode = false; /** * Create a new SendGrid channel instance. @@ -117,7 +117,7 @@ public function build(): Mail $email->setTemplateId($this->templateId); - if($this->sandbox_mode){ + if ($this->sandboxMode) { $email->enableSandBoxMode(); } @@ -129,14 +129,16 @@ public function build(): Mail } /** - * Set the "sandbox_mode". + * Enabling sandbox mode allows you to send a test email to + * ensure that your request body is formatted correctly + * without delivering the email to any of your recipients. * - * @param bool $enabled + * @see https://docs.sendgrid.com/for-developers/sending-email/sandbox-mode * @return $this */ - public function enableSandboxMode($enabled) + public function enableSandboxMode() { - $this->sandbox_mode = $enabled; + $this->sandboxMode = true; return $this; } diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index e3dd906..2314652 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -56,7 +56,7 @@ public function toSendGrid($notifiable) $this->assertEquals($message->payload['baz'], 'foo2'); $this->assertEquals($message->replyTo->getEmail(), 'replyto@example.com'); $this->assertEquals($message->replyTo->getName(), 'Reply To'); - $this->assertEquals($message->sandbox_mode, false); + $this->assertEquals($message->sandboxMode, false); // TODO: Verify that the Mail instance passed contains all the info from above $sendgrid->shouldReceive('send')->once()->andReturn($response); @@ -77,7 +77,7 @@ public function toSendGrid($notifiable) 'bar' => 'foo', 'baz' => 'foo2', ]) - ->enableSandboxMode(true); + ->enableSandboxMode(); } }; @@ -103,7 +103,7 @@ public function toSendGrid($notifiable) $this->assertEquals($message->payload['baz'], 'foo2'); $this->assertEquals($message->replyTo->getEmail(), 'replyto@example.com'); $this->assertEquals($message->replyTo->getName(), 'Reply To'); - $this->assertEquals($message->sandbox_mode, true); + $this->assertEquals($message->sandboxMode, true); // TODO: Verify that the Mail instance passed contains all the info from above $sendgrid->shouldReceive('send')->once()->andReturn($response);