Skip to content

Commit 1bb54d1

Browse files
authored
Add customize feature (#15)
* add customize feature * clean up * update github workflow
1 parent 24f1154 commit 1bb54d1

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: run-tests
33
on:
44
push:
55
branches:
6-
- master
6+
- main
77
pull_request:
88
branches:
9-
- master
9+
- main
1010
- dev
1111

1212
jobs:

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,33 @@ You can attach or embed (inline attachment) files to your messages. `SendGridMes
135135
- 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'])`)
136136
- `embed` and `embedData` methods will return the ContentID with `cid:` in front (e.g. `embed('avatar.jpg') -> "cid:avatar.jpg"`).
137137

138+
### Full Access to the Sendgrid Mail Object
139+
140+
If you need more customization options, you can work directly with the underlying Sendgrid Mail object.
141+
To utilize this, simply pass a callback using the `customize` method.
142+
143+
```php
144+
use SendGrid\Mail\Mail;
145+
146+
return (new SendGridMessage('Your SendGrid template ID'))
147+
->payload([
148+
'template_var_1' => 'template_value_1',
149+
'template_var_2' => [
150+
'value_1',
151+
'value_2',
152+
'value_3',
153+
],
154+
])
155+
->customize(function (Mail $mail) {
156+
// Send a carbon copy (cc) to another address
157+
$mail->addCc('test@test.com');
158+
// Send a blind carbon copy (bcc) to another address
159+
$mail->addBcc('bcc@test.com');
160+
});
161+
```
162+
163+
For all the options, you can see Sendgrid's Mail class [here](https://github.com/sendgrid/sendgrid-php/blob/main/lib/mail/Mail.php).
164+
138165
## Changelog
139166

140167
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/SendGridMessage.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SendGridMessage
2020
public $from;
2121

2222
/**
23-
* The "tos" for the message.
23+
* Recipients of the message.
2424
*
2525
* @var array
2626
*/
@@ -59,6 +59,13 @@ class SendGridMessage
5959
*/
6060
public $sandboxMode = false;
6161

62+
/**
63+
* The customizations callbacks for SendGrid Mail object.
64+
*
65+
* @var array
66+
*/
67+
private $customizeCallbacks = [];
68+
6269
/**
6370
* Create a new SendGrid channel instance.
6471
*
@@ -248,6 +255,11 @@ public function build(): Mail
248255
$email->addAttachment($attachment);
249256
}
250257

258+
if (count($this->customizeCallbacks)) {
259+
foreach ($this->customizeCallbacks as $callback) {
260+
$callback($email);
261+
}
262+
}
251263

252264
return $email;
253265
}
@@ -274,4 +286,15 @@ public function enableSandboxMode()
274286
{
275287
return $this->setSandboxMode(true);
276288
}
289+
290+
/**
291+
* Pass a callback that will be called with the SendGrid message
292+
* before it is sent. This allows you to fully customize the message using the SendGrid library's API.
293+
*/
294+
public function customize($callback)
295+
{
296+
$this->customizeCallbacks[] = $callback;
297+
298+
return $this;
299+
}
277300
}

tests/SendGridMessageTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,17 @@ public function testEmbeddingFromData()
164164
$this->assertEquals('inline', $attachment->getDisposition());
165165
$this->assertEquals('blank.png', $attachment->getContentID());
166166
}
167+
168+
public function testCustomizeCallback()
169+
{
170+
$message = new SendGridMessage('template-id');
171+
$mailRef = null;
172+
173+
$message->customize(function ($mail) use (&$mailRef) {
174+
$mailRef = $mail;
175+
});
176+
177+
$message->build();
178+
$this->assertInstanceOf(\SendGrid\Mail\Mail::class, $mailRef);
179+
}
167180
}

0 commit comments

Comments
 (0)