-
Notifications
You must be signed in to change notification settings - Fork 626
/
Copy pathMail.php
1877 lines (1745 loc) · 61.6 KB
/
Mail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This helper builds the request body for a /mail/send API call
*/
namespace SendGrid\Mail;
use InvalidArgumentException;
use SendGrid\Helper\Assert;
/**
* This class is used to construct a request body for the /mail/send API call
*
* @package SendGrid\Mail
*/
class Mail implements \JsonSerializable
{
/** @var $from From Email address of the sender */
private $from;
/** @var $subject Subject Subject of the email */
private $subject;
/** @var $contents Content[] Content(s) of the email */
private $contents;
/** @var $attachments Attachment[] Email attachments */
private $attachments;
/** @var $template_id TemplateId Id of a template that you would like to use */
private $template_id;
/** @var $sections Section[] Key/value pairs that define block sections of code to be used as substitutions */
private $sections;
/** @var $headers Header[] Header names and the value to substitute for them */
private $headers;
/** @var $categories Category[] Category names for this message */
private $categories;
/**
* @var $custom_args CustomArg[] Values that are specific to the entire send that will be carried along with the
* email and its activity data
*/
private $custom_args;
/**
* @var $substitutions Substitution[] Substitutions that will apply to the text and html content of the body of your
* email, in addition to the subject and reply-to parameters
*/
private $substitutions;
/** @var $send_at SendAt A unix timestamp allowing you to specify when you want your email to be delivered */
private $send_at;
/** @var $batch_id BatchId This ID represents a batch of emails to be sent at the same time */
private $batch_id;
/** @var $asm ASM Specifies how to handle unsubscribes */
private $asm;
/** @var $ip_pool_name IpPoolName The IP Pool that you would like to send this email from */
private $ip_pool_name;
/**
* @var $mail_settings MailSettings A collection of different mail settings that you can use to specify how you
* would like this email to be handled
*/
private $mail_settings;
/**
* @var $tracking_settings TrackingSettings Settings to determine how you would like to track the metrics of how
* your recipients interact with your email
*/
private $tracking_settings;
/** @var $reply_to ReplyTo Email to be use when replied to */
private $reply_to;
/** @var $personalization Personalization[] Messages and their metadata */
private $personalization;
/**
* If passing parameters into this constructor, include $from, $to, $subject,
* $plainTextContent, $htmlContent and $globalSubstitutions at a minimum.
* If you don't supply any, a Personalization object will be created for you.
*
* @param From|null $from Email address of the sender
* @param To|To[]|null $to Recipient(s) email address(es)
* @param Subject|Subject[]|null $subject Subject(s)
* @param PlainTextContent|null $plainTextContent Plain text version of content
* @param HtmlContent|null $htmlContent Html version of content
* @param Substitution[]|array|null $globalSubstitutions Substitutions for entire email
*
* @throws TypeException
*/
public function __construct(
$from = null,
$to = null,
$subject = null,
$plainTextContent = null,
$htmlContent = null,
array $globalSubstitutions = null
) {
if (!isset($from)
&& !isset($to)
&& !isset($subject)
&& !isset($plainTextContent)
&& !isset($htmlContent)
&& !isset($globalSubstitutions)
) {
$this->personalization[] = new Personalization();
return;
}
if (isset($from)) {
$this->setFrom($from);
}
if (isset($to)) {
if (!\is_array($to)) {
$to = [$to];
}
$subjectCount = 0;
foreach ($to as $email) {
if (\is_array($subject) || $email->isPersonalized()) {
$personalization = new Personalization();
$this->addTo($email, null, null, null, $personalization);
} else {
$this->addTo($email);
$personalization = \end($this->personalization);
}
if (\is_array($subject) && $subjectCount < \count($subject)) {
$personalization->setSubject($subject[$subjectCount]);
$subjectCount++;
}
if (\is_array($globalSubstitutions)) {
foreach ($globalSubstitutions as $key => $value) {
if ($value instanceof Substitution) {
$personalization->addSubstitution($value);
} else {
$personalization->addSubstitution($key, $value);
}
}
}
}
}
if (isset($subject) && !\is_array($subject)) {
$this->setSubject($subject);
}
if (isset($plainTextContent)) {
Assert::isInstanceOf($plainTextContent, 'plainTextContent', Content::class);
$this->addContent($plainTextContent);
}
if (isset($htmlContent)) {
Assert::isInstanceOf($htmlContent, 'htmlContent', Content::class);
$this->addContent($htmlContent);
}
}
/**
* Adds a To, Cc or Bcc object to a Personalization object
*
* @param string $emailType Object type name: To, Cc or Bcc
* @param string $email Recipient email address
* @param string|null $name Recipient name
* @param Substitution[]|array|null $substitutions Personalized
* substitutions
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
private function addRecipientEmail(
$emailType,
$email,
$name = null,
$substitutions = null,
$personalizationIndex = null,
$personalization = null
) {
$personalizationFunctionCall = 'add' . $emailType;
$emailTypeClass = '\SendGrid\Mail\\' . $emailType;
if (!($email instanceof $emailTypeClass)) {
$email = new $emailTypeClass(
$email,
$name,
$substitutions
);
}
if ($personalizationIndex === null && $personalization === null
&& $emailType === 'To' && $email->isPersonalized()) {
$personalization = new Personalization();
}
$personalization = $this->getPersonalization($personalizationIndex, $personalization);
$personalization->$personalizationFunctionCall($email);
if ($subs = $email->getSubstitutions()) {
foreach ($subs as $key => $value) {
$personalization->addSubstitution($key, $value);
}
}
if ($email->getSubject()) {
$personalization->setSubject($email->getSubject());
}
}
/**
* Adds an array of To, Cc or Bcc objects to a Personalization object
*
* @param string $emailType Object type name: To, Cc or Bcc
* @param To[]|Cc[]|Bcc[] $emails Array of email recipients
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
private function addRecipientEmails(
$emailType,
$emails,
$personalizationIndex = null,
$personalization = null
) {
$emailFunctionCall = 'add' . $emailType;
if (\current($emails) instanceof EmailAddress) {
foreach ($emails as $email) {
$this->$emailFunctionCall(
$email,
$name = null,
$substitutions = null,
$personalizationIndex,
$personalization
);
}
} else {
foreach ($emails as $email => $name) {
$this->$emailFunctionCall(
$email,
$name,
$substitutions = null,
$personalizationIndex,
$personalization
);
}
}
}
/**
* Add a Personalization object to the Mail object
*
* @param Personalization $personalization A Personalization object
*
* @throws TypeException
*/
public function addPersonalization($personalization)
{
Assert::isInstanceOf($personalization, 'personalization', Personalization::class);
$this->personalization[] = $personalization;
}
/**
* Retrieves a Personalization object, adds a pre-created Personalization
* object, or creates and adds a Personalization object.
*
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @return Personalization
*
* @throws TypeException
*/
public function getPersonalization($personalizationIndex = null, $personalization = null)
{
/**
* Approach:
* - Append if provided personalization + return
* - Return last added if not provided personalizationIndex (create on empty)
* - Return existing personalizationIndex
* - InvalidArgumentException on unexpected personalizationIndex ( > count)
* - Create + add Personalization and return
*/
// If given a Personalization instance
if (null !== $personalization) {
// Just append it onto Mail and return it
$this->addPersonalization($personalization);
return $personalization;
}
// Retrieve count of existing Personalization instances
$personalizationCount = $this->getPersonalizationCount();
// Not providing a personalizationIndex?
if (null === $personalizationIndex) {
// Create new Personalization instance depending on current count
if (0 === $personalizationCount) {
$this->addPersonalization(new Personalization());
}
// Return last added Personalization instance
return end($this->personalization);
}
// Existing personalizationIndex in personalization?
if (isset($this->personalization[$personalizationIndex])) {
// Return referred personalization
return $this->personalization[$personalizationIndex];
}
// Non-existent personalizationIndex given
// Only allow creation of next Personalization if given
// personalizationIndex equals personalizationCount
if (
($personalizationIndex < 0) ||
($personalizationIndex > $personalizationCount)
) {
throw new InvalidArgumentException(
'personalizationIndex ' . $personalizationIndex .
' must be less than ' . $personalizationCount
);
}
// Create new Personalization and return it
$personalization = new Personalization();
$this->addPersonalization($personalization);
return $personalization;
}
/**
* Retrieves Personalization object collection from the Mail object.
*
* @return Personalization[]|null
*/
public function getPersonalizations()
{
return $this->personalization;
}
/**
* Retrieve the number of Personalization objects associated with the Mail object
*
* @return int
*/
public function getPersonalizationCount()
{
return isset($this->personalization) ? \count($this->personalization) : 0;
}
/**
* Adds an email recipient to a Personalization object
*
* @param string|To $to Email address or To object
* @param string $name Recipient name
* @param array|Substitution[] $substitutions Personalized substitutions
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addTo(
$to,
$name = null,
$substitutions = null,
$personalizationIndex = null,
$personalization = null
) {
$this->addRecipientEmail(
'To',
$to,
$name,
$substitutions,
$personalizationIndex,
$personalization
);
}
/**
* Adds multiple email recipients to a Personalization object
*
* @param To[]|array $toEmails Array of To objects or key/value pairs of
* email address/recipient names
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addTos(
$toEmails,
$personalizationIndex = null,
$personalization = null
) {
Assert::minItems($toEmails, 'toEmails', 1);
Assert::maxItems($toEmails, 'toEmails', 1000);
$this->addRecipientEmails(
'To',
$toEmails,
$personalizationIndex,
$personalization
);
}
/**
* Adds an email cc recipient to a Personalization object
*
* @param string|Cc $cc Email address or Cc object
* @param string $name Recipient name
* @param Substitution[]|array|null $substitutions Personalized
* substitutions
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addCc(
$cc,
$name = null,
$substitutions = null,
$personalizationIndex = null,
$personalization = null
) {
$this->addRecipientEmail(
'Cc',
$cc,
$name,
$substitutions,
$personalizationIndex,
$personalization
);
}
/**
* Adds multiple email cc recipients to a Personalization object
*
* @param Cc[]|array $ccEmails Array of Cc objects or key/value pairs of
* email address/recipient names
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addCcs(
$ccEmails,
$personalizationIndex = null,
$personalization = null
) {
Assert::minItems($ccEmails, 'ccEmails', 1);
Assert::maxItems($ccEmails, 'ccEmails', 1000);
$this->addRecipientEmails(
'Cc',
$ccEmails,
$personalizationIndex,
$personalization
);
}
/**
* Adds an email bcc recipient to a Personalization object
*
* @param string|Bcc $bcc Email address or Bcc object
* @param string $name Recipient name
* @param Substitution[]|array|null $substitutions Personalized
* substitutions
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addBcc(
$bcc,
$name = null,
$substitutions = null,
$personalizationIndex = null,
$personalization = null
) {
$this->addRecipientEmail(
'Bcc',
$bcc,
$name,
$substitutions,
$personalizationIndex,
$personalization
);
}
/**
* Adds multiple email bcc recipients to a Personalization object
*
* @param Bcc[]|array $bccEmails Array of Bcc objects or key/value pairs of
* email address/recipient names
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addBccs(
$bccEmails,
$personalizationIndex = null,
$personalization = null
) {
Assert::minItems($bccEmails, 'bccEmails', 1);
Assert::maxItems($bccEmails, 'bccEmails', 1000);
$this->addRecipientEmails(
'Bcc',
$bccEmails,
$personalizationIndex,
$personalization
);
}
/**
* Add a subject to a Personalization or Mail object
*
* If you don't provide a Personalization object or index, the
* subject will be global to entire message. Note that
* subjects added to Personalization objects override
* global subjects.
*
* @param string|Subject $subject Email subject
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function setSubject(
$subject,
$personalizationIndex = null,
$personalization = null
) {
if (!($subject instanceof Subject)) {
$subject = new Subject($subject);
}
if ($personalization !== null) {
$personalization->setSubject($subject);
$this->addPersonalization($personalization);
return;
}
if ($personalizationIndex !== null) {
$this->personalization[$personalizationIndex]->setSubject($subject);
return;
}
$this->setGlobalSubject($subject);
}
/**
* Retrieve a subject attached to a Personalization object
*
* @param int $personalizationIndex Index into the array of existing
* Personalization objects
* @return Subject
*/
public function getSubject($personalizationIndex = 0)
{
return $this->personalization[$personalizationIndex]->getSubject();
}
/**
* Add a header to a Personalization or Mail object
*
* If you don't provide a Personalization object or index, the
* header will be global to entire message. Note that
* headers added to Personalization objects override
* global headers.
*
* @param string|Header $key Key or Header object
* @param string|null $value Value
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addHeader(
$key,
$value = null,
$personalizationIndex = null,
$personalization = null
) {
$header = null;
if ($key instanceof Header) {
$h = $key;
$header = new Header($h->getKey(), $h->getValue());
} else {
$header = new Header($key, $value);
}
$personalization = $this->getPersonalization($personalizationIndex, $personalization);
$personalization->addHeader($header);
}
/**
* Adds multiple headers to a Personalization or Mail object
*
* If you don't provide a Personalization object or index, the
* header will be global to entire message. Note that
* headers added to Personalization objects override
* global headers.
*
* @param array|Header[] $headers Array of Header objects or key values
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addHeaders(
$headers,
$personalizationIndex = null,
$personalization = null
) {
if (\current($headers) instanceof Header) {
foreach ($headers as $header) {
$this->addHeader($header);
}
} else {
foreach ($headers as $key => $value) {
$this->addHeader(
$key,
$value,
$personalizationIndex,
$personalization
);
}
}
}
/**
* Retrieve the headers attached to a Personalization object
*
* @param int $personalizationIndex Index into the array of existing
* Personalization objects
* @return Header[]
*/
public function getHeaders($personalizationIndex = 0)
{
return $this->personalization[$personalizationIndex]->getHeaders();
}
/**
* Add a Substitution object or key/value to a Personalization object
*
* @param Substitution|string $key Substitution object or the key of a
* dynamic data
* @param string|null $value Value
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addDynamicTemplateData(
$key,
$value = null,
$personalizationIndex = null,
$personalization = null
) {
$this->addSubstitution($key, $value, $personalizationIndex, $personalization);
}
/**
* Add a Substitution object or key/value to a Personalization object
*
* @param array|Substitution[] $datas Array of Substitution
* objects or key/values
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addDynamicTemplateDatas(
$datas,
$personalizationIndex = null,
$personalization = null
) {
$this->addSubstitutions($datas, $personalizationIndex, $personalization);
}
/**
* Retrieve dynamic template data key/value pairs from a Personalization object
*
* @param int|0 $personalizationIndex Index into the array of existing
* Personalization objects
* @return array
*/
public function getDynamicTemplateDatas($personalizationIndex = 0)
{
return $this->getSubstitutions($personalizationIndex);
}
/**
* Add a substitution to a Personalization or Mail object
*
* If you don't provide a Personalization object or index, the
* substitution will be global to entire message. Note that
* substitutions added to Personalization objects override
* global substitutions.
*
* @param string|Substitution $key Key or Substitution object
* @param string|null $value Value
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addSubstitution(
$key,
$value = null,
$personalizationIndex = null,
$personalization = null
) {
$substitution = null;
if ($key instanceof Substitution) {
$s = $key;
$substitution = new Substitution($s->getKey(), $s->getValue());
} else {
$substitution = new Substitution($key, $value);
}
$personalization = $this->getPersonalization($personalizationIndex, $personalization);
$personalization->addSubstitution($substitution);
}
/**
* Adds multiple substitutions to a Personalization or Mail object
*
* If you don't provide a Personalization object or index, the
* substitution will be global to entire message. Note that
* substitutions added to Personalization objects override
* global headers.
*
* @param array|Substitution[] $substitutions Array of Substitution
* objects or key/values
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addSubstitutions(
$substitutions,
$personalizationIndex = null,
$personalization = null
) {
if (\current($substitutions) instanceof Substitution) {
foreach ($substitutions as $substitution) {
$this->addSubstitution($substitution);
}
} else {
foreach ($substitutions as $key => $value) {
$this->addSubstitution(
$key,
$value,
$personalizationIndex,
$personalization
);
}
}
}
/**
* Retrieve the substitutions attached to a Personalization object
*
* @param int|0 $personalizationIndex Index into the array of existing
* Personalization objects
* @return Substitution[]
*/
public function getSubstitutions($personalizationIndex = 0)
{
return $this->personalization[$personalizationIndex]->getSubstitutions();
}
/**
* Add a custom arg to a Personalization or Mail object
*
* Note that custom args added to Personalization objects
* override global custom args.
*
* @param string|CustomArg $key Key or CustomArg object
* @param string|null $value Value
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addCustomArg(
$key,
$value = null,
$personalizationIndex = null,
$personalization = null
) {
$custom_arg = null;
if ($key instanceof CustomArg) {
$ca = $key;
$custom_arg = new CustomArg($ca->getKey(), $ca->getValue());
} else {
$custom_arg = new CustomArg($key, $value);
}
$personalization = $this->getPersonalization($personalizationIndex, $personalization);
$personalization->addCustomArg($custom_arg);
}
/**
* Adds multiple custom args to a Personalization or Mail object
*
* If you don't provide a Personalization object or index, the
* custom arg will be global to entire message. Note that
* custom args added to Personalization objects override
* global custom args.
*
* @param array|CustomArg[] $custom_args Array of CustomArg objects or
* key/values
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function addCustomArgs(
$custom_args,
$personalizationIndex = null,
$personalization = null
) {
if (\current($custom_args) instanceof CustomArg) {
foreach ($custom_args as $custom_arg) {
$this->addCustomArg($custom_arg);
}
} else {
foreach ($custom_args as $key => $value) {
$this->addCustomArg(
$key,
$value,
$personalizationIndex,
$personalization
);
}
}
}
/**
* Retrieve the custom args attached to a Personalization object
*
* @param int|0 $personalizationIndex Index into the array of existing
* Personalization objects
* @return CustomArg[]
*/
public function getCustomArgs($personalizationIndex = 0)
{
return $this->personalization[$personalizationIndex]->getCustomArgs();
}
/**
* Add a unix timestamp allowing you to specify when you want your
* email to be delivered to a Personalization or Mail object
*
* If you don't provide a Personalization object or index, the
* send at timestamp will be global to entire message. Note that
* timestamps added to Personalization objects override
* global timestamps.
*
* @param int|SendAt $send_at A unix timestamp
* @param int|null $personalizationIndex Index into the array of existing
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
* @throws TypeException
*/
public function setSendAt(
$send_at,
$personalizationIndex = null,
$personalization = null
) {
if (!($send_at instanceof SendAt)) {
$send_at = new SendAt($send_at);
}
$personalization = $this->getPersonalization($personalizationIndex, $personalization);
$personalization->setSendAt($send_at);
}
/**
* Retrieve the unix timestamp attached to a Personalization object
*
* @param int|0 $personalizationIndex Index into the array of existing
* Personalization objects
* @return SendAt|null
*/
public function getSendAt($personalizationIndex = 0)
{
return $this->personalization[$personalizationIndex]->getSendAt();
}
/**
* Add the sender email address to a Mail object
*
* @param string|From $email Email address or From object
* @param string|null $name Sender name
*
* @throws TypeException
*/
public function setFrom($email, $name = null)
{
if ($email instanceof From) {
$this->from = $email;
} else {
Assert::email(
$email,
'email',
'"$email" must be an instance of SendGrid\Mail\From or a valid email address'
);
$this->from = new From($email, $name);
}
}
/**
* Retrieve the sender attached to a Mail object
*
* @return From
*/
public function getFrom()
{
return $this->from;
}
/**
* Add the reply to email address to a Mail object
*
* @param string|ReplyTo $email Email address or From object
* @param string|null $name Reply to name
*
* @throws TypeException
*/
public function setReplyTo($email, $name = null)
{
if ($email instanceof ReplyTo) {
$this->reply_to = $email;
} else {
$this->reply_to = new ReplyTo($email, $name);
}
}
/**
* Retrieve the reply to information attached to a Mail object
*
* @return ReplyTo
*/
public function getReplyTo()
{
return $this->reply_to;
}
/**
* Add a subject to a Mail object
*
* Note that
* subjects added to Personalization objects override
* global subjects.
*
* @param string|Subject $subject Email subject
*
* @throws TypeException
*/
public function setGlobalSubject($subject)
{
if (!($subject instanceof Subject)) {
$subject = new Subject($subject);
}