1
|
|
|
<?php /** MicroMailMessage */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Mail; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Message class file. |
7
|
|
|
* |
8
|
|
|
* @author Oleg Lunegov <[email protected]> |
9
|
|
|
* @link https://github.com/linpax/microphp-framework |
10
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
11
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
12
|
|
|
* @package Micro |
13
|
|
|
* @subpackage Mail |
14
|
|
|
* @version 1.0 |
15
|
|
|
* @since 1.0 |
16
|
|
|
* @final |
17
|
|
|
*/ |
18
|
|
|
class Message implements IMessage |
19
|
|
|
{ |
20
|
|
|
/** @var string $to Recipient */ |
21
|
|
|
private $to; |
22
|
|
|
/** @var string $type Doctype */ |
23
|
|
|
private $type = 'text/html'; |
24
|
|
|
/** @var string $encoding encoding */ |
25
|
|
|
private $encoding = 'utf-8'; |
26
|
|
|
/** @var string $subject Subject */ |
27
|
|
|
private $subject; |
28
|
|
|
/** @var string $text Body */ |
29
|
|
|
private $text; |
30
|
|
|
/** @var array $headers Headers */ |
31
|
|
|
private $headers = []; |
32
|
|
|
/** @var array $params Parameters */ |
33
|
|
|
private $params = []; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Message constructor |
38
|
|
|
* |
39
|
|
|
* @access public |
40
|
|
|
* |
41
|
|
|
* @param string $from |
42
|
|
|
* @param string $fromName |
43
|
|
|
* |
44
|
|
|
* @result void |
45
|
|
|
*/ |
46
|
|
|
public function __construct($from = '', $fromName = '') |
47
|
|
|
{ |
48
|
|
|
if ($from) { |
49
|
|
|
$this->setHeaders('From', sprintf('=?utf-8?B?%s?= <%s>', base64_encode($fromName), $from)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function getTo() |
57
|
|
|
{ |
58
|
|
|
return $this->to; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function setTo($mail) |
65
|
|
|
{ |
66
|
|
|
$this->to = $mail; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
|
public function getSubject() |
73
|
|
|
{ |
74
|
|
|
return $this->subject; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public function setSubject($text) |
81
|
|
|
{ |
82
|
|
|
$this->subject = '=?utf-8?B?'.base64_encode($text).'?='; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
|
public function getText() |
89
|
|
|
{ |
90
|
|
|
return $this->text; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function setText($body, $type = 'text/plain', $encoding = 'utf-8') |
97
|
|
|
{ |
98
|
|
|
$this->text = $body; |
99
|
|
|
$this->type = $type; |
100
|
|
|
$this->encoding = $encoding; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
|
|
public function getHeaders() |
107
|
|
|
{ |
108
|
|
|
return sprintf("%s\r\nContent-type: %s; charset=%s\r\n", |
109
|
|
|
implode("\r\n", array_values($this->headers)), |
110
|
|
|
$this->type, |
111
|
|
|
$this->encoding |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function setHeaders($name, $value) |
119
|
|
|
{ |
120
|
|
|
$this->headers[$name] = $name.': '.$value; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public function getParams() |
127
|
|
|
{ |
128
|
|
|
if (!$this->params) { |
|
|
|
|
129
|
|
|
return false; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return implode("\r\n", array_values($this->params))."\r\n"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritdoc |
137
|
|
|
*/ |
138
|
|
|
public function setParams($name, $value) |
139
|
|
|
{ |
140
|
|
|
$this->params[$name] = $name.': '.$value; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.