forked from weluse/yii2-mailjet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailer.php
246 lines (191 loc) · 6.16 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Contains the Mailer class
*
* @package weluse/mailjet
*/
namespace weluse\mailjet;
use Mailjet\Resources;
use yii\base\InvalidConfigException;
use yii\base\UserException;
use yii\mail\BaseMailer;
use yii\validators\UrlValidator;
class Mailer extends BaseMailer
{
private $_mailjet;
private $_apikey;
private $_secret;
/**
* set your tracking event´s url
* bsp:
* [
* 'bounce' => 'http://yoururl.com/tracking/bounce',
* ]
*/
private $_tracking;
private $_allowedTrackingEvents = [
'sent',
'open',
'click',
'bounce',
'spam',
'blocked',
'unsub',
];
/**
* @var string message default class name.
*/
public $messageClass = 'weluse\mailjet\Message';
/**
* readonly
* @var $_response Mailjet\Response
*/
private $_response;
public function init()
{
if (!$this->_apikey) {
throw new InvalidConfigException(sprintf('"%s::apikey" cannot be null.', get_class($this)));
}
if (!$this->_secret) {
throw new InvalidConfigException(sprintf('"%s::secret" cannot be null.', get_class($this)));
}
try {
$this->createMailjet();
} catch (\Exception $exc) {
\Yii::error($exc->getMessage());
throw new \Exception('an error occurred with your mailer. Please check the application logs.', 500);
}
}
/**
* Sets the API secret key for Mailjet
*
* @param string $secret
* @throws InvalidConfigException
*/
public function setSecret($secret)
{
if (!is_string($secret)) {
throw new InvalidConfigException(sprintf('"%s::secret" should be a string, "%s" given.', get_class($this), gettype($apikey)));
}
$trimmedSecret = trim($secret);
if (!strlen($trimmedSecret) > 0) {
throw new InvalidConfigException(sprintf('"%s::secret" length should be greater than 0.', get_class($this)));
}
$this->_secret = $trimmedSecret;
}
/**
* Sets the API key for Mailjet
*
* @param string $apikey the Mailjet API key
* @throws InvalidConfigException
*/
public function setApikey($apikey)
{
if (!is_string($apikey)) {
throw new InvalidConfigException(sprintf('"%s::apikey" should be a string, "%s" given.', get_class($this), gettype($apikey)));
}
$trimmedApikey = trim($apikey);
if (!strlen($trimmedApikey) > 0) {
throw new InvalidConfigException(sprintf('"%s::apikey" length should be greater than 0.', get_class($this)));
}
$this->_apikey = $trimmedApikey;
}
/**
* Create the Mailjet Object
*/
public function createMailjet()
{
$mj = new \Mailjet\Client($this->_apikey, $this->_secret);
$this->_mailjet = $mj;
}
public function getResponse()
{
return $this->_response;
}
/**
* @inheritdoc
*/
protected function sendMessage($message)
{
$recipients = [];
foreach ($message->to as $email => $name) {
$newRecipient = [];
if (!empty($email)) {
$newRecipient['Email'] = $email;
}
if (!empty($name)) {
$newRecipient['Name'] = $name;
}
$recipients[] = $newRecipient;
}
$body = [
'Subject' => $message->subject,
'Text-part' => $message->textBody,
'Html-part' => $message->htmlBody,
'Recipients' => $recipients
];
$body = array_merge($message->from, $body);
$response = $this->_mailjet->post(Resources::$Email, ['body' => $body]);
$this->_response = $response;
return $response->success();
}
public function setTracking($tracking)
{
if (is_array($tracking)) {
$urlValidator = new UrlValidator;
foreach ($tracking as $event => $url) {
if (in_array($event, $this->_allowedTrackingEvents)) {
if (!$urlValidator->validate($url)) {
throw new InvalidConfigException(sprintf('"%s::%s" should be a url', get_class($this), $event));
}
$this->_tracking[$event] = $url;
} else {
throw new InvalidConfigException(sprintf('the %s event is not supported', $event));
}
}
} else {
throw new InvalidConfigException('The trackingActions must be an array');
}
}
public function activateAllTrackings()
{
foreach ($this->_tracking as $event => $url) {
$this->activateTracking($event, $url);
}
return true;
}
public function activateTracking($event, $url)
{
$body = [
'EventType' => $event,
'Url' => $url,
];
$response = $this->_mailjet->post(Resources::$Eventcallbackurl, ['body' => $body]);
if (!$response->success()) {
$eventCallbackurl = Resources::$Eventcallbackurl;
$eventCallbackurl[1] = $event;
$eventExist = $this->_mailjet->get($eventCallbackurl);
$responseData = $eventExist->getData();
/* check if is the tracking url the same */
if ($responseData[0]['Url'] != $url) {
throw new UserException('You must clear your old tracking urls first: Yii::$app->mailer->clearAllTrackings(); or Yii::$app->mailer->clearTracking(\'' . $event . '\');');
}
}
return true;
}
public function clearAllTrackings()
{
foreach ($this->_tracking as $event => $url) {
$this->clearTracking($event);
}
}
public function clearTracking($event)
{
if (!in_array($event, $this->_allowedTrackingEvents)) {
throw new InvalidConfigException(sprintf('the %s event is not supported', $event));
}
$eventCallbackurl = Resources::$Eventcallbackurl;
$eventCallbackurl[1] = $event;
$response = $this->_mailjet->delete($eventCallbackurl);
}
}