-
Notifications
You must be signed in to change notification settings - Fork 100
/
SimpleEmailServiceMessage.php
635 lines (515 loc) · 18.5 KB
/
SimpleEmailServiceMessage.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
<?php
/**
* SimpleEmailServiceMessage PHP class
*
* @link https://github.com/daniel-zahariev/php-aws-ses
* @package AmazonSimpleEmailService
* @version v0.9.5
*/
final class SimpleEmailServiceMessage {
// these are public for convenience only
// these are not to be used outside of the SimpleEmailService class!
public $to, $cc, $bcc, $replyto, $recipientsCharset;
public $from, $returnpath;
public $subject, $messagetext, $messagehtml;
public $subjectCharset, $messageTextCharset, $messageHtmlCharset;
public $attachments, $customHeaders, $configuration_set, $message_tags;
public $is_clean, $raw_message;
public function __construct() {
$this->to = array();
$this->cc = array();
$this->bcc = array();
$this->replyto = array();
$this->recipientsCharset = 'UTF-8';
$this->from = null;
$this->returnpath = null;
$this->subject = null;
$this->messagetext = null;
$this->messagehtml = null;
$this->subjectCharset = 'UTF-8';
$this->messageTextCharset = 'UTF-8';
$this->messageHtmlCharset = 'UTF-8';
$this->attachments = array();
$this->customHeaders = array();
$this->configuration_set = null;
$this->message_tags = array();
$this->is_clean = true;
$this->raw_message = null;
}
/**
* addTo, addCC, addBCC, and addReplyTo have the following behavior:
* If a single address is passed, it is appended to the current list of addresses.
* If an array of addresses is passed, that array is merged into the current list.
*
* @return SimpleEmailServiceMessage $this
* @link http://docs.aws.amazon.com/ses/latest/APIReference/API_Destination.html
*/
public function addTo($to) {
if (!is_array($to)) {
$this->to[] = $to;
} else {
$this->to = array_unique(array_merge($this->to, $to));
}
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setTo($to) {
$this->to = (array) $to;
$this->is_clean = false;
return $this;
}
/**
* Clear the To: email address(es) for the message
*
* @return SimpleEmailServiceMessage $this
*/
public function clearTo() {
$this->to = array();
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
* @see addTo()
*/
public function addCC($cc) {
if (!is_array($cc)) {
$this->cc[] = $cc;
} else {
$this->cc = array_merge($this->cc, $cc);
}
$this->is_clean = false;
return $this;
}
/**
* Clear the CC: email address(es) for the message
*
* @return SimpleEmailServiceMessage $this
*/
public function clearCC() {
$this->cc = array();
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
* @see addTo()
*/
public function addBCC($bcc) {
if (!is_array($bcc)) {
$this->bcc[] = $bcc;
} else {
$this->bcc = array_merge($this->bcc, $bcc);
}
$this->is_clean = false;
return $this;
}
/**
* Clear the BCC: email address(es) for the message
*
* @return SimpleEmailServiceMessage $this
*/
public function clearBCC() {
$this->bcc = array();
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
* @see addTo()
*/
public function addReplyTo($replyto) {
if (!is_array($replyto)) {
$this->replyto[] = $replyto;
} else {
$this->replyto = array_merge($this->replyto, $replyto);
}
$this->is_clean = false;
return $this;
}
/**
* Clear the Reply-To: email address(es) for the message
*
* @return SimpleEmailServiceMessage $this
*/
public function clearReplyTo() {
$this->replyto = array();
$this->is_clean = false;
return $this;
}
/**
* Clear all of the message recipients in one go
*
* @return SimpleEmailServiceMessage $this
* @uses clearTo()
* @uses clearCC()
* @uses clearBCC()
* @uses clearReplyTo()
*/
public function clearRecipients() {
$this->clearTo();
$this->clearCC();
$this->clearBCC();
$this->clearReplyTo();
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setFrom($from) {
$this->from = $from;
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setReturnPath($returnpath) {
$this->returnpath = $returnpath;
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setRecipientsCharset($charset) {
$this->recipientsCharset = $charset;
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setSubject($subject) {
$this->subject = $subject;
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setSubjectCharset($charset) {
$this->subjectCharset = $charset;
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
* @link http://docs.aws.amazon.com/ses/latest/APIReference/API_Message.html
*/
public function setMessageFromString($text, $html = null) {
$this->messagetext = $text;
$this->messagehtml = $html;
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setMessageFromFile($textfile, $htmlfile = null) {
if (file_exists($textfile) && is_file($textfile) && is_readable($textfile)) {
$this->messagetext = file_get_contents($textfile);
} else {
$this->messagetext = null;
}
if (file_exists($htmlfile) && is_file($htmlfile) && is_readable($htmlfile)) {
$this->messagehtml = file_get_contents($htmlfile);
} else {
$this->messagehtml = null;
}
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setMessageFromURL($texturl, $htmlurl = null) {
if ($texturl !== null) {
$this->messagetext = file_get_contents($texturl);
} else {
$this->messagetext = null;
}
if ($htmlurl !== null) {
$this->messagehtml = file_get_contents($htmlurl);
} else {
$this->messagehtml = null;
}
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setMessageCharset($textCharset, $htmlCharset = null) {
$this->messageTextCharset = $textCharset;
$this->messageHtmlCharset = $htmlCharset;
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function setConfigurationSet($configuration_set = null) {
$this->configuration_set = $configuration_set;
$this->is_clean = false;
return $this;
}
/**
* @return array $message_tags
*/
public function getMessageTags() {
return $this->message_tags;
}
/**
* @return null|mixed $message_tag
*/
public function getMessageTag($key) {
return isset($this->message_tags[$key]) ? $this->message_tags[$key] : null;
}
/**
* Add Message tag
*
* Both key and value can contain only ASCII letters (a-z, A-Z), numbers (0-9), underscores (_), or dashes (-) and be less than 256 characters.
*
* @param string $key
* @param mixed $value
* @return SimpleEmailServiceMessage $this
* @link https://docs.aws.amazon.com/ses/latest/DeveloperGuide/event-publishing-send-email.html
* @link https://docs.aws.amazon.com/ses/latest/APIReference/API_MessageTag.html
*/
public function setMessageTag($key, $value) {
$this->message_tags[$key] = $value;
$this->is_clean = false;
return $this;
}
/**
* @param string $key The key of the tag to be removed
* @return SimpleEmailServiceMessage $this
*/
public function removeMessageTag($key) {
unset($this->message_tags[$key]);
$this->is_clean = false;
return $this;
}
/**
* @param array $message_tags
* @return SimpleEmailServiceMessage $this
*/
public function setMessageTags($message_tags = array()) {
$this->message_tags = array_merge($this->message_tags, $message_tags);
$this->is_clean = false;
return $this;
}
/**
* @return SimpleEmailServiceMessage $this
*/
public function removeMessageTags() {
$this->message_tags = array();
$this->is_clean = false;
return $this;
}
/**
* Add custom header - this works only with SendRawEmail
*
* @param string $header Your custom header
* @return SimpleEmailServiceMessage $this
* @link( Restrictions on headers, http://docs.aws.amazon.com/ses/latest/DeveloperGuide/header-fields.html)
*/
public function addCustomHeader($header) {
$this->customHeaders[] = $header;
$this->is_clean = false;
return $this;
}
/**
* Add email attachment by directly passing the content
*
* @param string $name The name of the file attachment as it will appear in the email
* @param string $data The contents of the attachment file
* @param string $mimeType Specify custom MIME type
* @param string $contentId Content ID of the attachment for inclusion in the mail message
* @param string $attachmentType Attachment type: attachment or inline
* @return SimpleEmailServiceMessage $this
*/
public function addAttachmentFromData($name, $data, $mimeType = 'application/octet-stream', $contentId = null, $attachmentType = 'attachment') {
$this->attachments[$name] = array(
'name' => $name,
'mimeType' => $mimeType,
'data' => $data,
'contentId' => $contentId,
'attachmentType' => ($attachmentType == 'inline' ? 'inline; filename="' . $name . '"' : $attachmentType),
);
$this->is_clean = false;
return $this;
}
/**
* Add email attachment by passing file path
*
* @param string $name The name of the file attachment as it will appear in the email
* @param string $path Path to the attachment file
* @param string $mimeType Specify custom MIME type
* @param string $contentId Content ID of the attachment for inclusion in the mail message
* @param string $attachmentType Attachment type: attachment or inline
* @return boolean Status of the operation
*/
public function addAttachmentFromFile($name, $path, $mimeType = 'application/octet-stream', $contentId = null, $attachmentType = 'attachment') {
if (file_exists($path) && is_file($path) && is_readable($path)) {
$this->addAttachmentFromData($name, file_get_contents($path), $mimeType, $contentId, $attachmentType);
return true;
}
$this->is_clean = false;
return false;
}
/**
* Add email attachment by passing file path
*
* @param string $name The name of the file attachment as it will appear in the email
* @param string $url URL to the attachment file
* @param string $mimeType Specify custom MIME type
* @param string $contentId Content ID of the attachment for inclusion in the mail message
* @param string $attachmentType Attachment type: attachment or inline
* @return boolean Status of the operation
*/
public function addAttachmentFromUrl($name, $url, $mimeType = 'application/octet-stream', $contentId = null, $attachmentType = 'attachment') {
$data = file_get_contents($url);
if ($data !== false) {
$this->addAttachmentFromData($name, $data, $mimeType, $contentId, $attachmentType);
return true;
}
$this->is_clean = false;
return false;
}
/**
* Get the existence of attached inline messages
*
* @return boolean
*/
public function hasInlineAttachments() {
foreach ($this->attachments as $attachment) {
if ($attachment['attachmentType'] != 'attachment') {
return true;
}
}
return false;
}
/**
* Get the raw mail message
*
* @return string
*/
public function getRawMessage($encode = true) {
if ($this->is_clean && !is_null($this->raw_message) && $encode) {
return $this->raw_message;
}
$this->is_clean = true;
$boundary = uniqid(rand(), true);
$raw_message = count($this->customHeaders) > 0 ? join("\n", $this->customHeaders) . "\n" : '';
if (!empty($this->message_tags)) {
$message_tags = array();
foreach ($this->message_tags as $key => $value) {
$message_tags[] = "{$key}={$value}";
}
$raw_message .= 'X-SES-MESSAGE-TAGS: ' . join(', ', $message_tags) . "\n";
}
if (!is_null($this->configuration_set)) {
$raw_message .= 'X-SES-CONFIGURATION-SET: ' . $this->configuration_set . "\n";
}
$raw_message .= count($this->to) > 0 ? 'To: ' . $this->encodeRecipients($this->to) . "\n" : '';
$raw_message .= 'From: ' . $this->encodeRecipients($this->from) . "\n";
if (!empty($this->replyto)) {
$raw_message .= 'Reply-To: ' . $this->encodeRecipients($this->replyto) . "\n";
}
if (!empty($this->cc)) {
$raw_message .= 'CC: ' . $this->encodeRecipients($this->cc) . "\n";
}
if (!empty($this->bcc)) {
$raw_message .= 'BCC: ' . $this->encodeRecipients($this->bcc) . "\n";
}
if ($this->subject != null && strlen($this->subject) > 0) {
$raw_message .= 'Subject: =?' . $this->subjectCharset . '?B?' . base64_encode($this->subject) . "?=\n";
}
$raw_message .= 'MIME-Version: 1.0' . "\n";
$raw_message .= 'Content-type: ' . ($this->hasInlineAttachments() ? 'multipart/related' : 'Multipart/Mixed') . '; boundary="' . $boundary . '"' . "\n";
$raw_message .= "\n--{$boundary}\n";
$raw_message .= 'Content-type: Multipart/Alternative; boundary="alt-' . $boundary . '"' . "\n";
if ($this->messagetext != null && strlen($this->messagetext) > 0) {
$charset = empty($this->messageTextCharset) ? '' : "; charset=\"{$this->messageTextCharset}\"";
$raw_message .= "\n--alt-{$boundary}\n";
$raw_message .= 'Content-Type: text/plain' . $charset . "\n\n";
$raw_message .= $this->messagetext . "\n";
}
if ($this->messagehtml != null && strlen($this->messagehtml) > 0) {
$charset = empty($this->messageHtmlCharset) ? '' : "; charset=\"{$this->messageHtmlCharset}\"";
$raw_message .= "\n--alt-{$boundary}\n";
$raw_message .= 'Content-Type: text/html' . $charset . "\n\n";
$raw_message .= $this->messagehtml . "\n";
}
$raw_message .= "\n--alt-{$boundary}--\n";
foreach ($this->attachments as $attachment) {
$raw_message .= "\n--{$boundary}\n";
$raw_message .= 'Content-Type: ' . $attachment['mimeType'] . '; name="' . $attachment['name'] . '"' . "\n";
$raw_message .= 'Content-Disposition: ' . $attachment['attachmentType'] . "\n";
if (!empty($attachment['contentId'])) {
$raw_message .= 'Content-ID: ' . $attachment['contentId'] . '' . "\n";
}
$raw_message .= 'Content-Transfer-Encoding: base64' . "\n";
$raw_message .= "\n" . chunk_split(base64_encode($attachment['data']), 76, "\n") . "\n";
}
$raw_message .= "\n--{$boundary}--\n";
if (!$encode) {
return $raw_message;
}
$this->raw_message = base64_encode($raw_message);
return $this->raw_message;
}
/**
* Encode recipient with the specified charset in `recipientsCharset`
*
* @return string Encoded recipients joined with comma
*/
public function encodeRecipients($recipient) {
if (is_array($recipient)) {
return join(', ', array_map(array($this, 'encodeRecipients'), $recipient));
}
if (preg_match("/(.*)<(.*)>/", $recipient, $regs)) {
$recipient = '=?' . $this->recipientsCharset . '?B?' . base64_encode($regs[1]) . '?= <' . $regs[2] . '>';
}
return $recipient;
}
/**
* Validates whether the message object has sufficient information to submit a request to SES.
*
* This does not guarantee the message will arrive, nor that the request will succeed;
* instead, it makes sure that no required fields are missing.
*
* This is used internally before attempting a SendEmail or SendRawEmail request,
* but it can be used outside of this file if verification is desired.
* May be useful if e.g. the data is being populated from a form; developers can generally
* use this function to verify completeness instead of writing custom logic.
*
* @return boolean
*/
public function validate() {
// at least one destination is required
if (count($this->to) == 0 && count($this->cc) == 0 && count($this->bcc) == 0) {
return false;
}
// sender is required
if ($this->from == null || strlen($this->from) == 0) {
return false;
}
// subject is required
if (($this->subject == null || strlen($this->subject) == 0)) {
return false;
}
// message is required
if ((empty($this->messagetext) || strlen((string) $this->messagetext) == 0)
&& (empty($this->messagehtml) || strlen((string) $this->messagehtml) == 0)) {
return false;
}
return true;
}
}