Skip to content

Commit

Permalink
Add customize feature (#15)
Browse files Browse the repository at this point in the history
* add customize feature

* clean up

* update github workflow
  • Loading branch information
aozisik authored Mar 22, 2023
1 parent 24f1154 commit 1bb54d1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: run-tests
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main
- dev

jobs:
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ You can attach or embed (inline attachment) files to your messages. `SendGridMes
- You can use the `as` key in the options to change the filename to appears in the email. (e.g. `attach($file, ['as' => 'invoice-3252.pdf'])`)
- `embed` and `embedData` methods will return the ContentID with `cid:` in front (e.g. `embed('avatar.jpg') -> "cid:avatar.jpg"`).

### Full Access to the Sendgrid Mail Object

If you need more customization options, you can work directly with the underlying Sendgrid Mail object.
To utilize this, simply pass a callback using the `customize` method.

```php
use SendGrid\Mail\Mail;

return (new SendGridMessage('Your SendGrid template ID'))
->payload([
'template_var_1' => 'template_value_1',
'template_var_2' => [
'value_1',
'value_2',
'value_3',
],
])
->customize(function (Mail $mail) {
// Send a carbon copy (cc) to another address
$mail->addCc('[email protected]');
// Send a blind carbon copy (bcc) to another address
$mail->addBcc('[email protected]');
});
```

For all the options, you can see Sendgrid's Mail class [here](https://github.com/sendgrid/sendgrid-php/blob/main/lib/mail/Mail.php).

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
25 changes: 24 additions & 1 deletion src/SendGridMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SendGridMessage
public $from;

/**
* The "tos" for the message.
* Recipients of the message.
*
* @var array
*/
Expand Down Expand Up @@ -59,6 +59,13 @@ class SendGridMessage
*/
public $sandboxMode = false;

/**
* The customizations callbacks for SendGrid Mail object.
*
* @var array
*/
private $customizeCallbacks = [];

/**
* Create a new SendGrid channel instance.
*
Expand Down Expand Up @@ -248,6 +255,11 @@ public function build(): Mail
$email->addAttachment($attachment);
}

if (count($this->customizeCallbacks)) {
foreach ($this->customizeCallbacks as $callback) {
$callback($email);
}
}

return $email;
}
Expand All @@ -274,4 +286,15 @@ public function enableSandboxMode()
{
return $this->setSandboxMode(true);
}

/**
* Pass a callback that will be called with the SendGrid message
* before it is sent. This allows you to fully customize the message using the SendGrid library's API.
*/
public function customize($callback)
{
$this->customizeCallbacks[] = $callback;

return $this;
}
}
13 changes: 13 additions & 0 deletions tests/SendGridMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,17 @@ public function testEmbeddingFromData()
$this->assertEquals('inline', $attachment->getDisposition());
$this->assertEquals('blank.png', $attachment->getContentID());
}

public function testCustomizeCallback()
{
$message = new SendGridMessage('template-id');
$mailRef = null;

$message->customize(function ($mail) use (&$mailRef) {
$mailRef = $mail;
});

$message->build();
$this->assertInstanceOf(\SendGrid\Mail\Mail::class, $mailRef);
}
}

0 comments on commit 1bb54d1

Please sign in to comment.