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
3
3
on :
4
4
push :
5
5
branches :
6
- - master
6
+ - main
7
7
pull_request :
8
8
branches :
9
- - master
9
+ - main
10
10
- dev
11
11
12
12
jobs :
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
135
135
- 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']) ` )
136
136
- ` embed ` and ` embedData ` methods will return the ContentID with ` cid: ` in front (e.g. ` embed('avatar.jpg') -> "cid:avatar.jpg" ` ).
137
137
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
+
138
165
## Changelog
139
166
140
167
Please 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
20
20
public $ from ;
21
21
22
22
/**
23
- * The "tos" for the message.
23
+ * Recipients of the message.
24
24
*
25
25
* @var array
26
26
*/
@@ -59,6 +59,13 @@ class SendGridMessage
59
59
*/
60
60
public $ sandboxMode = false ;
61
61
62
+ /**
63
+ * The customizations callbacks for SendGrid Mail object.
64
+ *
65
+ * @var array
66
+ */
67
+ private $ customizeCallbacks = [];
68
+
62
69
/**
63
70
* Create a new SendGrid channel instance.
64
71
*
@@ -248,6 +255,11 @@ public function build(): Mail
248
255
$ email ->addAttachment ($ attachment );
249
256
}
250
257
258
+ if (count ($ this ->customizeCallbacks )) {
259
+ foreach ($ this ->customizeCallbacks as $ callback ) {
260
+ $ callback ($ email );
261
+ }
262
+ }
251
263
252
264
return $ email ;
253
265
}
@@ -274,4 +286,15 @@ public function enableSandboxMode()
274
286
{
275
287
return $ this ->setSandboxMode (true );
276
288
}
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
+ }
277
300
}
Original file line number Diff line number Diff line change @@ -164,4 +164,17 @@ public function testEmbeddingFromData()
164
164
$ this ->assertEquals ('inline ' , $ attachment ->getDisposition ());
165
165
$ this ->assertEquals ('blank.png ' , $ attachment ->getContentID ());
166
166
}
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
+ }
167
180
}
You can’t perform that action at this time.
0 commit comments