Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sandbox mode revisions #4

Merged
merged 4 commits into from
Aug 12, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
pull_request:
branches:
- master
- dev

jobs:
test:
Expand Down
53 changes: 9 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\SendGrid\SendGridChannel;

class ExampleNotification extends Notification
{
public function via($notifiable)
{
return [
SendGridChannel::class,
// And any other channels you want can go here...
];
}

// ...

public function toSendGrid($notifiable)
{
return (new SendGridMessage('Your SendGrid template ID'))
/**
* optionally set the from address.
* by default this comes from config/mail.from
* ->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.
Expand Down
14 changes: 8 additions & 6 deletions src/SendGridMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SendGridMessage
*
* @var bool
*/
public $sandbox_mode = false;
public $sandboxMode = false;

/**
* Create a new SendGrid channel instance.
Expand Down Expand Up @@ -117,7 +117,7 @@ public function build(): Mail

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

if($this->sandbox_mode){
if ($this->sandboxMode) {
$email->enableSandBoxMode();
}

Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/SendGridChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -77,7 +77,7 @@ public function toSendGrid($notifiable)
'bar' => 'foo',
'baz' => 'foo2',
])
->enableSandboxMode(true);
->enableSandboxMode();
}
};

Expand All @@ -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);
Expand Down