File tree Expand file tree Collapse file tree 4 files changed +66
-3
lines changed Expand file tree Collapse file tree 4 files changed +66
-3
lines changed Original file line number Diff line number Diff line change @@ -3,10 +3,10 @@ name: run-tests
33on :
44 push :
55 branches :
6- - master
6+ - main
77 pull_request :
88 branches :
9- - master
9+ - main
1010 - dev
1111
1212jobs :
Original file line number Diff line number Diff 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
140167Please see [ CHANGELOG] ( CHANGELOG.md ) for more information what has changed recently.
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments