-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostmark.php
More file actions
347 lines (303 loc) · 8.68 KB
/
Copy pathPostmark.php
File metadata and controls
347 lines (303 loc) · 8.68 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* Emailmanager PHP class
*
* Copyright 2010, Markus Hedlund, Mimmin AB, www.mimmin.com
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Markus Hedlund (markus@mimmin.com) at mimmin (www.mimmin.com)
* @copyright Copyright 2009, Markus Hedlund, Mimmin AB, www.mimmin.com
* @version 0.3
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
* Usage:
* Mail_Emailmanager::compose()
* ->to('address@example.com', 'Name')
* ->subject('Subject')
* ->messagePlain('Plaintext message')
* ->tag('Test tag')
* ->send();
*
* or:
*
* $email = new Mail_Emailmanager();
* $email->to('address@example.com', 'Name')
* ->subject('Subject')
* ->messagePlain('Plaintext message')
* ->tag('Test tag')
* ->send();
*/
class Mail_Emailmanager
{
const DEBUG_OFF = 0;
const DEBUG_VERBOSE = 1;
const DEBUG_RETURN = 2;
private $_fromName;
private $_fromAddress;
private $_tag;
private $_toName;
private $_toAddress;
private $_replyToName;
private $_replyToAddress;
private $_cc = array();
private $_bcc = array();
private $_subject;
private $_messagePlain;
private $_messageHtml;
private $_debugMode = self::DEBUG_OFF;
/**
* Initialize
*/
public function __construct()
{
$this->_default('EMAILMANAGERAPP_MAIL_FROM_NAME', null);
$this->_default('EMAILMANAGERAPP_MAIL_FROM_ADDRESS', null);
$this->_default('EMAILMANAGERAPP_API_KEY', null);
$this->from(EMAILMANAGERAPP_MAIL_FROM_ADDRESS, EMAILMANAGERAPP_MAIL_FROM_NAME)->messageHtml(null)->messagePlain(null);
}
/**
* New e-mail
* @return Mail_Emailmanager
*/
public static function compose()
{
return new self();
}
/**
* Turns debug output on
* @param int $mode One of the debug constants
* @return Mail_Emailmanager
*/
public function &debug($mode = self::DEBUG_VERBOSE)
{
$this->_debugMode = $mode;
return $this;
}
/**
* Specify sender. Overwrites default From.
* @param string $address E-mail address used in From
* @param string $name Optional. Name used in From
* @return Mail_Emailmanager
*/
public function &from($address, $name = null)
{
$this->_fromAddress = $address;
$this->_fromName = $name;
return $this;
}
/**
* Specify sender name. Overwrites default From name, but doesn't change address.
* @param string $name Name used in From
* @return Mail_Emailmanager
*/
public function &fromName($name)
{
$this->_fromName = $name;
return $this;
}
/**
* You can categorize outgoing email using the optional Tag property.
* If you use different tags for the different types of emails your
* application generates, you will be able to get detailed statistics
* for them through the Emailmanager user interface.
* Only 1 tag per mail is supported.
*
* @param string $tag One tag
* @return Mail_Emailmanager
*/
public function &tag($tag)
{
$this->_tag = $tag;
return $this;
}
/**
* Specify receiver
* @param string $address E-mail address used in To
* @param string $name Optional. Name used in To
* @return Mail_Emailmanager
*/
public function &to($address, $name = null)
{
$this->_toAddress = $address;
$this->_toName = $name;
return $this;
}
/**
* Specify reply-to
* @param string $address E-mail address used in To
* @param string $name Optional. Name used in To
* @return Mail_Emailmanager
*/
public function &replyTo($address, $name = null)
{
$this->_replyToAddress = $address;
$this->_replyToName = $name;
return $this;
}
/**
* Add a CC address
* @param string $address E-mail address used in CC
* @param string $name Optional. Name used in CC
* @return Mail_Emailmanager
*/
public function &addCC($address, $name = null)
{
$this->_cc[] = (is_null($name) ? $address : "$name <$address>");
return $this;
}
/**
* Add a BCC address
* @param string $address E-mail address used in BCC
* @param string $name Optional. Name used in BCC
* @return Mail_Emailmanager
*/
public function &addBCC($address, $name = null)
{
$this->_bcc[] = (is_null($name) ? $address : "$name <$address>");
return $this;
}
/**
* Specify subject
* @param string $subject E-mail subject
* @return Mail_Emailmanager
*/
public function &subject($subject)
{
$this->_subject = $subject;
return $this;
}
/**
* Add plaintext message. Can be used in conjunction with messageHtml()
* @param string $message E-mail message
* @return Mail_Emailmanager
*/
public function &messagePlain($message)
{
$this->_messagePlain = $message;
return $this;
}
/**
* Add HTML message. Can be used in conjunction with messagePlain()
* @param string $message E-mail message
* @return Mail_Emailmanager
*/
public function &messageHtml($message)
{
$this->_messageHtml = $message;
return $this;
}
/**
* Sends the e-mail. Prints debug output if debug mode is turned on
* @return Mail_Emailmanager
*/
public function &send()
{
if (is_null(EMAILMANAGERAPP_API_KEY)) {
throw new Exception('Emailmanager API key is not set');
}
if (is_null($this->_fromAddress)) {
throw new Exception('From address is not set');
}
if (!isset($this->_toAddress)) {
throw new Exception('To address is not set');
}
if (!$this->_validateAddress($this->_fromAddress)) {
throw new Exception("Invalid from address '{$this->_fromAddress}'");
}
if (!$this->_validateAddress($this->_toAddress)) {
throw new Exception("Invalid to address '{$this->_toAddress}'");
}
if (isset($this->_replyToAddress) && !$this->_validateAddress($this->_replyToAddress)) {
throw new Exception("Invalid reply to address '{$this->_replyToAddress}'");
}
if (1 + count($this->_cc) + count($this->_bcc) > 20) {
throw new Exception("Too many email recipients");
}
$data = $this->_prepareData();
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'X-Emailmanager-Server-Token: ' . EMAILMANAGERAPP_API_KEY
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://trans.emailmanager.com/email');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$return = curl_exec($ch);
if ($this->_debugMode == self::DEBUG_VERBOSE) {
echo "JSON: " . json_encode($data) . "\nHeaders: \n\t" . implode("\n\t", $headers) . "\nReturn:\n$return";
} else if ($this->_debugMode == self::DEBUG_RETURN) {
return array(
'json' => json_encode($data),
'headers' => $headers,
'return' => $return
);
}
if (curl_error($ch) != '') {
throw new Exception(curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (!$this->_isTwoHundred($httpCode)) {
$message = json_decode($return)->Message;
throw new Exception("Error while mailing. Emailmanager returned HTTP code $httpCode with message \"$message\"");
}
return $this;
}
/**
* Prepares the data array
*/
private function _prepareData()
{
$data = array(
'Subject' => $this->_subject
);
$data['From'] = is_null($this->_fromName) ? $this->_fromAddress : "{$this->_fromName} <{$this->_fromAddress}>";
$data['To'] = is_null($this->_toName) ? $this->_toAddress : "{$this->_toName} <{$this->_toAddress}>";
if (!is_null($this->_messageHtml)) {
$data['HtmlBody'] = $this->_messageHtml;
}
if (!is_null($this->_messagePlain)) {
$data['TextBody'] = $this->_messagePlain;
}
if (!is_null($this->_tag)) {
$data['Tag'] = $this->_tag;
}
if (!is_null($this->_replyToAddress)) {
$data['ReplyTo'] = is_null($this->_replyToName) ? $this->_replyToAddress : "{$this->_replyToName} <{$this->_replyToAddress}>";
}
if (!empty($this->_cc)) {
$data['Cc'] = implode(',',$this->_cc);
}
if (!empty($this->_bcc)) {
$data['Bcc'] = implode(',',$this->_bcc);
}
return $data;
}
/**
* If a number is 200-299
*/
private function _isTwoHundred($value)
{
return intval($value / 100) == 2;
}
/**
* Defines a constant, if it isn't defined
*/
private function _default($name, $default)
{
if (!defined($name)) {
define($name, $default);
}
}
/**
* Validates an e-mailadress
*/
private function _validateAddress($email)
{
// http://php.net/manual/en/function.filter-var.php
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
}