1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Mail.php |
4
|
|
|
* |
5
|
|
|
* PHP version 5.6+ |
6
|
|
|
* |
7
|
|
|
* @author Philippe Gaultier <[email protected]> |
8
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
9
|
|
|
* @license http://www.sweelix.net/license license |
10
|
|
|
* @version XXX |
11
|
|
|
* @link http://www.sweelix.net |
12
|
|
|
* @package sweelix\sendgrid |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace sweelix\sendgrid; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
use SendGrid\Exception as SendGridException; |
19
|
|
|
use SendGrid\Email as SendGridMail; |
20
|
|
|
use SendGrid as SendGridClient; |
21
|
|
|
use yii\base\InvalidConfigException; |
22
|
|
|
use yii\base\InvalidParamException; |
23
|
|
|
use yii\mail\BaseMailer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This component allow user to send an email |
27
|
|
|
* |
28
|
|
|
* @author Philippe Gaultier <[email protected]> |
29
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
30
|
|
|
* @license http://www.sweelix.net/license license |
31
|
|
|
* @version XXX |
32
|
|
|
* @link http://www.sweelix.net |
33
|
|
|
* @package sweelix\sendgrid |
34
|
|
|
* @since XXX |
35
|
|
|
* @todo implement batch messages using API |
36
|
|
|
*/ |
37
|
|
|
class Mailer extends BaseMailer |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string Sendgrid API Key |
41
|
|
|
*/ |
42
|
|
|
public $token; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Sendgrid login |
46
|
|
|
*/ |
47
|
|
|
public $user; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string Sendgrid password |
51
|
|
|
*/ |
52
|
|
|
public $password; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array options as defined in https://github.com/sendgrid/sendgrid-php#usage |
56
|
|
|
*/ |
57
|
|
|
public $options; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public $messageClass = 'sweelix\sendgrid\Message'; |
63
|
|
|
/** |
64
|
|
|
* @param Message $message |
65
|
|
|
* @since XXX |
66
|
|
|
* @throws InvalidConfigException |
67
|
|
|
*/ |
68
|
2 |
|
public function sendMessage($message) |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
2 |
|
if (($this->token === null) && ($this->user === null && $this->password === null)) { |
72
|
2 |
|
throw new InvalidConfigException('Token or login/password are missing'); |
73
|
|
|
} |
74
|
|
|
$client = null; |
75
|
|
|
if ($this->token !== null) { |
76
|
|
|
$client = new SendGridClient($this->token, $this->options); |
77
|
|
|
} elseif(($this->user !== null) && ($this->password !== null)) { |
78
|
|
|
$client = new SendGridClient($this->user, $this->password, $this->options); |
79
|
|
|
} |
80
|
|
|
if ($client === null) { |
81
|
|
|
throw new InvalidParamException('Email transport must be configured'); |
82
|
|
|
} |
83
|
|
|
$sendGridMail = new SendGridMail(); |
84
|
|
|
$replyTo = $message->getReplyTo(); |
85
|
|
|
if ($replyTo !== null) { |
86
|
|
|
$sendGridMail->setReplyTo($replyTo); |
87
|
|
|
} |
88
|
|
|
$sendGridMail->setFrom($message->getFrom()); |
89
|
|
|
if ($message->getFromName() !== null) { |
90
|
|
|
$sendGridMail->setFromName($message->getFromName()); |
91
|
|
|
} |
92
|
|
|
foreach($message->getTo() as $email => $name) { |
|
|
|
|
93
|
|
|
$sendGridMail->addTo($email, $name); |
94
|
|
|
} |
95
|
|
|
foreach($message->getCc() as $email => $name) { |
96
|
|
|
$sendGridMail->addCc($email, $name); |
97
|
|
|
} |
98
|
|
|
foreach($message->getBcc() as $email => $name) { |
99
|
|
|
$sendGridMail->addBcc($email, $name); |
100
|
|
|
} |
101
|
|
|
$sendGridMail->setSubject($message->getSubject()); |
102
|
|
|
foreach($message->getHeaders() as $header) { |
|
|
|
|
103
|
|
|
list($key, $value) = each($header); |
104
|
|
|
$sendGridMail->addHeader($key, $value); |
105
|
|
|
} |
106
|
|
|
foreach($message->getAttachments() as $attachment) { |
|
|
|
|
107
|
|
|
$cid = isset($attachment['ContentID']) ? $attachment['ContentID'] : null; |
108
|
|
|
$sendGridMail->addAttachment($attachment['File'], $attachment['Name'], $cid); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$templateId = $message->getTemplateId(); |
112
|
|
|
if ($templateId === null) { |
113
|
|
|
$data = $message->getHtmlBody(); |
114
|
|
|
if ($data !== null) { |
115
|
|
|
$sendGridMail->setHtml($data); |
116
|
|
|
} |
117
|
|
|
$data = $message->getTextBody(); |
118
|
|
|
if ($data !== null) { |
119
|
|
|
$sendGridMail->setText($data); |
120
|
|
|
} |
121
|
|
|
} else { |
122
|
|
|
$sendGridMail->setTemplateId($templateId); |
123
|
|
|
// trigger html template |
124
|
|
|
$sendGridMail->setHtml(' '); |
125
|
|
|
// trigger text template |
126
|
|
|
$sendGridMail->setText(' '); |
127
|
|
|
$templateModel = $message->getTemplateModel(); |
128
|
|
|
if (empty($templateModel) === false) { |
129
|
|
|
$sendGridMail->setSubstitutions($message->getTemplateModel()); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
$result = $client->send($sendGridMail); |
133
|
|
|
/* @var \SendGrid\Response $result */ |
134
|
|
|
return $result->code == 200; |
135
|
2 |
|
} catch (SendGridException $e) { |
136
|
|
|
throw $e; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.