Skip to content

Commit

Permalink
Merge pull request #5 from swiftmade/dev
Browse files Browse the repository at this point in the history
Release (2.1.0)
  • Loading branch information
aozisik committed Aug 12, 2022
2 parents 97bc67e + 403a616 commit 8361e78
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
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
3 changes: 2 additions & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# The MIT License (MIT)

Copyright (c) Ahmet Özisik <hello@swiftmade.co>
Copyright (c) 2022 Swiftmade OÜ <hello@swiftmade.co>
Copyright (c) 2019 Cuong Giang

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +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');`

### Enabling Sandbox Mode

To enable sandbox mode you will need to chain on the `enableSandboxMode()` to the message object.

Example:

```php
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 All @@ -120,7 +136,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Credits

- [swiftmade](https://github.com/swiftmade)
- [cuonggt](https://github.com/cuonggt/sendgrid)
- [cuonggt](https://github.com/cuonggt/laravel-sendgrid-notification-channel)
- [All Contributors](../../contributors)

## License
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": ">=7.2",
"illuminate/notifications": "^7.0|^8.0|^9.0",
"illuminate/support": "^7.0|^8.0|^9.0",
"sendgrid/sendgrid": "^7.11"
"sendgrid/sendgrid": "^7.11|^8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.8",
Expand Down
26 changes: 26 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 $sandboxMode = false;

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

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

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

foreach ($this->payload as $key => $value) {
$email->addDynamicTemplateData((string) $key, (string) $value);
}

return $email;
}

/**
* 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.
*
* @see https://docs.sendgrid.com/for-developers/sending-email/sandbox-mode
* @return $this
*/
public function enableSandboxMode()
{
$this->sandboxMode = true;

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

$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->sandboxMode, 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 8361e78

Please sign in to comment.