Skip to content

Commit defecfa

Browse files
authored
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
1 parent cc8a6d6 commit defecfa

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,57 @@ class ExampleNotification extends Notification
9999

100100
💡 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');`
101101

102+
## Sandbox Mode
103+
104+
To enable sandbox mode you will need to
105+
106+
1. Chain on the `enableSandboxMode(true)` to the `new SendGridMessage('template_id')`
107+
108+
Example:
109+
110+
```php
111+
<?php
112+
113+
namespace App\Notifications;
114+
115+
use Illuminate\Notifications\Notification;
116+
use NotificationChannels\SendGrid\SendGridChannel;
117+
118+
class ExampleNotification extends Notification
119+
{
120+
public function via($notifiable)
121+
{
122+
return [
123+
SendGridChannel::class,
124+
// And any other channels you want can go here...
125+
];
126+
}
127+
128+
// ...
129+
130+
public function toSendGrid($notifiable)
131+
{
132+
return (new SendGridMessage('Your SendGrid template ID'))
133+
/**
134+
* optionally set the from address.
135+
* by default this comes from config/mail.from
136+
* ->from('[email protected]', 'App name')
137+
*/
138+
/**
139+
* optionally set the recipient.
140+
* by default it's $notifiable->email:
141+
* ->to('[email protected]', 'Mr. Smith')
142+
*/
143+
144+
->enableSandboxMode(true)
145+
->payload([
146+
"template_var_1" => "template_value_1"
147+
]);
148+
}
149+
}
150+
151+
```
152+
102153
## Changelog
103154

104155
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/SendGridMessage.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class SendGridMessage
4242
*/
4343
public $payload = [];
4444

45+
/**
46+
* The sandbox mode for SendGrid
47+
*
48+
* @var bool
49+
*/
50+
public $sandbox_mode = false;
51+
4552
/**
4653
* Create a new SendGrid channel instance.
4754
*
@@ -110,10 +117,27 @@ public function build(): Mail
110117

111118
$email->setTemplateId($this->templateId);
112119

120+
if($this->sandbox_mode){
121+
$email->enableSandBoxMode();
122+
}
123+
113124
foreach ($this->payload as $key => $value) {
114125
$email->addDynamicTemplateData((string) $key, (string) $value);
115126
}
116127

117128
return $email;
118129
}
130+
131+
/**
132+
* Set the "sandbox_mode".
133+
*
134+
* @param bool $enabled
135+
* @return $this
136+
*/
137+
public function enableSandboxMode($enabled)
138+
{
139+
$this->sandbox_mode = $enabled;
140+
141+
return $this;
142+
}
119143
}

tests/SendGridChannelTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,54 @@ public function toSendGrid($notifiable)
5656
$this->assertEquals($message->payload['baz'], 'foo2');
5757
$this->assertEquals($message->replyTo->getEmail(), '[email protected]');
5858
$this->assertEquals($message->replyTo->getName(), 'Reply To');
59+
$this->assertEquals($message->sandbox_mode, false);
60+
61+
// TODO: Verify that the Mail instance passed contains all the info from above
62+
$sendgrid->shouldReceive('send')->once()->andReturn($response);
63+
64+
$channel->send($notifiable, $notification);
65+
}
66+
67+
public function testEmailIsNotSentViaSendGridWithSandbox()
68+
{
69+
$notification = new class extends Notification {
70+
public function toSendGrid($notifiable)
71+
{
72+
return (new SendGridMessage('sendgrid-template-id'))
73+
->from('[email protected]', 'Example User')
74+
->to('[email protected]', 'Example User1')
75+
->replyTo('[email protected]', 'Reply To')
76+
->payload([
77+
'bar' => 'foo',
78+
'baz' => 'foo2',
79+
])
80+
->enableSandboxMode(true);
81+
}
82+
};
83+
84+
$notifiable = new class {
85+
use Notifiable;
86+
};
87+
88+
$channel = new SendGridChannel(
89+
$sendgrid = Mockery::mock(new SendGrid('x'))
90+
);
91+
92+
$response = Mockery::mock(Response::class);
93+
$response->shouldReceive('statusCode')->andReturn(200);
94+
95+
$message = $notification->toSendGrid($notifiable);
96+
97+
$this->assertEquals($message->templateId, 'sendgrid-template-id');
98+
$this->assertEquals($message->from->getEmail(), '[email protected]');
99+
$this->assertEquals($message->from->getName(), 'Example User');
100+
$this->assertEquals($message->tos[0]->getEmail(), '[email protected]');
101+
$this->assertEquals($message->tos[0]->getName(), 'Example User1');
102+
$this->assertEquals($message->payload['bar'], 'foo');
103+
$this->assertEquals($message->payload['baz'], 'foo2');
104+
$this->assertEquals($message->replyTo->getEmail(), '[email protected]');
105+
$this->assertEquals($message->replyTo->getName(), 'Reply To');
106+
$this->assertEquals($message->sandbox_mode, true);
59107

60108
// TODO: Verify that the Mail instance passed contains all the info from above
61109
$sendgrid->shouldReceive('send')->once()->andReturn($response);

0 commit comments

Comments
 (0)