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

Add Sandbox Mode onto SendGridMessage #3

Merged
merged 7 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
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?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('[email protected]', 'App name')
*/
/**
* optionally set the recipient.
* by default it's $notifiable->email:
* ->to('[email protected]', 'Mr. Smith')
*/

->enableSandboxMode(true)
->payload([
"template_var_1" => "template_value_1"
]);
}
}

```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
24 changes: 24 additions & 0 deletions src/SendGridMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
48 changes: 48 additions & 0 deletions tests/SendGridChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,54 @@ public function toSendGrid($notifiable)
$this->assertEquals($message->payload['baz'], 'foo2');
$this->assertEquals($message->replyTo->getEmail(), '[email protected]');
$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('[email protected]', 'Example User')
->to('[email protected]', 'Example User1')
->replyTo('[email protected]', '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(), '[email protected]');
$this->assertEquals($message->from->getName(), 'Example User');
$this->assertEquals($message->tos[0]->getEmail(), '[email protected]');
$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(), '[email protected]');
$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);
Expand Down