Message::getTrackOpens()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Message.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\postmark
13
 */
14
15
namespace sweelix\postmark;
16
17
18
use yii\base\InvalidConfigException;
19
use yii\base\InvalidParamException;
20
use yii\base\NotSupportedException;
21
use yii\helpers\ArrayHelper;
22
use yii\mail\BaseMessage;
23
use Yii;
24
use yii\mail\MailerInterface;
25
26
/**
27
 * This component allow user to send an email
28
 *
29
 * @author Philippe Gaultier <[email protected]>
30
 * @copyright 2010-2017 Philippe Gaultier
31
 * @license http://www.sweelix.net/license license
32
 * @version XXX
33
 * @link http://www.sweelix.net
34
 * @package sweelix\postmark
35
 * @since XXX
36
 */
37
class Message extends BaseMessage
38
{
39
    /**
40
     * @var string|array from
41
     */
42
    protected $from;
43
44
    /**
45
     * @var array
46
     */
47
    protected $to = [];
48
49
    /**
50
     * @var string|array reply to
51
     */
52
    protected $replyTo;
53
54
    /**
55
     * @var array
56
     */
57
    protected $cc = [];
58
59
    /**
60
     * @var array
61
     */
62
    protected $bcc = [];
63
64
    /**
65
     * @var string
66
     */
67
    protected $subject;
68
69
    /**
70
     * @var string
71
     */
72
    protected $textBody;
73
74
    /**
75
     * @var string
76
     */
77
    protected $htmlBody;
78
79
    /**
80
     * @var array
81
     */
82
    protected $attachments = [];
83
84
    /**
85
     * @var string
86
     */
87
    protected $tag;
88
89
    /**
90
     * @var bool
91
     */
92
    protected $trackOpens = true;
93
94
    /**
95
     * @var array
96
     */
97
    protected $headers = [];
98
99
    /**
100
     * @var integer
101
     */
102
    protected $templateId;
103
104
    /**
105
     * @var array
106
     */
107
    protected $templateModel;
108
109
    /**
110
     * @var bool
111
     */
112
    protected $inlineCss = true;
113
114
    protected $charset = 'utf-8';
115
116
    /**
117
     * @inheritdoc
118
     */
119 1
    public function getCharset()
120
    {
121 1
        return $this->charset;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127 1
    public function setCharset($charset)
128
    {
129 1
        throw new NotSupportedException();
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135 1
    public function getFrom()
136
    {
137 1
        return self::stringifyEmails($this->from);
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143 3
    public function setFrom($from)
144
    {
145 3
        $this->from = $from;
146 3
        return $this;
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152 3
    public function getTo()
153
    {
154 3
        return $this->to;
155
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160 3
    public function setTo($to)
161
    {
162 3
        if (is_string($to) === true) {
163 3
            $to = [$to];
164 3
        }
165 3
        $this->to = $to;
166 3
        return $this;
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172 1
    public function getReplyTo()
173
    {
174 1
        return self::stringifyEmails($this->replyTo);
175
    }
176
177
    /**
178
     * @inheritdoc
179
     */
180 1
    public function setReplyTo($replyTo)
181
    {
182 1
        $this->replyTo = $replyTo;
183 1
        return $this;
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189 1
    public function getCc()
190
    {
191 1
        return $this->cc;
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197 1
    public function setCc($cc)
198
    {
199 1
        if (is_string($cc) === true) {
200 1
            $cc = [$cc];
201 1
        }
202 1
        $this->cc = $cc;
203 1
        return $this;
204
    }
205
206
    /**
207
     * @inheritdoc
208
     */
209 1
    public function getBcc()
210
    {
211 1
        return $this->bcc;
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217 1
    public function setBcc($bcc)
218
    {
219 1
        if (is_string($bcc) === true) {
220 1
            $bcc = [$bcc];
221 1
        }
222 1
        $this->bcc = $bcc;
223 1
        return $this;
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229 3
    public function getSubject()
230
    {
231 3
        return $this->subject;
232
    }
233
234
    /**
235
     * @inheritdoc
236
     */
237 2
    public function setSubject($subject)
238
    {
239 2
        $this->subject = $subject;
240 2
        return $this;
241
    }
242
243
    /**
244
     * @return string|null text body of the message
245
     * @since XXX
246
     */
247 1
    public function getTextBody()
248
    {
249 1
        return $this->textBody;
250
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255 2
    public function setTextBody($text)
256
    {
257 2
        $this->textBody = $text;
258 2
        return $this;
259
    }
260
261
    /**
262
     * @return string|null html body of the message
263
     * @since XXX
264
     */
265 1
    public function getHtmlBody()
266
    {
267 1
        return $this->htmlBody;
268
    }
269
270
    /**
271
     * @inheritdoc
272
     */
273 1
    public function setHtmlBody($html)
274
    {
275 1
        $this->htmlBody = $html;
276 1
        return $this;
277
    }
278
279
    /**
280
     * @return string tag associated to the email
281
     * @since XXX
282
     */
283 1
    public function getTag()
284
    {
285 1
        return $this->tag;
286
    }
287
288
    /**
289
     * @param string $tag tag which should be associated to the email
290
     * @return $this
291
     * @since XXX
292
     */
293 1
    public function setTag($tag)
294
    {
295 1
        $this->tag = $tag;
296 1
        return $this;
297
    }
298
299
    /**
300
     * @param bool $trackOpens define if mail should be tracked
301
     * @return $this
302
     * @since XXX
303
     */
304 1
    public function setTrackOpens($trackOpens)
305
    {
306 1
        $this->trackOpens = $trackOpens;
307 1
        return $this;
308
    }
309
310
    /**
311
     * @return bool tracking status
312
     * @since XXX
313
     */
314 1
    public function getTrackOpens()
315
    {
316 1
        return $this->trackOpens;
317
    }
318
319
    /**
320
     * @param integer $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded
321
     * @return $this
322
     * @since XXX
323
     */
324 2
    public function setTemplateId($templateId)
325
    {
326 2
        $this->templateId = $templateId;
327 2
        return $this;
328
    }
329
330
    /**
331
     * @return integer|null current templateId
332
     * @since XXX
333
     */
334 1
    public function getTemplateId()
335
    {
336 1
        return $this->templateId;
337
    }
338
339
    /**
340
     * @param array $templateModel model associated with the template
341
     * @return $this
342
     * @since XXX
343
     */
344 2
    public function setTemplateModel($templateModel)
345
    {
346 2
        $this->templateModel = $templateModel;
347 2
        return $this;
348
    }
349
350
    /**
351
     * @return array current template model
352
     * @since XXX
353
     */
354 1
    public function getTemplateModel()
355
    {
356 1
        return $this->templateModel;
357
    }
358
359
    /**
360
     * @param bool $inlineCss define if css should be inlined
361
     * @return $this
362
     * @since XXX
363
     */
364 1
    public function setInlineCss($inlineCss)
365
    {
366 1
        $this->inlineCss = $inlineCss;
367 1
        return $this;
368
    }
369
370
    /**
371
     * @return bool define if css should be inlined
372
     * @since XXX
373
     */
374 1
    public function getInlineCss()
375
    {
376 1
        return $this->inlineCss;
377
    }
378
379
    /**
380
     * @param array $header add custom header to the mail
381
     * @since XXX
382
     */
383 1
    public function addHeader($header)
384
    {
385 1
        $this->headers[] = $header;
386 1
    }
387
388
    /**
389
     * @return array|null headers which should be added to the mail
390
     * @since XXX
391
     */
392 1
    public function getHeaders()
393
    {
394 1
        return empty($this->headers) ? null : $this->headers;
395
    }
396
397
    /**
398
     * @return array|null list of attachments
399
     * @since XXX
400
     */
401 1
    public function getAttachments()
402
    {
403 1
        return empty($this->attachments) ? null : $this->attachments;
404
    }
405
406
    /**
407
     * @inheritdoc
408
     */
409 1
    public function attach($fileName, array $options = [])
410
    {
411
        $attachment = [
412 1
            'Content' => base64_encode(file_get_contents($fileName))
413 1
        ];
414 1
        if (!empty($options['fileName'])) {
415 1
            $attachment['Name'] = $options['fileName'];
416 1
        } else {
417 1
            $attachment['Name'] = pathinfo($fileName, PATHINFO_BASENAME);
418
        }
419 1
        if (!empty($options['contentType'])) {
420 1
            $attachment['ContentType'] = $options['contentType'];
421 1
        } else {
422 1
            $attachment['ContentType'] = 'application/octet-stream';
423
        }
424 1
        $this->attachments[] = $attachment;
425 1
        return $this;
426
    }
427
428
    /**
429
     * @inheritdoc
430
     */
431 2
    public function attachContent($content, array $options = [])
432
    {
433
        $attachment = [
434 2
            'Content' => base64_encode($content)
435 2
        ];
436 2
        if (!empty($options['fileName'])) {
437 1
            $attachment['Name'] = $options['fileName'];
438 1
        } else {
439 1
            throw new InvalidParamException('Filename is missing');
440
        }
441 1
        if (!empty($options['contentType'])) {
442 1
            $attachment['ContentType'] = $options['contentType'];
443 1
        } else {
444 1
            $attachment['ContentType'] = 'application/octet-stream';
445
        }
446 1
        $this->attachments[] = $attachment;
447 1
        return $this;
448
    }
449
450
    /**
451
     * @inheritdoc
452
     */
453 1
    public function embed($fileName, array $options = [])
454
    {
455
        $embed = [
456 1
            'Content' => base64_encode(file_get_contents($fileName))
457 1
        ];
458 1
        if (!empty($options['fileName'])) {
459 1
            $embed['Name'] = $options['fileName'];
460 1
        } else {
461 1
            $embed['Name'] = pathinfo($fileName, PATHINFO_BASENAME);
462
        }
463 1
        if (!empty($options['contentType'])) {
464 1
            $embed['ContentType'] = $options['contentType'];
465 1
        } else {
466 1
            $embed['ContentType'] = 'application/octet-stream';
467
        }
468 1
        $embed['ContentID'] = 'cid:' . uniqid();
469 1
        $this->attachments[] = $embed;
470 1
        return $embed['ContentID'];
471
    }
472
473
    /**
474
     * @inheritdoc
475
     */
476 2
    public function embedContent($content, array $options = [])
477
    {
478
        $embed = [
479 2
            'Content' => base64_encode($content)
480 2
        ];
481 2
        if (!empty($options['fileName'])) {
482 1
            $embed['Name'] = $options['fileName'];
483 1
        } else {
484 1
            throw new InvalidParamException('Filename is missing');
485
        }
486 1
        if (!empty($options['contentType'])) {
487 1
            $embed['ContentType'] = $options['contentType'];
488 1
        } else {
489 1
            $embed['ContentType'] = 'application/octet-stream';
490
        }
491 1
        $embed['ContentID'] = 'cid:' . uniqid();
492 1
        $this->attachments[] = $embed;
493 1
        return $embed['ContentID'];
494
    }
495
496
    /**
497
     * @inheritdoc
498
     * @todo make real serialization to make message compliant with PostmarkAPI
499
     */
500
    public function toString()
501
    {
502
        return serialize($this);
503
    }
504
505
506
    /**
507
     * @param array|string $emailsData email can be defined as string. In this case no transformation is done
508
     *                                 or as an array ['[email protected]', '[email protected]' => 'Email 2']
509
     * @return string|null
510
     * @since XXX
511
     */
512 1
    public static function stringifyEmails($emailsData)
513
    {
514 1
        $emails = null;
515 1
        if (empty($emailsData) === false) {
516 1
            if (is_array($emailsData) === true) {
517 1
                foreach ($emailsData as $key => $email) {
518 1
                    if (is_int($key) === true) {
519 1
                        $emails[] = $email;
520 1
                    } else {
521 1
                        if (preg_match('/[.,:]/', $email) > 0) {
522 1
                            $email = '"'. $email .'"';
523 1
                        }
524 1
                        $emails[] = $email . ' ' . '<' . $key . '>';
525
                    }
526 1
                }
527 1
                $emails = implode(', ', $emails);
528 1
            } elseif (is_string($emailsData) === true) {
529 1
                $emails = $emailsData;
530 1
            }
531 1
        }
532 1
        return $emails;
533
    }
534
535
536
}