1 | <?php |
||
19 | class Message |
||
20 | { |
||
21 | /** |
||
22 | * Message charset. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $charset = Headers::DEFAULT_CHARSET; |
||
27 | |||
28 | /** |
||
29 | * In HTML format. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $in_html = false; |
||
34 | |||
35 | /** |
||
36 | * Headers. |
||
37 | * |
||
38 | * @var Headers |
||
39 | */ |
||
40 | protected $headers; |
||
41 | |||
42 | /** |
||
43 | * From. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $from = ''; |
||
48 | |||
49 | /** |
||
50 | * To. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $to = ''; |
||
55 | |||
56 | /** |
||
57 | * Subject. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $subject = ''; |
||
62 | |||
63 | /** |
||
64 | * Message text. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $text = ''; |
||
69 | |||
70 | /** |
||
71 | * Construct. |
||
72 | */ |
||
73 | 20 | public function __construct() |
|
74 | { |
||
75 | 20 | $this->headers = new Headers(); |
|
76 | 20 | $this->setCharset($this->charset)->setSubject(''); |
|
77 | 20 | $this->headers->set('MIME-Version', '1.0'); |
|
78 | 20 | } |
|
79 | |||
80 | /** |
||
81 | * Set message charset. |
||
82 | * |
||
83 | * @param string $charset |
||
84 | * |
||
85 | * @return self |
||
86 | */ |
||
87 | 20 | public function setCharset($charset) |
|
88 | { |
||
89 | 20 | $this->charset = $charset; |
|
90 | 20 | $this->headers->setCharset($charset); |
|
91 | |||
92 | 20 | return $this->setContentType(); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get message charset. |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 1 | public function getCharset() |
|
101 | { |
||
102 | 1 | return $this->charset; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Send from E-mail. |
||
107 | * |
||
108 | * @param string $from |
||
109 | * @param string $name |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | 3 | public function setFrom($from, $name = '') |
|
114 | { |
||
115 | 3 | $this->from = $from; |
|
116 | 3 | $this->headers->set('From', $this->headers->foramatName($from, $name)); |
|
117 | |||
118 | 3 | return $this; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Get from E-mail. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 1 | public function getFrom() |
|
130 | |||
131 | /** |
||
132 | * Set reply to E-mail. |
||
133 | * |
||
134 | * @param string $to |
||
135 | * @param string $name |
||
136 | * |
||
137 | * @return self |
||
138 | */ |
||
139 | 2 | public function setReplyTo($to, $name = '') |
|
140 | { |
||
141 | 2 | $this->headers->set('Reply-To', $this->headers->foramatName($to, $name)); |
|
142 | |||
143 | 2 | return $this; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Send to E-mail. |
||
148 | * |
||
149 | * @param string $to |
||
150 | * |
||
151 | * @return self |
||
152 | */ |
||
153 | 8 | public function setTo($to) |
|
154 | { |
||
155 | 8 | $this->to = $to; |
|
156 | 8 | $this->headers->set('To', $to); |
|
157 | |||
158 | 8 | return $this; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get to E-mail. |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | 1 | public function getTo() |
|
167 | { |
||
168 | 1 | return $this->to; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Set message subject. |
||
173 | * |
||
174 | * @param string $subject |
||
175 | * |
||
176 | * @return self |
||
177 | */ |
||
178 | 20 | public function setSubject($subject) |
|
179 | { |
||
180 | 20 | $this->subject = $subject; |
|
181 | 20 | $this->headers->set('Subject', $subject, true); |
|
182 | |||
183 | 20 | return $this; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Get message subject. |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | 1 | public function getSubject() |
|
192 | { |
||
193 | 1 | return $this->subject; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Set message text. |
||
198 | * |
||
199 | * @param string $text |
||
200 | * |
||
201 | * @return self |
||
202 | */ |
||
203 | 1 | public function setText($text) |
|
204 | { |
||
205 | 1 | $this->text = $text; |
|
206 | |||
207 | 1 | return $this; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get message text. |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | 1 | public function getText() |
|
216 | { |
||
217 | 1 | return $this->text; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Send E-mail in HTML format. |
||
222 | * |
||
223 | * @return self |
||
224 | */ |
||
225 | 1 | public function inHTML() |
|
226 | { |
||
227 | 1 | $this->in_html = true; |
|
228 | |||
229 | 1 | return $this->setContentType(); |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * @return string |
||
234 | */ |
||
235 | 17 | public function getHeaders() |
|
236 | { |
||
237 | 17 | return $this->headers->toString(); |
|
238 | } |
||
239 | |||
240 | 16 | public function __clone() |
|
244 | |||
245 | /** |
||
246 | * @return self |
||
247 | */ |
||
248 | 20 | protected function setContentType() |
|
249 | { |
||
250 | 20 | $this->headers->set( |
|
251 | 20 | 'Content-type', |
|
252 | 20 | 'text/'.($this->in_html ? 'html' : 'plain').'; charset="'.$this->charset.'"' |
|
253 | ); |
||
254 | |||
255 | 20 | return $this; |
|
256 | } |
||
257 | } |
||
258 |