Mail::enableBypassSpamManagement()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * This helper builds the request body for a /mail/send API call
4
 */
5
6
namespace SendGrid\Mail;
7
8
use InvalidArgumentException;
9
use SendGrid\Helper\Assert;
10
11
/**
12
 * This class is used to construct a request body for the /mail/send API call
13
 *
14
 * @package SendGrid\Mail
15
 */
16
class Mail implements \JsonSerializable
17
{
18
    /** @var $from From Email address of the sender */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $from at position 0 could not be parsed: Unknown type name '$from' at position 0 in $from.
Loading history...
19
    private $from;
20
21
    /** @var $subject Subject Subject of the email */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $subject at position 0 could not be parsed: Unknown type name '$subject' at position 0 in $subject.
Loading history...
22
    private $subject;
23
24
    /** @var $contents Content[] Content(s) of the email */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $contents at position 0 could not be parsed: Unknown type name '$contents' at position 0 in $contents.
Loading history...
25
    private $contents;
26
27
    /** @var $attachments Attachment[] Email attachments */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $attachments at position 0 could not be parsed: Unknown type name '$attachments' at position 0 in $attachments.
Loading history...
28
    private $attachments;
29
30
    /** @var $template_id TemplateId Id of a template that you would like to use */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $template_id at position 0 could not be parsed: Unknown type name '$template_id' at position 0 in $template_id.
Loading history...
31
    private $template_id;
32
33
    /** @var $sections Section[] Key/value pairs that define block sections of code to be used as substitutions */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $sections at position 0 could not be parsed: Unknown type name '$sections' at position 0 in $sections.
Loading history...
34
    private $sections;
35
36
    /** @var $headers Header[] Header names and the value to substitute for them */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $headers at position 0 could not be parsed: Unknown type name '$headers' at position 0 in $headers.
Loading history...
37
    private $headers;
38
39
    /** @var $categories Category[] Category names for this message */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $categories at position 0 could not be parsed: Unknown type name '$categories' at position 0 in $categories.
Loading history...
40
    private $categories;
41
42
    /**
43
     * @var $custom_args CustomArg[] Values that are specific to the entire send that will be carried along with the
0 ignored issues
show
Documentation Bug introduced by
The doc comment $custom_args at position 0 could not be parsed: Unknown type name '$custom_args' at position 0 in $custom_args.
Loading history...
44
     *                               email and its activity data
45
     */
46
    private $custom_args;
47
48
    /**
49
     * @var $substitutions Substitution[] Substitutions that will apply to the text and html content of the body of your
0 ignored issues
show
Documentation Bug introduced by
The doc comment $substitutions at position 0 could not be parsed: Unknown type name '$substitutions' at position 0 in $substitutions.
Loading history...
50
     *                                    email, in addition to the subject and reply-to parameters
51
     */
52
    private $substitutions;
53
54
    /** @var $send_at SendAt A unix timestamp allowing you to specify when you want your email to be delivered */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $send_at at position 0 could not be parsed: Unknown type name '$send_at' at position 0 in $send_at.
Loading history...
55
    private $send_at;
56
57
    /** @var $batch_id BatchId This ID represents a batch of emails to be sent at the same time */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $batch_id at position 0 could not be parsed: Unknown type name '$batch_id' at position 0 in $batch_id.
Loading history...
58
    private $batch_id;
59
60
    /** @var $asm ASM Specifies how to handle unsubscribes */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $asm at position 0 could not be parsed: Unknown type name '$asm' at position 0 in $asm.
Loading history...
61
    private $asm;
62
63
    /** @var $ip_pool_name IpPoolName The IP Pool that you would like to send this email from */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $ip_pool_name at position 0 could not be parsed: Unknown type name '$ip_pool_name' at position 0 in $ip_pool_name.
Loading history...
64
    private $ip_pool_name;
65
66
    /**
67
     * @var $mail_settings MailSettings A collection of different mail settings that you can use to specify how you
0 ignored issues
show
Documentation Bug introduced by
The doc comment $mail_settings at position 0 could not be parsed: Unknown type name '$mail_settings' at position 0 in $mail_settings.
Loading history...
68
     *                                  would like this email to be handled
69
     */
70
    private $mail_settings;
71
72
    /**
73
     * @var $tracking_settings TrackingSettings Settings to determine how you would like to track the metrics of how
0 ignored issues
show
Documentation Bug introduced by
The doc comment $tracking_settings at position 0 could not be parsed: Unknown type name '$tracking_settings' at position 0 in $tracking_settings.
Loading history...
74
     *                                          your recipients interact with your email
75
     */
76
    private $tracking_settings;
77
78
    /** @var $reply_to ReplyTo Email to be use when replied to */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $reply_to at position 0 could not be parsed: Unknown type name '$reply_to' at position 0 in $reply_to.
Loading history...
79
    private $reply_to;
80
81
    /** @var $personalization Personalization[] Messages and their metadata */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $personalization at position 0 could not be parsed: Unknown type name '$personalization' at position 0 in $personalization.
Loading history...
82
    private $personalization;
83
84
    /**
85
     * If passing parameters into this constructor, include $from, $to, $subject,
86
     * $plainTextContent, $htmlContent and $globalSubstitutions at a minimum.
87
     * If you don't supply any, a Personalization object will be created for you.
88
     *
89
     * @param From|null                 $from                Email address of the sender
90
     * @param To|To[]|null              $to                  Recipient(s) email address(es)
91
     * @param Subject|Subject[]|null    $subject             Subject(s)
92
     * @param PlainTextContent|null     $plainTextContent    Plain text version of content
93
     * @param HtmlContent|null          $htmlContent         Html version of content
94
     * @param Substitution[]|array|null $globalSubstitutions Substitutions for entire email
95
     *
96
     * @throws TypeException
97
     */
98
    public function __construct(
99
        $from = null,
100
        $to = null,
101
        $subject = null,
102
        $plainTextContent = null,
103
        $htmlContent = null,
104
        array $globalSubstitutions = null
105
    ) {
106
        if (!isset($from)
107
            && !isset($to)
108
            && !isset($subject)
109
            && !isset($plainTextContent)
110
            && !isset($htmlContent)
111
            && !isset($globalSubstitutions)
112
        ) {
113
            $this->personalization[] = new Personalization();
114
            return;
115
        }
116
        if (isset($from)) {
117
            $this->setFrom($from);
118
        }
119
        if (isset($to)) {
120
            if (!\is_array($to)) {
121
                $to = [$to];
122
            }
123
            $subjectCount = 0;
124
            foreach ($to as $email) {
125
                if (\is_array($subject) || $email->isPersonalized()) {
126
                    $personalization = new Personalization();
127
                    $this->addTo($email, null, null, null, $personalization);
128
                } else {
129
                    $this->addTo($email);
130
                    $personalization = \end($this->personalization);
131
                }
132
133
                if (\is_array($subject) && $subjectCount < \count($subject)) {
134
                    $personalization->setSubject($subject[$subjectCount]);
135
                    $subjectCount++;
136
                }
137
138
                if (\is_array($globalSubstitutions)) {
139
                    foreach ($globalSubstitutions as $key => $value) {
140
                        if ($value instanceof Substitution) {
141
                            $personalization->addSubstitution($value);
142
                        } else {
143
                            $personalization->addSubstitution($key, $value);
144
                        }
145
                    }
146
                }
147
            }
148
        }
149
        if (isset($subject) && !\is_array($subject)) {
150
            $this->setSubject($subject);
151
        }
152
        if (isset($plainTextContent)) {
153
            Assert::isInstanceOf($plainTextContent, 'plainTextContent', Content::class);
154
155
            $this->addContent($plainTextContent);
156
        }
157
        if (isset($htmlContent)) {
158
            Assert::isInstanceOf($htmlContent, 'htmlContent', Content::class);
159
160
            $this->addContent($htmlContent);
161
        }
162
    }
163
164
    /**
165
     * Adds a To, Cc or Bcc object to a Personalization object
166
     *
167
     * @param string                    $emailType            Object type name: To, Cc or Bcc
168
     * @param string                    $email                Recipient email address
169
     * @param string|null               $name                 Recipient name
170
     * @param Substitution[]|array|null $substitutions        Personalized
171
     *                                                        substitutions
172
     * @param int|null                  $personalizationIndex Index into the array of existing
173
     *                                                        Personalization objects
174
     * @param Personalization|null      $personalization      A pre-created
175
     *                                                        Personalization object
176
     *
177
     * @throws TypeException
178
     */
179
    private function addRecipientEmail(
180
        $emailType,
181
        $email,
182
        $name = null,
183
        $substitutions = null,
184
        $personalizationIndex = null,
185
        $personalization = null
186
    ) {
187
        $personalizationFunctionCall = 'add' . $emailType;
188
        $emailTypeClass = '\SendGrid\Mail\\' . $emailType;
189
        if (!($email instanceof $emailTypeClass)) {
190
            $email = new $emailTypeClass(
191
                $email,
192
                $name,
193
                $substitutions
194
            );
195
        }
196
197
        if ($personalizationIndex === null && $personalization === null
198
            && $emailType === 'To' && $email->isPersonalized()) {
199
            $personalization = new Personalization();
200
        }
201
202
        $personalization = $this->getPersonalization($personalizationIndex, $personalization);
203
        $personalization->$personalizationFunctionCall($email);
204
205
        if ($subs = $email->getSubstitutions()) {
206
            foreach ($subs as $key => $value) {
207
                $personalization->addSubstitution($key, $value);
208
            }
209
        }
210
211
        if ($email->getSubject()) {
212
            $personalization->setSubject($email->getSubject());
213
        }
214
    }
215
216
    /**
217
     * Adds an array of To, Cc or Bcc objects to a Personalization object
218
     *
219
     * @param string               $emailType            Object type name: To, Cc  or Bcc
220
     * @param To[]|Cc[]|Bcc[]      $emails               Array of email recipients
221
     * @param int|null             $personalizationIndex Index into the array of existing
222
     *                                                   Personalization objects
223
     * @param Personalization|null $personalization      A pre-created
224
     *                                                   Personalization object
225
     *
226
     * @throws TypeException
227
     */
228
    private function addRecipientEmails(
229
        $emailType,
230
        $emails,
231
        $personalizationIndex = null,
232
        $personalization = null
233
    ) {
234
        $emailFunctionCall = 'add' . $emailType;
235
236
        if (\current($emails) instanceof EmailAddress) {
237
            foreach ($emails as $email) {
238
                $this->$emailFunctionCall(
239
                    $email,
240
                    $name = null,
241
                    $substitutions = null,
242
                    $personalizationIndex,
243
                    $personalization
244
                );
245
            }
246
        } else {
247
            foreach ($emails as $email => $name) {
248
                $this->$emailFunctionCall(
249
                    $email,
250
                    $name,
251
                    $substitutions = null,
252
                    $personalizationIndex,
253
                    $personalization
254
                );
255
            }
256
        }
257
    }
258
259
    /**
260
     * Add a Personalization object to the Mail object
261
     *
262
     * @param Personalization $personalization A Personalization object
263
     *
264
     * @throws TypeException
265
     */
266
    public function addPersonalization($personalization)
267
    {
268
        Assert::isInstanceOf($personalization, 'personalization', Personalization::class);
269
270
        $this->personalization[] = $personalization;
271
    }
272
273
    /**
274
     * Retrieves a Personalization object, adds a pre-created Personalization
275
     * object, or creates and adds a Personalization object.
276
     *
277
     * @param int|null             $personalizationIndex Index into the array of existing
278
     *                                                   Personalization objects
279
     * @param Personalization|null $personalization      A pre-created
280
     *                                                   Personalization object
281
     *
282
     * @return Personalization
283
     *
284
     * @throws TypeException
285
     */
286
    public function getPersonalization($personalizationIndex = null, $personalization = null)
287
    {
288
        /**
289
         * Approach:
290
         * - Append if provided personalization + return
291
         * - Return last added if not provided personalizationIndex (create on empty)
292
         * - Return existing personalizationIndex
293
         * - InvalidArgumentException on unexpected personalizationIndex ( > count)
294
         * - Create + add Personalization and return
295
         */
296
297
        //  If given a Personalization instance
298
        if (null !== $personalization) {
299
            //  Just append it onto Mail and return it
300
            $this->addPersonalization($personalization);
301
            return $personalization;
302
        }
303
304
        //  Retrieve count of existing Personalization instances
305
        $personalizationCount = $this->getPersonalizationCount();
306
307
        //  Not providing a personalizationIndex?
308
        if (null === $personalizationIndex) {
309
            //  Create new Personalization instance depending on current count
310
            if (0 === $personalizationCount) {
311
                $this->addPersonalization(new Personalization());
312
            }
313
314
            //  Return last added Personalization instance
315
            return end($this->personalization);
316
        }
317
318
        //  Existing personalizationIndex in personalization?
319
        if (isset($this->personalization[$personalizationIndex])) {
320
            //  Return referred personalization
321
            return $this->personalization[$personalizationIndex];
322
        }
323
324
        //  Non-existent personalizationIndex given
325
        //  Only allow creation of next Personalization if given
326
        //  personalizationIndex equals personalizationCount
327
        if (
328
            ($personalizationIndex < 0) ||
329
            ($personalizationIndex > $personalizationCount)
330
        ) {
331
            throw new InvalidArgumentException(
332
                'personalizationIndex ' . $personalizationIndex .
333
                ' must be less than ' . $personalizationCount
334
            );
335
        }
336
337
        //  Create new Personalization and return it
338
        $personalization = new Personalization();
339
        $this->addPersonalization($personalization);
340
        return $personalization;
341
    }
342
343
    /**
344
     * Retrieves Personalization object collection from the Mail object.
345
     *
346
     * @return Personalization[]|null
347
     */
348
    public function getPersonalizations()
349
    {
350
        return $this->personalization;
351
    }
352
353
    /**
354
     * Retrieve the number of Personalization objects associated with the Mail object
355
     *
356
     * @return int
357
     */
358
    public function getPersonalizationCount()
359
    {
360
        return isset($this->personalization) ? \count($this->personalization) : 0;
361
    }
362
363
    /**
364
     * Adds an email recipient to a Personalization object
365
     *
366
     * @param string|To            $to                   Email address or To object
367
     * @param string               $name                 Recipient name
368
     * @param array|Substitution[] $substitutions        Personalized substitutions
369
     * @param int|null             $personalizationIndex Index into the array of existing
370
     *                                                   Personalization objects
371
     * @param Personalization|null $personalization      A pre-created
372
     *                                                   Personalization object
373
     *
374
     * @throws TypeException
375
     */
376
    public function addTo(
377
        $to,
378
        $name = null,
379
        $substitutions = null,
380
        $personalizationIndex = null,
381
        $personalization = null
382
    ) {
383
        $this->addRecipientEmail(
384
            'To',
385
            $to,
0 ignored issues
show
Bug introduced by
It seems like $to can also be of type SendGrid\Mail\To; however, parameter $email of SendGrid\Mail\Mail::addRecipientEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

385
            /** @scrutinizer ignore-type */ $to,
Loading history...
386
            $name,
387
            $substitutions,
388
            $personalizationIndex,
389
            $personalization
390
        );
391
    }
392
393
    /**
394
     * Adds multiple email recipients to a Personalization object
395
     *
396
     * @param To[]|array           $toEmails             Array of To objects or key/value pairs of
397
     *                                                   email address/recipient names
398
     * @param int|null             $personalizationIndex Index into the array of existing
399
     *                                                   Personalization objects
400
     * @param Personalization|null $personalization      A pre-created
401
     *                                                   Personalization object
402
     *
403
     * @throws TypeException
404
     */
405
    public function addTos(
406
        $toEmails,
407
        $personalizationIndex = null,
408
        $personalization = null
409
    ) {
410
        Assert::minItems($toEmails, 'toEmails', 1);
411
        Assert::maxItems($toEmails, 'toEmails', 1000);
412
413
        $this->addRecipientEmails(
414
            'To',
415
            $toEmails,
416
            $personalizationIndex,
417
            $personalization
418
        );
419
    }
420
421
    /**
422
     * Adds an email cc recipient to a Personalization object
423
     *
424
     * @param string|Cc                 $cc                   Email address or Cc object
425
     * @param string                    $name                 Recipient name
426
     * @param Substitution[]|array|null $substitutions        Personalized
427
     *                                                        substitutions
428
     * @param int|null                  $personalizationIndex Index into the array of existing
429
     *                                                        Personalization objects
430
     * @param Personalization|null      $personalization      A pre-created
431
     *                                                        Personalization object
432
     *
433
     * @throws TypeException
434
     */
435
    public function addCc(
436
        $cc,
437
        $name = null,
438
        $substitutions = null,
439
        $personalizationIndex = null,
440
        $personalization = null
441
    ) {
442
        $this->addRecipientEmail(
443
            'Cc',
444
            $cc,
0 ignored issues
show
Bug introduced by
It seems like $cc can also be of type SendGrid\Mail\Cc; however, parameter $email of SendGrid\Mail\Mail::addRecipientEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

444
            /** @scrutinizer ignore-type */ $cc,
Loading history...
445
            $name,
446
            $substitutions,
447
            $personalizationIndex,
448
            $personalization
449
        );
450
    }
451
452
    /**
453
     * Adds multiple email cc recipients to a Personalization object
454
     *
455
     * @param Cc[]|array           $ccEmails             Array of Cc objects or key/value pairs of
456
     *                                                   email address/recipient names
457
     * @param int|null             $personalizationIndex Index into the array of existing
458
     *                                                   Personalization objects
459
     * @param Personalization|null $personalization      A pre-created
460
     *                                                   Personalization object
461
     *
462
     * @throws TypeException
463
     */
464
    public function addCcs(
465
        $ccEmails,
466
        $personalizationIndex = null,
467
        $personalization = null
468
    ) {
469
        Assert::minItems($ccEmails, 'ccEmails', 1);
470
        Assert::maxItems($ccEmails, 'ccEmails', 1000);
471
472
        $this->addRecipientEmails(
473
            'Cc',
474
            $ccEmails,
475
            $personalizationIndex,
476
            $personalization
477
        );
478
    }
479
480
    /**
481
     * Adds an email bcc recipient to a Personalization object
482
     *
483
     * @param string|Bcc                $bcc                  Email address or Bcc object
484
     * @param string                    $name                 Recipient name
485
     * @param Substitution[]|array|null $substitutions        Personalized
486
     *                                                        substitutions
487
     * @param int|null                  $personalizationIndex Index into the array of existing
488
     *                                                        Personalization objects
489
     * @param Personalization|null      $personalization      A pre-created
490
     *                                                        Personalization object
491
     *
492
     * @throws TypeException
493
     */
494
    public function addBcc(
495
        $bcc,
496
        $name = null,
497
        $substitutions = null,
498
        $personalizationIndex = null,
499
        $personalization = null
500
    ) {
501
        $this->addRecipientEmail(
502
            'Bcc',
503
            $bcc,
0 ignored issues
show
Bug introduced by
It seems like $bcc can also be of type SendGrid\Mail\Bcc; however, parameter $email of SendGrid\Mail\Mail::addRecipientEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

503
            /** @scrutinizer ignore-type */ $bcc,
Loading history...
504
            $name,
505
            $substitutions,
506
            $personalizationIndex,
507
            $personalization
508
        );
509
    }
510
511
    /**
512
     * Adds multiple email bcc recipients to a Personalization object
513
     *
514
     * @param Bcc[]|array          $bccEmails            Array of Bcc objects or key/value pairs of
515
     *                                                   email address/recipient names
516
     * @param int|null             $personalizationIndex Index into the array of existing
517
     *                                                   Personalization objects
518
     * @param Personalization|null $personalization      A pre-created
519
     *                                                   Personalization object
520
     *
521
     * @throws TypeException
522
     */
523
    public function addBccs(
524
        $bccEmails,
525
        $personalizationIndex = null,
526
        $personalization = null
527
    ) {
528
        Assert::minItems($bccEmails, 'bccEmails', 1);
529
        Assert::maxItems($bccEmails, 'bccEmails', 1000);
530
531
        $this->addRecipientEmails(
532
            'Bcc',
533
            $bccEmails,
534
            $personalizationIndex,
535
            $personalization
536
        );
537
    }
538
539
    /**
540
     * Add a subject to a Personalization or Mail object
541
     *
542
     * If you don't provide a Personalization object or index, the
543
     * subject will be global to entire message. Note that
544
     * subjects added to Personalization objects override
545
     * global subjects.
546
     *
547
     * @param string|Subject       $subject              Email subject
548
     * @param int|null             $personalizationIndex Index into the array of existing
549
     *                                                   Personalization objects
550
     * @param Personalization|null $personalization      A pre-created
551
     *                                                   Personalization object
552
     * @throws TypeException
553
     */
554
    public function setSubject(
555
        $subject,
556
        $personalizationIndex = null,
557
        $personalization = null
558
    ) {
559
        if (!($subject instanceof Subject)) {
560
            $subject = new Subject($subject);
561
        }
562
563
        if ($personalization !== null) {
564
            $personalization->setSubject($subject);
565
            $this->addPersonalization($personalization);
566
            return;
567
        }
568
        if ($personalizationIndex !== null) {
569
            $this->personalization[$personalizationIndex]->setSubject($subject);
570
            return;
571
        }
572
        $this->setGlobalSubject($subject);
573
    }
574
575
    /**
576
     * Retrieve a subject attached to a Personalization object
577
     *
578
     * @param int $personalizationIndex   Index into the array of existing
579
     *                                    Personalization objects
580
     * @return Subject
581
     */
582
    public function getSubject($personalizationIndex = 0)
583
    {
584
        return $this->personalization[$personalizationIndex]->getSubject();
585
    }
586
587
    /**
588
     * Add a header to a Personalization or Mail object
589
     *
590
     * If you don't provide a Personalization object or index, the
591
     * header will be global to entire message. Note that
592
     * headers added to Personalization objects override
593
     * global headers.
594
     *
595
     * @param string|Header        $key                  Key or Header object
596
     * @param string|null          $value                Value
597
     * @param int|null             $personalizationIndex Index into the array of existing
598
     *                                                   Personalization objects
599
     * @param Personalization|null $personalization      A pre-created
600
     *                                                   Personalization object
601
     * @throws TypeException
602
     */
603
    public function addHeader(
604
        $key,
605
        $value = null,
606
        $personalizationIndex = null,
607
        $personalization = null
608
    ) {
609
        $header = null;
610
        if ($key instanceof Header) {
611
            $h = $key;
612
            $header = new Header($h->getKey(), $h->getValue());
613
        } else {
614
            $header = new Header($key, $value);
615
        }
616
617
        $personalization = $this->getPersonalization($personalizationIndex, $personalization);
618
        $personalization->addHeader($header);
619
    }
620
621
    /**
622
     * Adds multiple headers to a Personalization or Mail object
623
     *
624
     * If you don't provide a Personalization object or index, the
625
     * header will be global to entire message. Note that
626
     * headers added to Personalization objects override
627
     * global headers.
628
     *
629
     * @param array|Header[]       $headers              Array of Header objects or key values
630
     * @param int|null             $personalizationIndex Index into the array of existing
631
     *                                                   Personalization objects
632
     * @param Personalization|null $personalization      A pre-created
633
     *                                                   Personalization object
634
     * @throws TypeException
635
     */
636
    public function addHeaders(
637
        $headers,
638
        $personalizationIndex = null,
639
        $personalization = null
640
    ) {
641
        if (\current($headers) instanceof Header) {
642
            foreach ($headers as $header) {
643
                $this->addHeader($header);
644
            }
645
        } else {
646
            foreach ($headers as $key => $value) {
647
                $this->addHeader(
648
                    $key,
649
                    $value,
650
                    $personalizationIndex,
651
                    $personalization
652
                );
653
            }
654
        }
655
    }
656
657
    /**
658
     * Retrieve the headers attached to a Personalization object
659
     *
660
     * @param int $personalizationIndex   Index into the array of existing
661
     *                                    Personalization objects
662
     * @return Header[]
663
     */
664
    public function getHeaders($personalizationIndex = 0)
665
    {
666
        return $this->personalization[$personalizationIndex]->getHeaders();
667
    }
668
669
    /**
670
     * Add a Substitution object or key/value to a Personalization object
671
     *
672
     * @param Substitution|string  $key                  Substitution object or the key of a
673
     *                                                   dynamic data
674
     * @param string|null          $value                Value
675
     * @param int|null             $personalizationIndex Index into the array of existing
676
     *                                                   Personalization objects
677
     * @param Personalization|null $personalization      A pre-created
678
     *                                                   Personalization object
679
     * @throws TypeException
680
     */
681
    public function addDynamicTemplateData(
682
        $key,
683
        $value = null,
684
        $personalizationIndex = null,
685
        $personalization = null
686
    ) {
687
        $this->addSubstitution($key, $value, $personalizationIndex, $personalization);
688
    }
689
690
    /**
691
     * Add a Substitution object or key/value to a Personalization object
692
     *
693
     * @param array|Substitution[] $datas                Array of Substitution
694
     *                                                   objects or key/values
695
     * @param int|null             $personalizationIndex Index into the array of existing
696
     *                                                   Personalization objects
697
     * @param Personalization|null $personalization      A pre-created
698
     *                                                   Personalization object
699
     * @throws TypeException
700
     */
701
    public function addDynamicTemplateDatas(
702
        $datas,
703
        $personalizationIndex = null,
704
        $personalization = null
705
    ) {
706
        $this->addSubstitutions($datas, $personalizationIndex, $personalization);
707
    }
708
709
    /**
710
     * Retrieve dynamic template data key/value pairs from a Personalization object
711
     *
712
     * @param int|0 $personalizationIndex Index into the array of existing
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|0 at position 2 could not be parsed: Unknown type name '0' at position 2 in int|0.
Loading history...
713
     *                                    Personalization objects
714
     * @return array
715
     */
716
    public function getDynamicTemplateDatas($personalizationIndex = 0)
717
    {
718
        return $this->getSubstitutions($personalizationIndex);
719
    }
720
721
    /**
722
     * Add a substitution to a Personalization or Mail object
723
     *
724
     * If you don't provide a Personalization object or index, the
725
     * substitution will be global to entire message. Note that
726
     * substitutions added to Personalization objects override
727
     * global substitutions.
728
     *
729
     * @param string|Substitution  $key                  Key or Substitution object
730
     * @param string|null          $value                Value
731
     * @param int|null             $personalizationIndex Index into the array of existing
732
     *                                                   Personalization objects
733
     * @param Personalization|null $personalization      A pre-created
734
     *                                                   Personalization object
735
     * @throws TypeException
736
     */
737
    public function addSubstitution(
738
        $key,
739
        $value = null,
740
        $personalizationIndex = null,
741
        $personalization = null
742
    ) {
743
        $substitution = null;
744
        if ($key instanceof Substitution) {
745
            $s = $key;
746
            $substitution = new Substitution($s->getKey(), $s->getValue());
747
        } else {
748
            $substitution = new Substitution($key, $value);
749
        }
750
751
        $personalization = $this->getPersonalization($personalizationIndex, $personalization);
752
        $personalization->addSubstitution($substitution);
753
    }
754
755
    /**
756
     * Adds multiple substitutions to a Personalization or Mail object
757
     *
758
     * If you don't provide a Personalization object or index, the
759
     * substitution will be global to entire message. Note that
760
     * substitutions added to Personalization objects override
761
     * global headers.
762
     *
763
     * @param array|Substitution[] $substitutions        Array of Substitution
764
     *                                                   objects or key/values
765
     * @param int|null             $personalizationIndex Index into the array of existing
766
     *                                                   Personalization objects
767
     * @param Personalization|null $personalization      A pre-created
768
     *                                                   Personalization object
769
     * @throws TypeException
770
     */
771
    public function addSubstitutions(
772
        $substitutions,
773
        $personalizationIndex = null,
774
        $personalization = null
775
    ) {
776
        if (\current($substitutions) instanceof Substitution) {
777
            foreach ($substitutions as $substitution) {
778
                $this->addSubstitution($substitution);
779
            }
780
        } else {
781
            foreach ($substitutions as $key => $value) {
782
                $this->addSubstitution(
783
                    $key,
784
                    $value,
785
                    $personalizationIndex,
786
                    $personalization
787
                );
788
            }
789
        }
790
    }
791
792
    /**
793
     * Retrieve the substitutions attached to a Personalization object
794
     *
795
     * @param int|0 $personalizationIndex Index into the array of existing
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|0 at position 2 could not be parsed: Unknown type name '0' at position 2 in int|0.
Loading history...
796
     *                                    Personalization objects
797
     * @return Substitution[]
798
     */
799
    public function getSubstitutions($personalizationIndex = 0)
800
    {
801
        return $this->personalization[$personalizationIndex]->getSubstitutions();
802
    }
803
804
    /**
805
     * Add a custom arg to a Personalization or Mail object
806
     *
807
     * Note that custom args added to Personalization objects
808
     * override global custom args.
809
     *
810
     * @param string|CustomArg     $key                  Key or CustomArg object
811
     * @param string|null          $value                Value
812
     * @param int|null             $personalizationIndex Index into the array of existing
813
     *                                                   Personalization objects
814
     * @param Personalization|null $personalization      A pre-created
815
     *                                                   Personalization object
816
     * @throws TypeException
817
     */
818
    public function addCustomArg(
819
        $key,
820
        $value = null,
821
        $personalizationIndex = null,
822
        $personalization = null
823
    ) {
824
        $custom_arg = null;
825
        if ($key instanceof CustomArg) {
826
            $ca = $key;
827
            $custom_arg = new CustomArg($ca->getKey(), $ca->getValue());
828
        } else {
829
            $custom_arg = new CustomArg($key, $value);
830
        }
831
832
        $personalization = $this->getPersonalization($personalizationIndex, $personalization);
833
        $personalization->addCustomArg($custom_arg);
834
    }
835
836
    /**
837
     * Adds multiple custom args to a Personalization or Mail object
838
     *
839
     * If you don't provide a Personalization object or index, the
840
     * custom arg will be global to entire message. Note that
841
     * custom args added to Personalization objects override
842
     * global custom args.
843
     *
844
     * @param array|CustomArg[]    $custom_args          Array of CustomArg objects or
845
     *                                                   key/values
846
     * @param int|null             $personalizationIndex Index into the array of existing
847
     *                                                   Personalization objects
848
     * @param Personalization|null $personalization      A pre-created
849
     *                                                   Personalization object
850
     * @throws TypeException
851
     */
852
    public function addCustomArgs(
853
        $custom_args,
854
        $personalizationIndex = null,
855
        $personalization = null
856
    ) {
857
        if (\current($custom_args) instanceof CustomArg) {
858
            foreach ($custom_args as $custom_arg) {
859
                $this->addCustomArg($custom_arg);
860
            }
861
        } else {
862
            foreach ($custom_args as $key => $value) {
863
                $this->addCustomArg(
864
                    $key,
865
                    $value,
866
                    $personalizationIndex,
867
                    $personalization
868
                );
869
            }
870
        }
871
    }
872
873
    /**
874
     * Retrieve the custom args attached to a Personalization object
875
     *
876
     * @param int|0 $personalizationIndex Index into the array of existing
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|0 at position 2 could not be parsed: Unknown type name '0' at position 2 in int|0.
Loading history...
877
     *                                    Personalization objects
878
     * @return CustomArg[]
879
     */
880
    public function getCustomArgs($personalizationIndex = 0)
881
    {
882
        return $this->personalization[$personalizationIndex]->getCustomArgs();
883
    }
884
885
    /**
886
     * Add a unix timestamp allowing you to specify when you want your
887
     * email to be delivered to a Personalization or Mail object
888
     *
889
     * If you don't provide a Personalization object or index, the
890
     * send at timestamp will be global to entire message. Note that
891
     * timestamps added to Personalization objects override
892
     * global timestamps.
893
     *
894
     * @param int|SendAt           $send_at              A unix timestamp
895
     * @param int|null             $personalizationIndex Index into the array of existing
896
     *                                                   Personalization objects
897
     * @param Personalization|null $personalization      A pre-created
898
     *                                                   Personalization object
899
     * @throws TypeException
900
     */
901
    public function setSendAt(
902
        $send_at,
903
        $personalizationIndex = null,
904
        $personalization = null
905
    ) {
906
        if (!($send_at instanceof SendAt)) {
907
            $send_at = new SendAt($send_at);
908
        }
909
910
        $personalization = $this->getPersonalization($personalizationIndex, $personalization);
911
        $personalization->setSendAt($send_at);
912
    }
913
914
    /**
915
     * Retrieve the unix timestamp attached to a Personalization object
916
     *
917
     * @param int|0 $personalizationIndex Index into the array of existing
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|0 at position 2 could not be parsed: Unknown type name '0' at position 2 in int|0.
Loading history...
918
     *                                    Personalization objects
919
     * @return SendAt|null
920
     */
921
    public function getSendAt($personalizationIndex = 0)
922
    {
923
        return $this->personalization[$personalizationIndex]->getSendAt();
924
    }
925
926
    /**
927
     * Add the sender email address to a Mail object
928
     *
929
     * @param string|From $email Email address or From object
930
     * @param string|null $name  Sender name
931
     *
932
     * @throws TypeException
933
     */
934
    public function setFrom($email, $name = null)
935
    {
936
        if ($email instanceof From) {
937
            $this->from = $email;
938
        } else {
939
            Assert::email(
940
                $email,
941
                'email',
942
                '"$email" must be an instance of SendGrid\Mail\From or a valid email address'
943
            );
944
            $this->from = new From($email, $name);
945
        }
946
    }
947
948
    /**
949
     * Retrieve the sender attached to a Mail object
950
     *
951
     * @return From
952
     */
953
    public function getFrom()
954
    {
955
        return $this->from;
956
    }
957
958
    /**
959
     * Add the reply to email address to a Mail object
960
     *
961
     * @param string|ReplyTo $email Email address or From object
962
     * @param string|null    $name  Reply to name
963
     *
964
     * @throws TypeException
965
     */
966
    public function setReplyTo($email, $name = null)
967
    {
968
        if ($email instanceof ReplyTo) {
969
            $this->reply_to = $email;
970
        } else {
971
            $this->reply_to = new ReplyTo($email, $name);
972
        }
973
    }
974
975
    /**
976
     * Retrieve the reply to information attached to a Mail object
977
     *
978
     * @return ReplyTo
979
     */
980
    public function getReplyTo()
981
    {
982
        return $this->reply_to;
983
    }
984
985
    /**
986
     * Add a subject to a Mail object
987
     *
988
     * Note that
989
     * subjects added to Personalization objects override
990
     * global subjects.
991
     *
992
     * @param string|Subject $subject Email subject
993
     *
994
     * @throws TypeException
995
     */
996
    public function setGlobalSubject($subject)
997
    {
998
        if (!($subject instanceof Subject)) {
999
            $subject = new Subject($subject);
1000
        }
1001
        $this->subject = $subject;
1002
    }
1003
1004
    /**
1005
     * Retrieve a subject attached to a Mail object
1006
     *
1007
     * @return Subject
1008
     */
1009
    public function getGlobalSubject()
1010
    {
1011
        return $this->subject;
1012
    }
1013
1014
    /**
1015
     * Add content to a Mail object
1016
     *
1017
     * For a list of pre-configured mime types, please see
1018
     * MimeType.php
1019
     *
1020
     * @param string|Content $type  Mime type or Content object
1021
     * @param string|null    $value Contents (e.g. text or html)
1022
     *
1023
     * @throws TypeException
1024
     */
1025
    public function addContent($type, $value = null)
1026
    {
1027
        if ($type instanceof Content) {
1028
            $content = $type;
1029
        } else {
1030
            $content = new Content($type, $value);
1031
        }
1032
        $this->contents[] = $content;
1033
    }
1034
1035
    /**
1036
     * Adds multiple Content objects to a Mail object
1037
     *
1038
     * @param array|Content[] $contents Array of Content objects
1039
     *                                  or key value pairs
1040
     *
1041
     * @throws TypeException
1042
     */
1043
    public function addContents($contents)
1044
    {
1045
        if (\current($contents) instanceof Content) {
1046
            foreach ($contents as $content) {
1047
                $this->addContent($content);
1048
            }
1049
        } else {
1050
            foreach ($contents as $key => $value) {
1051
                $this->addContent($key, $value);
1052
            }
1053
        }
1054
    }
1055
1056
    /**
1057
     * Retrieve the contents attached to a Mail object
1058
     *
1059
     * Will return array of Content Objects with text/plain MimeType first
1060
     * Array re-ordered before return where this is not already the case
1061
     *
1062
     * @return Content[]
1063
     */
1064
    public function getContents()
1065
    {
1066
        if ($this->contents) {
1067
            if ($this->contents[0]->getType() !== MimeType::TEXT && \count($this->contents) > 1) {
1068
                foreach ($this->contents as $key => $value) {
1069
                    if ($value->getType() === MimeType::TEXT) {
1070
                        $plain_content = $value;
1071
                        unset($this->contents[$key]);
1072
                        break;
1073
                    }
1074
                }
1075
                if (isset($plain_content)) {
1076
                    array_unshift($this->contents, $plain_content);
1077
                }
1078
            }
1079
        }
1080
1081
        return $this->contents;
1082
    }
1083
1084
    /**
1085
     * Add an attachment to a Mail object
1086
     *
1087
     * @param string|Attachment $attachment  Attachment object or
1088
     *                                       Base64 encoded content
1089
     * @param string|null       $type        Mime type of the attachment
1090
     * @param string|null       $filename    File name of the attachment
1091
     * @param string|null       $disposition How the attachment should be
1092
     *                                       displayed: inline or attachment
1093
     *                                       default is attachment
1094
     * @param string|null       $content_id  Used when disposition is inline
1095
     *                                       to display the file within the
1096
     *                                       body of the email
1097
     * @throws TypeException
1098
     */
1099
    public function addAttachment(
1100
        $attachment,
1101
        $type = null,
1102
        $filename = null,
1103
        $disposition = null,
1104
        $content_id = null
1105
    ) {
1106
        if (\is_array($attachment)) {
0 ignored issues
show
introduced by
The condition is_array($attachment) is always false.
Loading history...
1107
            $attachment = new Attachment(
1108
                $attachment[0],
1109
                $attachment[1],
1110
                $attachment[2],
1111
                $attachment[3],
1112
                $attachment[4]
1113
            );
1114
        } else if (!($attachment instanceof Attachment)) {
1115
            $attachment = new Attachment(
1116
                $attachment,
1117
                $type,
1118
                $filename,
1119
                $disposition,
1120
                $content_id
1121
            );
1122
        }
1123
        $this->attachments[] = $attachment;
1124
    }
1125
1126
    /**
1127
     * Adds multiple attachments to a Mail object
1128
     *
1129
     * @param array|Attachment[] $attachments Array of Attachment objects or
1130
     *                                        arrays
1131
     * @throws TypeException
1132
     */
1133
    public function addAttachments($attachments)
1134
    {
1135
        foreach ($attachments as $attachment) {
1136
            $this->addAttachment($attachment);
1137
        }
1138
    }
1139
1140
    /**
1141
     * Retrieve the attachments attached to a Mail object
1142
     *
1143
     * @return Attachment[]
1144
     */
1145
    public function getAttachments()
1146
    {
1147
        return $this->attachments;
1148
    }
1149
1150
    /**
1151
     * Add a template id to a Mail object
1152
     *
1153
     * @param TemplateId|string $template_id The id of the template to be
1154
     *                                       applied to this email
1155
     * @throws TypeException
1156
     */
1157
    public function setTemplateId($template_id)
1158
    {
1159
        if (!($template_id instanceof TemplateId)) {
1160
            $template_id = new TemplateId($template_id);
1161
        }
1162
1163
        $this->template_id = $template_id;
1164
    }
1165
1166
    /**
1167
     * Retrieve a template id attached to a Mail object
1168
     *
1169
     * @return TemplateId
1170
     */
1171
    public function getTemplateId()
1172
    {
1173
        return $this->template_id;
1174
    }
1175
1176
    /**
1177
     * Add a section to a Mail object
1178
     *
1179
     * @param string|Section $key   Key or Section object
1180
     * @param string|null    $value Value
1181
     */
1182
    public function addSection($key, $value = null)
1183
    {
1184
        if ($key instanceof Section) {
1185
            $section = $key;
1186
            $this->sections[$section->getKey()] = $section->getValue();
1187
            return;
1188
        }
1189
        $this->sections[$key] = (string)$value;
1190
    }
1191
1192
    /**
1193
     * Adds multiple sections to a Mail object
1194
     *
1195
     * @param array|Section[] $sections Array of CustomArg objects
1196
     *                                  or key/values
1197
     */
1198
    public function addSections($sections)
1199
    {
1200
        if (\current($sections) instanceof Section) {
1201
            foreach ($sections as $section) {
1202
                $this->addSection($section);
1203
            }
1204
        } else {
1205
            foreach ($sections as $key => $value) {
1206
                $this->addSection($key, $value);
1207
            }
1208
        }
1209
    }
1210
1211
    /**
1212
     * Retrieve the section(s) attached to a Mail object
1213
     *
1214
     * @return Section[]
1215
     */
1216
    public function getSections()
1217
    {
1218
        return $this->sections;
1219
    }
1220
1221
    /**
1222
     * Add a header to a Mail object
1223
     *
1224
     * Note that headers added to Personalization objects override
1225
     * global headers.
1226
     *
1227
     * @param string|Header $key   Key or Header object
1228
     * @param string|null   $value Value
1229
     */
1230
    public function addGlobalHeader($key, $value = null)
1231
    {
1232
        if ($key instanceof Header) {
1233
            $header = $key;
1234
            $this->headers[$header->getKey()] = $header->getValue();
1235
            return;
1236
        }
1237
        $this->headers[$key] = (string)$value;
1238
    }
1239
1240
    /**
1241
     * Adds multiple headers to a Mail object
1242
     *
1243
     * Note that headers added to Personalization objects override
1244
     * global headers.
1245
     *
1246
     * @param array|Header[] $headers Array of Header objects
1247
     *                                or key values
1248
     */
1249
    public function addGlobalHeaders($headers)
1250
    {
1251
        if (\current($headers) instanceof Header) {
1252
            foreach ($headers as $header) {
1253
                $this->addGlobalHeader($header);
1254
            }
1255
        } else {
1256
            foreach ($headers as $key => $value) {
1257
                $this->addGlobalHeader($key, $value);
1258
            }
1259
        }
1260
    }
1261
1262
    /**
1263
     * Retrieve the headers attached to a Mail object
1264
     *
1265
     * @return Header[]
1266
     */
1267
    public function getGlobalHeaders()
1268
    {
1269
        return $this->headers;
1270
    }
1271
1272
    /**
1273
     * Add a substitution to a Mail object
1274
     *
1275
     * Note that substitutions added to Personalization objects override
1276
     * global substitutions.
1277
     *
1278
     * @param string|Substitution $key   Key or Substitution object
1279
     * @param string|null         $value Value
1280
     */
1281
    public function addGlobalSubstitution($key, $value = null)
1282
    {
1283
        if ($key instanceof Substitution) {
1284
            $substitution = $key;
1285
            $this->substitutions[$substitution->getKey()] = $substitution->getValue();
1286
            return;
1287
        }
1288
        $this->substitutions[$key] = $value;
1289
    }
1290
1291
    /**
1292
     * Adds multiple substitutions to a Mail object
1293
     *
1294
     * Note that substitutions added to Personalization objects override
1295
     * global headers.
1296
     *
1297
     * @param array|Substitution[] $substitutions Array of Substitution
1298
     *                                            objects or key/values
1299
     */
1300
    public function addGlobalSubstitutions($substitutions)
1301
    {
1302
        if (\current($substitutions) instanceof Substitution) {
1303
            foreach ($substitutions as $substitution) {
1304
                $this->addGlobalSubstitution($substitution);
1305
            }
1306
        } else {
1307
            foreach ($substitutions as $key => $value) {
1308
                $this->addGlobalSubstitution($key, $value);
1309
            }
1310
        }
1311
    }
1312
1313
    /**
1314
     * Retrieve the substitutions attached to a Mail object
1315
     *
1316
     * @return Substitution[]
1317
     */
1318
    public function getGlobalSubstitutions()
1319
    {
1320
        return $this->substitutions;
1321
    }
1322
1323
    /**
1324
     * Add a category to a Mail object
1325
     *
1326
     * @param string|Category $category Category object or category name
1327
     * @throws TypeException
1328
     */
1329
    public function addCategory($category)
1330
    {
1331
        if (!($category instanceof Category)) {
1332
            $category = new Category($category);
1333
        }
1334
1335
        Assert::accept($category, 'category', function () {
1336
            $categories = $this->categories;
1337
            if (!\is_array($categories)) {
1338
                $categories = [];
1339
            }
1340
            return \count($categories) < 10;
1341
        }, 'Number of elements in "$categories" can not exceed 10.');
1342
1343
        $this->categories[] = $category;
1344
    }
1345
1346
    /**
1347
     * Adds multiple categories to a Mail object
1348
     *
1349
     * @param array|Category[] $categories Array of Category objects or arrays
1350
     * @throws TypeException
1351
     */
1352
    public function addCategories($categories)
1353
    {
1354
        foreach ($categories as $category) {
1355
            $this->addCategory($category);
1356
        }
1357
    }
1358
1359
    /**
1360
     * Retrieve the categories attached to a Mail object
1361
     *
1362
     * @return Category[]
1363
     */
1364
    public function getCategories()
1365
    {
1366
        return $this->categories;
1367
    }
1368
1369
    /**
1370
     * Add a custom arg to a Mail object
1371
     *
1372
     * Note that custom args added to Personalization objects override
1373
     * global custom args.
1374
     *
1375
     * @param string|CustomArg $key   Key or CustomArg object
1376
     * @param string|null      $value Value
1377
     */
1378
    public function addGlobalCustomArg($key, $value = null)
1379
    {
1380
        if ($key instanceof CustomArg) {
1381
            $custom_arg = $key;
1382
            $this->custom_args[$custom_arg->getKey()] = $custom_arg->getValue();
1383
            return;
1384
        }
1385
        $this->custom_args[$key] = (string)$value;
1386
    }
1387
1388
    /**
1389
     * Adds multiple custom args to a Mail object
1390
     *
1391
     * Note that custom args added to Personalization objects override
1392
     * global custom args.
1393
     *
1394
     * @param array|CustomArg[] $custom_args Array of CustomArg objects
1395
     *                                       or key/values
1396
     */
1397
    public function addGlobalCustomArgs($custom_args)
1398
    {
1399
        if (\current($custom_args) instanceof CustomArg) {
1400
            foreach ($custom_args as $custom_arg) {
1401
                $this->addGlobalCustomArg($custom_arg);
1402
            }
1403
        } else {
1404
            foreach ($custom_args as $key => $value) {
1405
                $this->addGlobalCustomArg($key, $value);
1406
            }
1407
        }
1408
    }
1409
1410
    /**
1411
     * Retrieve the custom args attached to a Mail object
1412
     *
1413
     * @return CustomArg[]
1414
     */
1415
    public function getGlobalCustomArgs()
1416
    {
1417
        return $this->custom_args;
1418
    }
1419
1420
    /**
1421
     * Add a unix timestamp allowing you to specify when you want your
1422
     * email to be delivered to a Mail object
1423
     *
1424
     * Note that timestamps added to Personalization objects override
1425
     * global timestamps.
1426
     *
1427
     * @param int|SendAt $send_at A unix timestamp
1428
     * @throws TypeException
1429
     */
1430
    public function setGlobalSendAt($send_at)
1431
    {
1432
        if (!($send_at instanceof SendAt)) {
1433
            $send_at = new SendAt($send_at);
1434
        }
1435
        $this->send_at = $send_at;
1436
    }
1437
1438
    /**
1439
     * Retrieve the unix timestamp attached to a Mail object
1440
     *
1441
     * @return SendAt
1442
     */
1443
    public function getGlobalSendAt()
1444
    {
1445
        return $this->send_at;
1446
    }
1447
1448
    /**
1449
     * Add a batch id to a Mail object
1450
     *
1451
     * @param string|BatchId $batch_id Id for a batch of emails
1452
     *                                 to be sent at the same time
1453
     * @throws TypeException
1454
     */
1455
    public function setBatchId($batch_id)
1456
    {
1457
        if (!($batch_id instanceof BatchId)) {
1458
            $batch_id = new BatchId($batch_id);
1459
        }
1460
        $this->batch_id = $batch_id;
1461
    }
1462
1463
    /**
1464
     * Retrieve the batch id attached to a Mail object
1465
     *
1466
     * @return BatchId
1467
     */
1468
    public function getBatchId()
1469
    {
1470
        return $this->batch_id;
1471
    }
1472
1473
    /**
1474
     * Add a Asm describing how to handle unsubscribes to a Mail object
1475
     *
1476
     * @param int|Asm $group_id          Asm object or unsubscribe group id
1477
     *                                   to associate this email with
1478
     * @param array   $groups_to_display Array of integer ids of unsubscribe
1479
     *                                   groups to be displayed on the
1480
     *                                   unsubscribe preferences page
1481
     * @throws TypeException
1482
     */
1483
    public function setAsm($group_id, $groups_to_display = null)
1484
    {
1485
        if ($group_id instanceof Asm) {
1486
            $asm = $group_id;
1487
            $this->asm = $asm;
1488
        } else {
1489
            $this->asm = new Asm($group_id, $groups_to_display);
1490
        }
1491
    }
1492
1493
    /**
1494
     * Retrieve the Asm object describing how to handle unsubscribes attached
1495
     * to a Mail object
1496
     *
1497
     * @return Asm
1498
     */
1499
    public function getAsm()
1500
    {
1501
        return $this->asm;
1502
    }
1503
1504
    /**
1505
     * Add the IP pool name to a Mail object
1506
     *
1507
     * @param string|IpPoolName $ip_pool_name The IP Pool that you would
1508
     *                                        like to send this email from
1509
     * @throws TypeException
1510
     */
1511
    public function setIpPoolName($ip_pool_name)
1512
    {
1513
        if ($ip_pool_name instanceof IpPoolName) {
1514
            $this->ip_pool_name = $ip_pool_name->getIpPoolName();
1515
        } else {
1516
            $this->ip_pool_name = new IpPoolName($ip_pool_name);
1517
        }
1518
    }
1519
1520
    /**
1521
     * Retrieve the IP pool name attached to a Mail object
1522
     *
1523
     * @return IpPoolName
1524
     */
1525
    public function getIpPoolName()
1526
    {
1527
        return $this->ip_pool_name;
1528
    }
1529
1530
    /**
1531
     * Add a MailSettings object to a Mail object
1532
     *
1533
     * @param MailSettings $mail_settings A collection of different
1534
     *                                    mail settings that you can
1535
     *                                    use to specify how you would
1536
     *                                    like this email to be handled
1537
     */
1538
    public function setMailSettings($mail_settings)
1539
    {
1540
        $this->mail_settings = $mail_settings;
1541
    }
1542
1543
    /**
1544
     * Retrieve the MailSettings object attached to a Mail object
1545
     *
1546
     * @return MailSettings
1547
     */
1548
    public function getMailSettings()
1549
    {
1550
        return $this->mail_settings;
1551
    }
1552
1553
    /**
1554
     * Set the Bcc settings on a MailSettings object
1555
     *
1556
     * @param bool|BccSettings $enable A BccSettings object or a boolean
1557
     *                                 to determine if this setting is active
1558
     * @param string|null      $email  The email address to be bcc'ed
1559
     * @throws TypeException
1560
     */
1561
    public function setBccSettings($enable, $email = null)
1562
    {
1563
        if (!($this->mail_settings instanceof MailSettings)) {
1564
            $this->mail_settings = new MailSettings();
1565
        }
1566
        $this->mail_settings->setBccSettings($enable, $email);
1567
    }
1568
1569
    /**
1570
     * Enable bypass bounce management on a MailSettings object
1571
     *
1572
     * Allows you to bypass the bounce list to ensure that the email is delivered to recipients.
1573
     * Spam report and unsubscribe lists will still be checked; addresses on these other lists
1574
     * will not receive the message.
1575
     *
1576
     * This filter cannot be combined with the bypass_list_management filter.
1577
     *
1578
     * @throws TypeException
1579
     */
1580
    public function enableBypassBounceManagement()
1581
    {
1582
        if (!$this->mail_settings instanceof MailSettings) {
1583
            $this->mail_settings = new MailSettings();
1584
        }
1585
        $this->mail_settings->setBypassBounceManagement(true);
1586
    }
1587
1588
    /**
1589
     * Enable bypass list management on a MailSettings object
1590
     *
1591
     * Allows you to bypass all unsubscribe groups and suppressions to ensure
1592
     * that the email is delivered to every single recipient. This should only
1593
     * be used in emergencies when it is absolutely necessary that every
1594
     * recipient receives your email.
1595
     *
1596
     * @throws TypeException
1597
     */
1598
    public function enableBypassListManagement()
1599
    {
1600
        if (!$this->mail_settings instanceof MailSettings) {
1601
            $this->mail_settings = new MailSettings();
1602
        }
1603
        $this->mail_settings->setBypassListManagement(true);
1604
    }
1605
1606
    /**
1607
     * Enable bypass spam management on a MailSettings object
1608
     *
1609
     * Allows you to bypass the spam report list to ensure that the email is delivered to recipients.
1610
     * Bounce and unsubscribe lists will still be checked; addresses on these other lists will not
1611
     * receive the message.
1612
     *
1613
     * This filter cannot be combined with the bypass_list_management filter.
1614
     *
1615
     * @throws TypeException
1616
     */
1617
    public function enableBypassSpamManagement()
1618
    {
1619
        if (!$this->mail_settings instanceof MailSettings) {
1620
            $this->mail_settings = new MailSettings();
1621
        }
1622
        $this->mail_settings->setBypassSpamManagement(true);
1623
    }
1624
1625
    /**
1626
     * Enable bypass unsubscribe management on a MailSettings object
1627
     *
1628
     * Allows you to bypass the global unsubscribe list to ensure that the email is delivered
1629
     * to recipients. Bounce and spam report lists will still be checked; addresses on these
1630
     * other lists will not receive the message. This filter applies only to global unsubscribes
1631
     * and will not bypass group unsubscribes.
1632
     *
1633
     * This filter cannot be combined with the bypass_list_management filter.
1634
     *
1635
     * @throws TypeException
1636
     */
1637
    public function enableBypassUnsubscribeManagement()
1638
    {
1639
        if (!$this->mail_settings instanceof MailSettings) {
1640
            $this->mail_settings = new MailSettings();
1641
        }
1642
        $this->mail_settings->setBypassUnsubscribeManagement(true);
1643
    }
1644
1645
    /**
1646
     * Disable bypass bounce management on a MailSettings object
1647
     *
1648
     * Allows you to bypass the bounce list to ensure that the email is delivered to recipients.
1649
     * Spam report and unsubscribe lists will still be checked; addresses on these other lists
1650
     * will not receive the message.
1651
     *
1652
     * This filter cannot be combined with the bypass_list_management filter.
1653
     *
1654
     * @throws TypeException
1655
     */
1656
    public function disableBypassBounceManagement()
1657
    {
1658
        if (!($this->mail_settings instanceof MailSettings)) {
1659
            $this->mail_settings = new MailSettings();
1660
        }
1661
        $this->mail_settings->setBypassBounceManagement(false);
1662
    }
1663
1664
    /**
1665
     * Disable bypass list management on a MailSettings object
1666
     *
1667
     * Allows you to bypass all unsubscribe groups and suppressions to ensure
1668
     * that the email is delivered to every single recipient. This should only
1669
     * be used in emergencies when it is absolutely necessary that every
1670
     * recipient receives your email.
1671
     *
1672
     * @throws TypeException
1673
     */
1674
    public function disableBypassListManagement()
1675
    {
1676
        if (!($this->mail_settings instanceof MailSettings)) {
1677
            $this->mail_settings = new MailSettings();
1678
        }
1679
        $this->mail_settings->setBypassListManagement(false);
1680
    }
1681
1682
1683
    /**
1684
     * Disable bypass spam management on a MailSettings object
1685
     *
1686
     * Allows you to bypass the spam report list to ensure that the email is delivered to recipients.
1687
     * Bounce and unsubscribe lists will still be checked; addresses on these other lists will not
1688
     * receive the message.
1689
     *
1690
     * This filter cannot be combined with the bypass_list_management filter.
1691
     *
1692
     * @throws TypeException
1693
     */
1694
    public function disableBypassSpamManagement()
1695
    {
1696
        if (!($this->mail_settings instanceof MailSettings)) {
1697
            $this->mail_settings = new MailSettings();
1698
        }
1699
        $this->mail_settings->setBypassSpamManagement(false);
1700
    }
1701
1702
    /**
1703
     * Disable bypass global unsubscribe management on a MailSettings object
1704
     *
1705
     * Allows you to bypass the global unsubscribe list to ensure that the email is delivered
1706
     * to recipients. Bounce and spam report lists will still be checked; addresses on these
1707
     * other lists will not receive the message. This filter applies only to global unsubscribes
1708
     * and will not bypass group unsubscribes.
1709
     *
1710
     * This filter cannot be combined with the bypass_list_management filter.
1711
     *
1712
     * @throws TypeException
1713
     */
1714
    public function disableBypassUnsubscribeManagement()
1715
    {
1716
        if (!($this->mail_settings instanceof MailSettings)) {
1717
            $this->mail_settings = new MailSettings();
1718
        }
1719
        $this->mail_settings->setBypassUnsubscribeManagement(false);
1720
    }
1721
1722
    /**
1723
     * Set the Footer settings on a MailSettings object
1724
     *
1725
     * @param bool|Footer $enable A Footer object or a boolean
1726
     *                            to determine if this setting is active
1727
     * @param string|null $text   The plain text content of the footer
1728
     * @param string|null $html   The HTML content of the footer
1729
     *
1730
     * @throws TypeException
1731
     */
1732
    public function setFooter($enable = null, $text = null, $html = null)
1733
    {
1734
        if (!$this->mail_settings instanceof MailSettings) {
1735
            $this->mail_settings = new MailSettings();
1736
        }
1737
        $this->mail_settings->setFooter($enable, $text, $html);
1738
    }
1739
1740
    /**
1741
     * Enable sandbox mode on a MailSettings object
1742
     *
1743
     * This allows you to send a test email to ensure that your request
1744
     * body is valid and formatted correctly.
1745
     *
1746
     * @throws TypeException
1747
     */
1748
    public function enableSandBoxMode()
1749
    {
1750
        if (!($this->mail_settings instanceof MailSettings)) {
1751
            $this->mail_settings = new MailSettings();
1752
        }
1753
        $this->mail_settings->setSandBoxMode(true);
1754
    }
1755
1756
    /**
1757
     * Disable sandbox mode on a MailSettings object
1758
     *
1759
     * This allows you to send a test email to ensure that your request
1760
     * body is valid and formatted correctly.
1761
     *
1762
     * @throws TypeException
1763
     */
1764
    public function disableSandBoxMode()
1765
    {
1766
        if (!($this->mail_settings instanceof MailSettings)) {
1767
            $this->mail_settings = new MailSettings();
1768
        }
1769
        $this->mail_settings->setSandBoxMode(false);
1770
    }
1771
1772
    /**
1773
     * Set the spam check settings on a MailSettings object
1774
     *
1775
     * @param bool|SpamCheck $enable      A SpamCheck object or a boolean
1776
     *                                    to determine if this setting is active
1777
     * @param int|null       $threshold   The threshold used to determine if your
1778
     *                                    content qualifies as spam on a scale from
1779
     *                                    1 to 10, with 10 being most strict, or
1780
     *                                    most likely to be considered as spam
1781
     * @param string|null    $post_to_url An Inbound Parse URL that you would like
1782
     *                                    a copy of your email along with the spam
1783
     *                                    report to be sent to
1784
     *
1785
     * @throws TypeException
1786
     */
1787
    public function setSpamCheck($enable = null, $threshold = null, $post_to_url = null)
1788
    {
1789
        if (!$this->mail_settings instanceof MailSettings) {
1790
            $this->mail_settings = new MailSettings();
1791
        }
1792
        $this->mail_settings->setSpamCheck($enable, $threshold, $post_to_url);
1793
    }
1794
1795
    /**
1796
     * Add a TrackingSettings object to a Mail object
1797
     *
1798
     * @param TrackingSettings $tracking_settings Settings to determine how you
1799
     *                                            would like to track the metrics
1800
     *                                            of how your recipients interact
1801
     *                                            with your email
1802
     */
1803
    public function setTrackingSettings($tracking_settings)
1804
    {
1805
        $this->tracking_settings = $tracking_settings;
1806
    }
1807
1808
    /**
1809
     * Retrieve the TrackingSettings object attached to a Mail object
1810
     *
1811
     * @return TrackingSettings
1812
     */
1813
    public function getTrackingSettings()
1814
    {
1815
        return $this->tracking_settings;
1816
    }
1817
1818
    /**
1819
     * Set the click tracking settings on a TrackingSettings object
1820
     *
1821
     * @param bool|ClickTracking $enable      A ClickTracking object or a boolean
1822
     *                                        to determine if this setting is active
1823
     * @param bool|null          $enable_text Indicates if this setting should be
1824
     *                                        included in the text/plain portion of
1825
     *                                        your email
1826
     *
1827
     * @throws TypeException
1828
     */
1829
    public function setClickTracking($enable = null, $enable_text = null)
1830
    {
1831
        if (!($this->tracking_settings instanceof TrackingSettings)) {
1832
            $this->tracking_settings = new TrackingSettings();
1833
        }
1834
        $this->tracking_settings->setClickTracking($enable, $enable_text);
1835
    }
1836
1837
    /**
1838
     * Set the open tracking settings on a TrackingSettings object
1839
     *
1840
     * @param bool|OpenTracking $enable           A OpenTracking object or a boolean
1841
     *                                            to determine if this setting is
1842
     *                                            active
1843
     * @param string|null       $substitution_tag Allows you to specify a
1844
     *                                            substitution tag that you can
1845
     *                                            insert in the body of your email
1846
     *                                            at a location that you desire.
1847
     *                                            This tag will be replaced by the
1848
     *                                            open tracking pixel
1849
     *
1850
     * @throws TypeException
1851
     */
1852
    public function setOpenTracking($enable = null, $substitution_tag = null)
1853
    {
1854
        if (!($this->tracking_settings instanceof TrackingSettings)) {
1855
            $this->tracking_settings = new TrackingSettings();
1856
        }
1857
        $this->tracking_settings->setOpenTracking($enable, $substitution_tag);
1858
    }
1859
1860
    /**
1861
     * Set the subscription tracking settings on a TrackingSettings object
1862
     *
1863
     * @param bool|SubscriptionTracking $enable           A SubscriptionTracking
1864
     *                                                    object or a boolean to
1865
     *                                                    determine if this setting
1866
     *                                                    is active
1867
     * @param string|null               $text             Text to be appended to the
1868
     *                                                    email, with the
1869
     *                                                    subscription tracking
1870
     *                                                    link. You may control
1871
     *                                                    where the link is by using
1872
     *                                                    the tag <% %>
1873
     * @param string|null               $html             HTML to be appended to the
1874
     *                                                    email, with the
1875
     *                                                    subscription tracking
1876
     *                                                    link. You may control
1877
     *                                                    where the link is by using
1878
     *                                                    the tag <% %>
1879
     * @param string|null               $substitution_tag A tag that will be
1880
     *                                                    replaced with the
1881
     *                                                    unsubscribe URL. for
1882
     *                                                    example:
1883
     *                                                    [unsubscribe_url]. If this
1884
     *                                                    parameter is used, it will
1885
     *                                                    override both the text and
1886
     *                                                    html parameters. The URL
1887
     *                                                    of the link will be placed
1888
     *                                                    at the substitution tag’s
1889
     *                                                    location, with no
1890
     *                                                    additional formatting
1891
     */
1892
    public function setSubscriptionTracking(
1893
        $enable = null,
1894
        $text = null,
1895
        $html = null,
1896
        $substitution_tag = null
1897
    ) {
1898
        if (!($this->tracking_settings instanceof TrackingSettings)) {
1899
            $this->tracking_settings = new TrackingSettings();
1900
        }
1901
        $this->tracking_settings->setSubscriptionTracking(
1902
            $enable,
1903
            $text,
1904
            $html,
1905
            $substitution_tag
1906
        );
1907
    }
1908
1909
    /**
1910
     * Set the Google anatlyics settings on a TrackingSettings object
1911
     *
1912
     * @param bool|Ganalytics $enable       A Ganalytics object or a boolean to
1913
     *                                      determine if this setting
1914
     *                                      is active
1915
     * @param string|null     $utm_source   Name of the referrer source. (e.g.
1916
     *                                      Google, SomeDomain.com, or
1917
     *                                      Marketing Email)
1918
     * @param string|null     $utm_medium   Name of the marketing medium.
1919
     *                                      (e.g. Email)
1920
     * @param string|null     $utm_term     Used to identify any paid keywords.
1921
     * @param string|null     $utm_content  Used to differentiate your campaign
1922
     *                                      from advertisements
1923
     * @param string|null     $utm_campaign The name of the campaign
1924
     *
1925
     * @throws TypeException
1926
     */
1927
    public function setGanalytics(
1928
        $enable = null,
1929
        $utm_source = null,
1930
        $utm_medium = null,
1931
        $utm_term = null,
1932
        $utm_content = null,
1933
        $utm_campaign = null
1934
    ) {
1935
        if (!($this->tracking_settings instanceof TrackingSettings)) {
1936
            $this->tracking_settings = new TrackingSettings();
1937
        }
1938
        $this->tracking_settings->setGanalytics(
1939
            $enable,
1940
            $utm_source,
1941
            $utm_medium,
1942
            $utm_term,
1943
            $utm_content,
1944
            $utm_campaign
1945
        );
1946
    }
1947
1948
    /**
1949
     * Return an array representing a request object for the Twilio SendGrid API
1950
     *
1951
     * @return null|array
1952
     * @throws TypeException
1953
     */
1954
    #[\ReturnTypeWillChange]
1955
    public function jsonSerialize()
1956
    {
1957
        // Detect if we are using the new dynamic templates
1958
        if ($this->getTemplateId() !== null && strpos($this->getTemplateId()->getTemplateId(), 'd-') === 0) {
1959
            foreach ($this->personalization as $personalization) {
1960
                $personalization->setHasDynamicTemplate(true);
1961
            }
1962
        }
1963
1964
        return array_filter(
1965
            [
1966
                'personalizations' => array_values(array_filter(
1967
                    $this->getPersonalizations(),
0 ignored issues
show
Bug introduced by
It seems like $this->getPersonalizations() can also be of type null; however, parameter $array of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1967
                    /** @scrutinizer ignore-type */ $this->getPersonalizations(),
Loading history...
1968
                    static function ($value) {
1969
                        return null !== $value && null !== $value->jsonSerialize();
1970
                    }
1971
                )),
1972
                'from' => $this->getFrom(),
1973
                'reply_to' => $this->getReplyTo(),
1974
                'subject' => $this->getGlobalSubject(),
1975
                'content' => $this->getContents(),
1976
                'attachments' => $this->getAttachments(),
1977
                'template_id' => $this->getTemplateId(),
1978
                'sections' => $this->getSections(),
1979
                'headers' => $this->getGlobalHeaders(),
1980
                'categories' => $this->getCategories(),
1981
                'custom_args' => $this->getGlobalCustomArgs(),
1982
                'send_at' => $this->getGlobalSendAt(),
1983
                'batch_id' => $this->getBatchId(),
1984
                'asm' => $this->getASM(),
1985
                'ip_pool_name' => $this->getIpPoolName(),
1986
                'substitutions' => $this->getGlobalSubstitutions(),
1987
                'mail_settings' => $this->getMailSettings(),
1988
                'tracking_settings' => $this->getTrackingSettings()
1989
            ],
1990
            static function ($value) {
1991
                return $value !== null;
1992
            }
1993
        ) ?: null;
1994
    }
1995
}
1996