|
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\mailjet |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace sweelix\mailjet; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
use Mailjet\Client; |
|
19
|
|
|
use Mailjet\Resources; |
|
20
|
|
|
use yii\base\InvalidConfigException; |
|
21
|
|
|
use yii\mail\BaseMailer; |
|
22
|
|
|
use Exception; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This component allow user to send an email |
|
26
|
|
|
* |
|
27
|
|
|
* @author Philippe Gaultier <[email protected]> |
|
28
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
|
29
|
|
|
* @license http://www.sweelix.net/license license |
|
30
|
|
|
* @version XXX |
|
31
|
|
|
* @link http://www.sweelix.net |
|
32
|
|
|
* @package sweelix\mailjet |
|
33
|
|
|
* @since XXX |
|
34
|
|
|
* @todo implement batch messages using API |
|
35
|
|
|
*/ |
|
36
|
|
|
class Mailer extends BaseMailer |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $apiKey; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
public $apiSecret; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var boolean |
|
50
|
|
|
*/ |
|
51
|
|
|
public $enable = true; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
public $apiVersion = 'v3.1'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
public $apiUrl; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public $secured = true; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritdoc |
|
70
|
|
|
*/ |
|
71
|
|
|
public $messageClass = 'sweelix\mailjet\Message'; |
|
72
|
|
|
/** |
|
73
|
|
|
* @param Message $message |
|
74
|
|
|
* @since XXX |
|
75
|
|
|
* @throws InvalidConfigException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function sendMessage($message) |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
if ($this->apiKey === null) { |
|
81
|
|
|
throw new InvalidConfigException('API Key is missing'); |
|
82
|
|
|
} |
|
83
|
|
|
if ($this->apiSecret === null) { |
|
84
|
|
|
throw new InvalidConfigException('API Secret is missing'); |
|
85
|
|
|
} |
|
86
|
|
|
$settings = [ |
|
87
|
|
|
'secured' => $this->secured, |
|
88
|
|
|
'version' => $this->apiVersion, |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
if ($this->apiUrl !== null) { |
|
92
|
|
|
$settings['url'] = $this->apiUrl; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$client = new Client($this->apiKey, $this->apiSecret, $this->enable, $settings); |
|
96
|
|
|
|
|
97
|
|
|
$fromEmails = Message::convertEmails($message->getFrom()); |
|
98
|
|
|
$toEmails = Message::convertEmails($message->getTo()); |
|
99
|
|
|
|
|
100
|
|
|
$mailJetMessage = [ |
|
101
|
|
|
// 'FromEmail' => $fromEmails[0]['Email'], |
|
102
|
|
|
'From' => $fromEmails[0], |
|
103
|
|
|
'To' => $toEmails, |
|
104
|
|
|
]; |
|
105
|
|
|
/* |
|
106
|
|
|
if (isset($fromEmails[0]['Name']) === true) { |
|
107
|
|
|
$mailJetMessage['FromName'] = $fromEmails[0]['Name']; |
|
108
|
|
|
} |
|
109
|
|
|
*/ |
|
110
|
|
|
|
|
111
|
|
|
/* |
|
112
|
|
|
$sender = $message->getSender(); |
|
113
|
|
|
if (empty($sender) === false) { |
|
114
|
|
|
$sender = Message::convertEmails($sender); |
|
115
|
|
|
$mailJetMessage['Sender'] = $sender[0]; |
|
116
|
|
|
} |
|
117
|
|
|
*/ |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$cc = $message->getCc(); |
|
121
|
|
|
if (empty($cc) === false) { |
|
122
|
|
|
$cc = Message::convertEmails($cc); |
|
123
|
|
|
$mailJetMessage['Cc'] = $cc; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$bcc = $message->getBcc(); |
|
127
|
|
|
if (empty($cc) === false) { |
|
128
|
|
|
$bcc = Message::convertEmails($bcc); |
|
129
|
|
|
$mailJetMessage['Bcc'] = $bcc; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$attachments = $message->getAttachments(); |
|
133
|
|
|
if ($attachments !== null) { |
|
134
|
|
|
$mailJetMessage['Attachments'] = $attachments; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
//Get inilined attachments |
|
138
|
|
|
$inlinedAttachments = $message->getInlinedAttachments(); |
|
139
|
|
|
if ($inlinedAttachments !== null) { |
|
140
|
|
|
$mailJetMessage['InlinedAttachments'] = $inlinedAttachments; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$headers = $message->getHeaders(); |
|
144
|
|
|
if (empty($headers) === false) { |
|
145
|
|
|
$mailJetMessage['Headers'] = $headers; |
|
146
|
|
|
} |
|
147
|
|
|
$mailJetMessage['TrackOpens'] = $message->getTrackOpens(); |
|
148
|
|
|
$mailJetMessage['TrackClicks'] = $message->getTrackClicks(); |
|
149
|
|
|
|
|
150
|
|
|
$templateModel = $message->getTemplateModel(); |
|
151
|
|
|
if (empty($templateModel) === false) { |
|
152
|
|
|
$mailJetMessage['Variables'] = $templateModel; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$templateId = $message->getTemplateId(); |
|
156
|
|
|
if ($templateId === null) { |
|
157
|
|
|
$mailJetMessage['Subject'] = $message->getSubject(); |
|
158
|
|
|
$textBody = $message->getTextBody(); |
|
159
|
|
|
if (empty($textBody) === false) { |
|
160
|
|
|
$mailJetMessage['TextPart'] = $textBody; |
|
161
|
|
|
} |
|
162
|
|
|
$htmlBody = $message->getHtmlBody(); |
|
163
|
|
|
if (empty($htmlBody) === false) { |
|
164
|
|
|
$mailJetMessage['HTMLPart'] = $htmlBody; |
|
165
|
|
|
} |
|
166
|
|
|
$sendResult = $client->post(Resources::$Email, [ |
|
167
|
|
|
'body' => [ |
|
168
|
|
|
'Messages' => [ |
|
169
|
|
|
$mailJetMessage, |
|
170
|
|
|
] |
|
171
|
|
|
] |
|
172
|
|
|
]); |
|
173
|
|
|
} else { |
|
174
|
|
|
$mailJetMessage['TemplateID'] = $templateId; |
|
175
|
|
|
$processLanguage = $message->getTemplateLanguage(); |
|
176
|
|
|
if ($processLanguage === true) { |
|
177
|
|
|
$mailJetMessage['TemplateLanguage'] = $processLanguage; |
|
178
|
|
|
} |
|
179
|
|
|
$sendResult = $client->post(Resources::$Email, [ |
|
180
|
|
|
'body' => [ |
|
181
|
|
|
'Messages' => [ |
|
182
|
|
|
$mailJetMessage, |
|
183
|
|
|
] |
|
184
|
|
|
] |
|
185
|
|
|
]); |
|
186
|
|
|
} |
|
187
|
|
|
//TODO: handle error codes and log stuff |
|
188
|
|
|
return $sendResult->success(); |
|
189
|
|
|
} catch (Exception $e) { |
|
190
|
|
|
throw $e; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
} |