Issues (3627)

SendGrid/Mail/SendGridMailPersonalization.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\EmailBundle\Swiftmailer\SendGrid\Mail;
13
14
use Mautic\EmailBundle\Swiftmailer\Message\MauticMessage;
15
use SendGrid\Email;
16
use SendGrid\Mail;
17
use SendGrid\Personalization;
18
19
class SendGridMailPersonalization
20
{
21
    public function addPersonalizedDataToMail(Mail $mail, \Swift_Mime_SimpleMessage $message)
22
    {
23
        if (!$message instanceof MauticMessage) { //Used for "Send test email" in settings
24
            foreach ($message->getTo() as $recipientEmail => $recipientName) {
25
                $personalization = new Personalization();
26
                $to              = new Email($recipientName, $recipientEmail);
27
                $personalization->addTo($to);
28
                $mail->addPersonalization($personalization);
29
            }
30
31
            return;
32
        }
33
34
        $metadata = $message->getMetadata();
35
        $ccEmail  = $message->getCc();
36
        if ($ccEmail) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ccEmail of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
37
            $cc = new Email(current($ccEmail), key($ccEmail));
38
        }
39
        foreach ($message->getTo() as $recipientEmail => $recipientName) {
40
            if (empty($metadata[$recipientEmail])) {
41
                //Recipient is not in metadata = we do not have tokens for this email.
42
                continue;
43
            }
44
            $personalization = new Personalization();
45
            $to              = new Email($recipientName, $recipientEmail);
46
            $personalization->addTo($to);
47
48
            if (isset($cc)) {
49
                $clone = clone $cc;
50
                $personalization->addCc($clone);
51
            }
52
53
            foreach ($metadata[$recipientEmail]['tokens'] as $token => $value) {
54
                $personalization->addSubstitution($token, (string) $value);
55
            }
56
57
            $mail->addPersonalization($personalization);
58
            unset($metadata[$recipientEmail]);
59
        }
60
    }
61
}
62