Skip to content

Commit

Permalink
Add Sandbox Mode onto SendGridMessage (#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zbrody committed Aug 12, 2022
1 parent cc8a6d6 commit defecfa
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
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('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.
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(), '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);
Expand Down

0 comments on commit defecfa

Please sign in to comment.