forked from slot/MandrillBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.php
809 lines (694 loc) · 17.6 KB
/
Message.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
<?php
namespace Hip\MandrillBundle;
/**
* A mandrill message
*
* Copyright (c) 2013 Hipaway Travel GmbH, Berlin
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @author: Sven Loth <sven.loth@hipaway.com>
* @copyright: 2013 Hipaway Travel GmbH, Berlin
*/
class Message
{
/**
* the full HTML content to be sent
*
* @var string
*/
protected $html = '';
/**
* optional full text content to be sent
*
* @var string
*/
protected $text;
/**
* the message subject
*
* @var string
*/
protected $subject;
/**
* an array of recipient information
*
* @var array
*/
protected $to = array();
/**
* the sender email address (required)
*
* @var string
*/
protected $fromEmail;
/**
* optional from name to be used
*
* @var string
*/
protected $fromName;
/**
* optional extra headers to add to the message (currently only Reply-To and X-* headers are allowed)
*
* @var array
*/
protected $headers = array();
/**
* whether or not to turn on open tracking for the message
*
* @var bool
*/
protected $trackOpens = true;
/**
* whether or not to turn on click tracking for the message
*
* @var bool
*/
protected $trackClicks = true;
/**
* whether or not to automatically generate a text part for messages that are not given text
*
* @var bool
*/
protected $autoText = true;
/**
* whether or not to strip the query string from URLs when aggregating tracked URL data
*
* @var bool
*/
protected $urlStripQs = true;
/**
* whether or not to expose all recipients in to "To" header for each email
*
* @var bool
*/
protected $preserveRecipients = true;
/**
* an optional address to receive an exact copy of each recipient's email
*
* @var string
*/
protected $bccAddress;
/**
* whether to evaluate merge tags in the message.
* Will automatically be set to true if either merge_vars or global_merge_vars are provided.
*
* @var bool
*/
protected $merge = false;
/**
* global merge variables to use for all recipients. You can override these per recipient.
*
* @var array
*/
protected $globalMergeVars = array();
/**
* per-recipient merge variables, which override global merge variables with the same name.
*
* @var array
*/
protected $mergeVars = array();
/**
* an array of string to tag the message with.
*
* Stats are accumulated using tags, though we only store
* the first 100 we see, so this should not be unique or
* change frequently. Tags should be 50 characters or less.
* Any tags starting with an underscore are reserved for
* internal use and will cause errors.
*
* @var array
*/
protected $tags = array();
/**
* an array of strings indicating for which any matching URLs
* will automatically have Google Analytics parameters appended
* to their query string automatically.
*
* @var array
*/
protected $googleAnalyticsDomains = array();
/**
* optional string indicating the value to set for the utm_campaign
* tracking parameter. If this isn't provided the email's from
* address will be used instead.
*
* @var string
*/
protected $googleAnalyticsCampaign = '';
/**
* metadata an associative array of user metadata. Mandrill
* will store this metadata and make it available for retrieval.
* In addition, you can select up to 10 metadata fields to index
* and make searchable using the Mandrill search api.
*
* @var array
*/
protected $metadata = array();
/**
* Per-recipient metadata that will override the global values specified
* in the metadata parameter.
*
* @var array
*/
protected $recipientMetadata = array();
/**
* an array of supported attachments to add to the message
*
* @var array
*/
protected $attachments = array();
/**
* Add a recipient
*
* @param string $email
* @param string $name
* @param string $type
*
* @return Message
*/
public function addTo($email, $name = '', $type = 'to')
{
$this->to[] = array('email' => $email, 'name' => $name, 'type' => $type);
return $this;
}
/**
* Add extra header
*
* currently only Reply-To and X-* headers are allowed
*
* @param $key
* @param $value
*
* @throws \Exception
* @return Message
*/
public function addHeader($key, $value)
{
$this->headers[$key] = $value;
return $this;
}
/**
* Set global merge variable to use for all recipients.
* You can override these per recipient.
*
* @param string $name
* @param string $content
*
* @return Message
*/
public function addGlobalMergeVar($name, $content)
{
$this->globalMergeVars[] = array(
'name' => $name,
'content' => $content,
);
return $this;
}
/**
* Add per-recipient merge variable,
* which override global merge variables with the same name.
*
* @param string $recipient
* @param string $name
* @param string $content
*
* @return Message
*/
public function addMergeVar($recipient, $name, $content)
{
$this->mergeVars[] = array(
'rcpt' => $recipient,
'vars' => array(
array(
'name' => $name,
'content' => $content
)
)
);
return $this;
}
/**
* Add several per-recipient merge variables,
* which override global merge variables with the same name.
*
* @param string $recipient
* @param array $data
*
* @return Message
*/
public function addMergeVars($recipient, $data)
{
$vars = array();
foreach ( $data as $name => $content ) {
$vars[] = array('name' => $name, 'content' => $content);
}
$this->mergeVars[] = array(
'rcpt' => $recipient,
'vars' => $vars
);
return $this;
}
/**
* Add a string to tag the message with.
*
* Stats are accumulated using tags, though we only store the first 100 we see,
* so this should not be unique or change frequently. Tags should be 50
* characters or less. Any tags starting with an underscore are reserved
* for internal use and will cause errors.
*
* @param string $tag
*
* @throws \Exception
* @return Message
*/
public function addTag($tag)
{
if (substr($tag, 0, 1) == '_') {
throw new \Exception('tag cannot start with underscore');
}
$this->tags[] = $tag;
return $this;
}
/**
* Add google Analytics domain
*
* @param string $domain
*
* @return Message
*/
public function addGoogleAnalyticsDomain($domain)
{
// todo: add domain name validator
$this->googleAnalyticsDomains[] = $domain;
return $this;
}
/**
* Add global metadata.
*
* Mandrill will store this metadata and make it available for retrieval.
* In addition, you can select up to 10 metadata fields to index and
* make searchable using the Mandrill search api.
*
* @param string $data
*
* @return Message
*/
public function addMetadata($data)
{
$this->metadata[] = $data;
return $this;
}
/**
* Add Per-recipient metadata that will override the global
* values specified in the metadata parameter.
*
* @param string $recipient
* @param string $data
*
* @return Message
*/
public function addRecipientMetadata($recipient, $data)
{
foreach ($this->recipientMetadata as $idx => $rcptMetadata) {
if (isset($rcptMetadata['rcpt']) && $rcptMetadata['rcpt'] == $recipient) {
$this->recipientMetadata[$idx]['values'][] = $data;
return $this;
}
}
$this->recipientMetadata[] = array('rcpt' => $recipient, 'values' => array($data));
return $this;
}
/**
* Add supported attachments to add to the message
*
* @param string $type - the MIME type of the attachment - allowed types are text/*, image/*, and application/pdf
* @param string $name - the file name of the attachment
* @param string $data - base64 encoded attachment data
*
* @return Message
*/
public function addAttachment($type, $name, $data)
{
$this->attachments[] = array(
'type' => $type,
'name' => $name,
'content' => $data
);
return $this;
}
public function addAttachmentFromPath($path, $type = '', $name = '') {
if (!is_readable($path)) {
throw new \Exception('cannot read file ' . $path);
}
$data = file_get_contents($path);
$base64data = base64_encode($data);
if (empty($name)) {
$name = basename($path);
}
if (empty($type)) {
// open fileinfo database
$finfo = finfo_open(FILEINFO_MIME, '/usr/share/misc/magic');
if (!$finfo) {
throw new \Exception('Opening fileinfo database failed, please specify type');
}
$type = finfo_file($finfo, $path);
finfo_close($finfo);
}
$this->addAttachment($type, $name, $base64data);
return $this;
}
/**
* an optional address to receive an exact copy of each recipient's email
*
* @param string $bccAddress
* @return \Hip\MandrillBundle\Message
*/
public function setBccAddress($bccAddress)
{
$this->bccAddress = $bccAddress;
return $this;
}
/**
* the sender email address
*
* @param string $fromEmail
* @return \Hip\MandrillBundle\Message
*/
public function setFromEmail($fromEmail)
{
$this->fromEmail = $fromEmail;
return $this;
}
/**
* optional from name to be used
*
* @param string $fromName
* @return \Hip\MandrillBundle\Message
*/
public function setFromName($fromName)
{
$this->fromName = $fromName;
return $this;
}
/**
* the full HTML content to be sent
*
* @param string $html
* @return \Hip\MandrillBundle\Message
*/
public function setHtml($html)
{
$this->html = $html;
return $this;
}
/**
* optional full text content to be sent
*
* @param string $text
* @return \Hip\MandrillBundle\Message
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* the message subject
*
* @param string $subject
* @return \Hip\MandrillBundle\Message
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* whether or not to turn on click tracking for the message
*
* @param boolean $trackClicks
* @return \Hip\MandrillBundle\Message
*/
public function setTrackClicks($trackClicks)
{
$this->trackClicks = $trackClicks;
return $this;
}
/**
* whether or not to turn on open tracking for the message
*
* @param boolean $trackOpens
* @return \Hip\MandrillBundle\Message
*/
public function setTrackOpens($trackOpens)
{
$this->trackOpens = $trackOpens;
return $this;
}
/**
* whether or not to strip the query string from URLs when aggregating tracked URL data
*
* @param boolean $urlStripQs
* @return \Hip\MandrillBundle\Message
*/
public function setUrlStripQs($urlStripQs)
{
$this->urlStripQs = $urlStripQs;
return $this;
}
/**
* whether or not to expose all recipients in to "To" header for each email
*
* @param boolean $preserveRecipients
* @return \Hip\MandrillBundle\Message
*/
public function setPreserveRecipients($preserveRecipients)
{
$this->preserveRecipients = $preserveRecipients;
return $this;
}
/**
* whether to evaluate merge tags in the message.
*
* Will automatically be set to true if either merge_vars or global_merge_vars are provided.
*
* @param boolean $merge
* @return \Hip\MandrillBundle\Message
*/
public function setMerge($merge)
{
$this->merge = $merge;
return $this;
}
/**
* optional string indicating the value to set for the utm_campaign tracking parameter.
*
* If this isn't provided the email's from address will be used instead.
*
* @param string $googleAnalyticsCampaign
* @return \Hip\MandrillBundle\Message
*/
public function setGoogleAnalyticsCampaign($googleAnalyticsCampaign)
{
$this->googleAnalyticsCampaign = $googleAnalyticsCampaign;
return $this;
}
/**
* whether or not to automatically generate a text part for messages that are not given text
*
* @param boolean $autoText
* @return \Hip\MandrillBundle\Message
*/
public function setAutoText($autoText)
{
$this->autoText = $autoText;
return $this;
}
public function toArray()
{
$data = array();
$reflectedThis = new \ReflectionObject( $this );
foreach($reflectedThis->getProperties() as $reflectionProperty) {
$key = $reflectionProperty->name;
$key_underscore = preg_replace('/(?<=\\w)(?=[A-Z])/','_$1', $key);
$key_underscore = strtolower($key_underscore);
$data[$key_underscore] = $this->{$key};
}
return $data;
}
/**
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* @return boolean
*/
public function getAutoText()
{
return $this->autoText;
}
/**
* @return string
*/
public function getBccAddress()
{
return $this->bccAddress;
}
/**
* @return string
*/
public function getFromEmail()
{
return $this->fromEmail;
}
/**
* @return string
*/
public function getFromName()
{
return $this->fromName;
}
/**
* @return array
*/
public function getGlobalMergeVars()
{
return $this->globalMergeVars;
}
/**
* @return string
*/
public function getGoogleAnalyticsCampaign()
{
return $this->googleAnalyticsCampaign;
}
/**
* @return array
*/
public function getGoogleAnalyticsDomains()
{
return $this->googleAnalyticsDomains;
}
/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @return string
*/
public function getHtml()
{
return $this->html;
}
/**
* @return boolean
*/
public function getMerge()
{
return $this->merge;
}
/**
* @return array
*/
public function getMergeVars()
{
return $this->mergeVars;
}
/**
* @return array
*/
public function getMetadata()
{
return $this->metadata;
}
/**
* @return boolean
*/
public function getPreserveRecipients()
{
return $this->preserveRecipients;
}
/**
* @return array
*/
public function getRecipientMetadata()
{
return $this->recipientMetadata;
}
/**
* @return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* @return array
*/
public function getTags()
{
return $this->tags;
}
/**
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @return array
*/
public function getTo()
{
return $this->to;
}
/**
* @return boolean
*/
public function getTrackClicks()
{
return $this->trackClicks;
}
/**
* @return boolean
*/
public function getTrackOpens()
{
return $this->trackOpens;
}
/**
* @return boolean
*/
public function getUrlStripQs()
{
return $this->urlStripQs;
}
}