Skip to content

Commit 8361e78

Browse files
authored
Merge pull request #5 from swiftmade/dev
Release (2.1.0)
2 parents 97bc67e + 403a616 commit 8361e78

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
pull_request:
88
branches:
99
- master
10+
- dev
1011

1112
jobs:
1213
test:

LICENSE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The MIT License (MIT)
22

3-
Copyright (c) Ahmet Özisik <[email protected]>
3+
Copyright (c) 2022 Swiftmade OÜ <[email protected]>
4+
Copyright (c) 2019 Cuong Giang
45

56
> Permission is hereby granted, free of charge, to any person obtaining a copy
67
> of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ 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+
### Enabling Sandbox Mode
103+
104+
To enable sandbox mode you will need to chain on the `enableSandboxMode()` to the message object.
105+
106+
Example:
107+
108+
```php
109+
return (new SendGridMessage('Your SendGrid template ID'))
110+
->enableSandboxMode()
111+
->payload([
112+
"template_var_1" => "template_value_1"
113+
]);
114+
```
115+
116+
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).
117+
102118
## Changelog
103119

104120
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
@@ -120,7 +136,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
120136
## Credits
121137

122138
- [swiftmade](https://github.com/swiftmade)
123-
- [cuonggt](https://github.com/cuonggt/sendgrid)
139+
- [cuonggt](https://github.com/cuonggt/laravel-sendgrid-notification-channel)
124140
- [All Contributors](../../contributors)
125141

126142
## License

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php": ">=7.2",
1616
"illuminate/notifications": "^7.0|^8.0|^9.0",
1717
"illuminate/support": "^7.0|^8.0|^9.0",
18-
"sendgrid/sendgrid": "^7.11"
18+
"sendgrid/sendgrid": "^7.11|^8.0"
1919
},
2020
"require-dev": {
2121
"friendsofphp/php-cs-fixer": "^3.8",

src/SendGridMessage.php

Lines changed: 26 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 $sandboxMode = false;
51+
4552
/**
4653
* Create a new SendGrid channel instance.
4754
*
@@ -110,10 +117,29 @@ public function build(): Mail
110117

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

120+
if ($this->sandboxMode) {
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+
* Enabling sandbox mode allows you to send a test email to
133+
* ensure that your request body is formatted correctly
134+
* without delivering the email to any of your recipients.
135+
*
136+
* @see https://docs.sendgrid.com/for-developers/sending-email/sandbox-mode
137+
* @return $this
138+
*/
139+
public function enableSandboxMode()
140+
{
141+
$this->sandboxMode = true;
142+
143+
return $this;
144+
}
119145
}

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->sandboxMode, 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();
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->sandboxMode, 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)