@@ -12,410 +12,409 @@ |
||
12 | 12 | class Archangel |
13 | 13 | { |
14 | 14 | |
15 | - /** @var string $subject */ |
|
16 | - protected $subject; |
|
17 | - |
|
18 | - /** @var array $to */ |
|
19 | - protected $to = array(); |
|
20 | - |
|
21 | - /** @var array $cc */ |
|
22 | - protected $cc = array(); |
|
23 | - |
|
24 | - /** @var array $bcc */ |
|
25 | - protected $bcc = array(); |
|
26 | - |
|
27 | - /** @var array $headers */ |
|
28 | - protected $headers = array(); |
|
29 | - |
|
30 | - /** @var string $plainMessage */ |
|
31 | - protected $plainMessage; |
|
32 | - |
|
33 | - /** @var string $htmlMessage */ |
|
34 | - protected $htmlMessage; |
|
35 | - |
|
36 | - /** @var array $attachments */ |
|
37 | - protected $attachments = array(); |
|
38 | - |
|
39 | - /** @var string $boundary */ |
|
40 | - protected $boundary; |
|
41 | - |
|
42 | - /** @var string $alternativeBoundary */ |
|
43 | - protected $alternativeBoundary; |
|
44 | - |
|
45 | - /** @var string LINE_BREAK */ |
|
46 | - const LINE_BREAK = "\r\n"; |
|
47 | - |
|
48 | - /** |
|
49 | - * @param string $mailer |
|
50 | - */ |
|
51 | - public function __construct($mailer = null) |
|
52 | - { |
|
53 | - if (is_null($mailer)) { |
|
54 | - $mailer = sprintf('PHP/%s', phpversion()); |
|
55 | - } |
|
56 | - $this->headers['X-Mailer'] = $mailer; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Setter method for adding recipients |
|
61 | - * |
|
62 | - * @param string $address email address for the recipient |
|
63 | - * @param string $title name of the recipient (optional) |
|
64 | - |
|
65 | - * @return object instantiated $this |
|
66 | - */ |
|
67 | - public function addTo($address, $title = '') |
|
68 | - { |
|
69 | - if (!empty($title)) { |
|
70 | - $address = sprintf('"%s" <%s>', $title, $address); |
|
71 | - } |
|
72 | - array_push($this->to, $address); |
|
73 | - |
|
74 | - return $this; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Setter method for adding cc recipients |
|
79 | - * |
|
80 | - * @param string $address email address for the cc recipient |
|
81 | - * @param string $title name of the cc recipient (optional) |
|
82 | - * |
|
83 | - * @return object instantiated $this |
|
84 | - */ |
|
85 | - public function addCC($address, $title = '') |
|
86 | - { |
|
87 | - if (!empty($title)) { |
|
88 | - $address = sprintf('"%s" <%s>', $title, $address); |
|
89 | - } |
|
90 | - array_push($this->cc, $address); |
|
91 | - |
|
92 | - return $this; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Setter method for adding bcc recipients |
|
97 | - * |
|
98 | - * @param string $address email address for the bcc recipient |
|
99 | - * @param string $title name of the bcc recipient (optional) |
|
100 | - * |
|
101 | - * @return object instantiated $this |
|
102 | - */ |
|
103 | - public function addBCC($address, $title = '') |
|
104 | - { |
|
105 | - if (!empty($title)) { |
|
106 | - $address = sprintf('"%s" <%s>', $title, $address); |
|
107 | - } |
|
108 | - array_push($this->bcc, $address); |
|
109 | - |
|
110 | - return $this; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Setter method for setting the single 'from' field |
|
115 | - * |
|
116 | - * @param string $address email address for the sender |
|
117 | - * @param string $title name of the sender (optional) |
|
118 | - * |
|
119 | - * @return object instantiated $this |
|
120 | - */ |
|
121 | - public function setFrom($address, $title = '') |
|
122 | - { |
|
123 | - if (!empty($title)) { |
|
124 | - $address = sprintf('"%s" <%s>', $title, $address); |
|
125 | - } |
|
126 | - $this->headers['From'] = $address; |
|
127 | - |
|
128 | - return $this; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Setter method for setting the single 'reply-to' field |
|
133 | - * |
|
134 | - * @param string $address email address for the reply-to |
|
135 | - * @param string $title name of the reply-to (optional) |
|
136 | - * |
|
137 | - * @return object instantiated $this |
|
138 | - */ |
|
139 | - public function setReplyTo($address, $title = '') |
|
140 | - { |
|
141 | - if (!empty($title)) { |
|
142 | - $address = sprintf('"%s" <%s>', $title, $address); |
|
143 | - } |
|
144 | - $this->headers['Reply-To'] = $address; |
|
145 | - |
|
146 | - return $this; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Setter method for setting a subject |
|
151 | - * |
|
152 | - * @param string $subject subject for the email |
|
153 | - * |
|
154 | - * @return object instantiated $this |
|
155 | - */ |
|
156 | - public function setSubject($subject) |
|
157 | - { |
|
158 | - $this->subject = $subject; |
|
159 | - |
|
160 | - return $this; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Setter method for the plain text message |
|
165 | - * |
|
166 | - * @param string $message the plain-text message |
|
167 | - * |
|
168 | - * @return object instantiated $this |
|
169 | - */ |
|
170 | - public function setPlainMessage($message) |
|
171 | - { |
|
172 | - $this->plainMessage = $message; |
|
173 | - |
|
174 | - return $this; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Setter method for the html message |
|
179 | - * |
|
180 | - * @param string $message the html message |
|
181 | - * |
|
182 | - * @return object instantiated $this |
|
183 | - */ |
|
184 | - public function setHTMLMessage($message) |
|
185 | - { |
|
186 | - $this->htmlMessage = $message; |
|
187 | - |
|
188 | - return $this; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Setter method for adding attachments |
|
193 | - * |
|
194 | - * @param string $path the full path of the attachment |
|
195 | - * @param string $type mime type of the file |
|
196 | - * @param string $title the title of the attachment (optional) |
|
197 | - * |
|
198 | - * @return object instantiated $this |
|
199 | - */ |
|
200 | - public function addAttachment($path, $type, $title = '') |
|
201 | - { |
|
202 | - array_push($this->attachments, array( |
|
203 | - 'path' => $path, |
|
204 | - 'type' => $type, |
|
205 | - 'title' => $title, |
|
206 | - )); |
|
207 | - |
|
208 | - return $this; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * The executing step, the actual sending of the email |
|
213 | - * First checks to make sure the minimum fields are set (returns false if they are not) |
|
214 | - * Second it attempts to send the mail with php's mail() (returns false if it fails) |
|
215 | - * |
|
216 | - * return boolean whether or not the email was valid & sent |
|
217 | - */ |
|
218 | - public function send() |
|
219 | - { |
|
220 | - if (!$this->checkRequiredFields()) { |
|
221 | - return false; |
|
222 | - } |
|
223 | - |
|
224 | - $to = $this->buildTo(); |
|
225 | - $subject = $this->subject; |
|
226 | - $message = $this->buildMessage(); |
|
227 | - $headers = $this->buildHeaders(); |
|
228 | - |
|
229 | - return mail($to, $subject, $message, $headers); |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Call to check the minimum required fields |
|
234 | - * |
|
235 | - * @return boolean whether or not the email meets the minimum required fields |
|
236 | - */ |
|
237 | - protected function checkRequiredFields() |
|
238 | - { |
|
239 | - if (empty($this->to)) { |
|
240 | - return false; |
|
241 | - } |
|
242 | - if (empty($this->subject)) { |
|
243 | - return false; |
|
244 | - } |
|
245 | - |
|
246 | - if ( |
|
247 | - empty($this->plainMessage) && |
|
248 | - empty($this->htmlMessage) && |
|
249 | - empty($this->attachments) |
|
250 | - ) { |
|
251 | - return false; |
|
252 | - } |
|
253 | - |
|
254 | - return true; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Build the recipients from 'to' |
|
259 | - * |
|
260 | - * @return string comma-separated lit of recipients |
|
261 | - */ |
|
262 | - protected function buildTo() |
|
263 | - { |
|
264 | - return implode(', ', $this->to); |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see |
|
269 | - * |
|
270 | - * @return string email message |
|
271 | - */ |
|
272 | - protected function buildMessage() |
|
273 | - { |
|
274 | - $message = ''; |
|
15 | + /** @var string $subject */ |
|
16 | + protected $subject; |
|
17 | + |
|
18 | + /** @var array $to */ |
|
19 | + protected $to = array(); |
|
20 | + |
|
21 | + /** @var array $cc */ |
|
22 | + protected $cc = array(); |
|
23 | + |
|
24 | + /** @var array $bcc */ |
|
25 | + protected $bcc = array(); |
|
26 | + |
|
27 | + /** @var array $headers */ |
|
28 | + protected $headers = array(); |
|
29 | + |
|
30 | + /** @var string $plainMessage */ |
|
31 | + protected $plainMessage; |
|
32 | + |
|
33 | + /** @var string $htmlMessage */ |
|
34 | + protected $htmlMessage; |
|
35 | + |
|
36 | + /** @var array $attachments */ |
|
37 | + protected $attachments = array(); |
|
38 | + |
|
39 | + /** @var string $boundary */ |
|
40 | + protected $boundary; |
|
41 | + |
|
42 | + /** @var string $alternativeBoundary */ |
|
43 | + protected $alternativeBoundary; |
|
44 | + |
|
45 | + /** @var string LINE_BREAK */ |
|
46 | + const LINE_BREAK = "\r\n"; |
|
47 | + |
|
48 | + /** |
|
49 | + * @param string $mailer |
|
50 | + */ |
|
51 | + public function __construct($mailer = null) |
|
52 | + { |
|
53 | + if (is_null($mailer)) { |
|
54 | + $mailer = sprintf('PHP/%s', phpversion()); |
|
55 | + } |
|
56 | + $this->headers['X-Mailer'] = $mailer; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Setter method for adding recipients |
|
61 | + * |
|
62 | + * @param string $address email address for the recipient |
|
63 | + * @param string $title name of the recipient (optional) |
|
64 | + * @return object instantiated $this |
|
65 | + */ |
|
66 | + public function addTo($address, $title = '') |
|
67 | + { |
|
68 | + if (!empty($title)) { |
|
69 | + $address = sprintf('"%s" <%s>', $title, $address); |
|
70 | + } |
|
71 | + array_push($this->to, $address); |
|
72 | + |
|
73 | + return $this; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Setter method for adding cc recipients |
|
78 | + * |
|
79 | + * @param string $address email address for the cc recipient |
|
80 | + * @param string $title name of the cc recipient (optional) |
|
81 | + * |
|
82 | + * @return object instantiated $this |
|
83 | + */ |
|
84 | + public function addCC($address, $title = '') |
|
85 | + { |
|
86 | + if (!empty($title)) { |
|
87 | + $address = sprintf('"%s" <%s>', $title, $address); |
|
88 | + } |
|
89 | + array_push($this->cc, $address); |
|
90 | + |
|
91 | + return $this; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Setter method for adding bcc recipients |
|
96 | + * |
|
97 | + * @param string $address email address for the bcc recipient |
|
98 | + * @param string $title name of the bcc recipient (optional) |
|
99 | + * |
|
100 | + * @return object instantiated $this |
|
101 | + */ |
|
102 | + public function addBCC($address, $title = '') |
|
103 | + { |
|
104 | + if (!empty($title)) { |
|
105 | + $address = sprintf('"%s" <%s>', $title, $address); |
|
106 | + } |
|
107 | + array_push($this->bcc, $address); |
|
108 | + |
|
109 | + return $this; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Setter method for setting the single 'from' field |
|
114 | + * |
|
115 | + * @param string $address email address for the sender |
|
116 | + * @param string $title name of the sender (optional) |
|
117 | + * |
|
118 | + * @return object instantiated $this |
|
119 | + */ |
|
120 | + public function setFrom($address, $title = '') |
|
121 | + { |
|
122 | + if (!empty($title)) { |
|
123 | + $address = sprintf('"%s" <%s>', $title, $address); |
|
124 | + } |
|
125 | + $this->headers['From'] = $address; |
|
126 | + |
|
127 | + return $this; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Setter method for setting the single 'reply-to' field |
|
132 | + * |
|
133 | + * @param string $address email address for the reply-to |
|
134 | + * @param string $title name of the reply-to (optional) |
|
135 | + * |
|
136 | + * @return object instantiated $this |
|
137 | + */ |
|
138 | + public function setReplyTo($address, $title = '') |
|
139 | + { |
|
140 | + if (!empty($title)) { |
|
141 | + $address = sprintf('"%s" <%s>', $title, $address); |
|
142 | + } |
|
143 | + $this->headers['Reply-To'] = $address; |
|
144 | + |
|
145 | + return $this; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Setter method for setting a subject |
|
150 | + * |
|
151 | + * @param string $subject subject for the email |
|
152 | + * |
|
153 | + * @return object instantiated $this |
|
154 | + */ |
|
155 | + public function setSubject($subject) |
|
156 | + { |
|
157 | + $this->subject = $subject; |
|
158 | + |
|
159 | + return $this; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Setter method for the plain text message |
|
164 | + * |
|
165 | + * @param string $message the plain-text message |
|
166 | + * |
|
167 | + * @return object instantiated $this |
|
168 | + */ |
|
169 | + public function setPlainMessage($message) |
|
170 | + { |
|
171 | + $this->plainMessage = $message; |
|
172 | + |
|
173 | + return $this; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Setter method for the html message |
|
178 | + * |
|
179 | + * @param string $message the html message |
|
180 | + * |
|
181 | + * @return object instantiated $this |
|
182 | + */ |
|
183 | + public function setHTMLMessage($message) |
|
184 | + { |
|
185 | + $this->htmlMessage = $message; |
|
186 | + |
|
187 | + return $this; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Setter method for adding attachments |
|
192 | + * |
|
193 | + * @param string $path the full path of the attachment |
|
194 | + * @param string $type mime type of the file |
|
195 | + * @param string $title the title of the attachment (optional) |
|
196 | + * |
|
197 | + * @return object instantiated $this |
|
198 | + */ |
|
199 | + public function addAttachment($path, $type, $title = '') |
|
200 | + { |
|
201 | + array_push($this->attachments, array( |
|
202 | + 'path' => $path, |
|
203 | + 'type' => $type, |
|
204 | + 'title' => $title, |
|
205 | + )); |
|
206 | + |
|
207 | + return $this; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * The executing step, the actual sending of the email |
|
212 | + * First checks to make sure the minimum fields are set (returns false if they are not) |
|
213 | + * Second it attempts to send the mail with php's mail() (returns false if it fails) |
|
214 | + * |
|
215 | + * return boolean whether or not the email was valid & sent |
|
216 | + */ |
|
217 | + public function send() |
|
218 | + { |
|
219 | + if (!$this->checkRequiredFields()) { |
|
220 | + return false; |
|
221 | + } |
|
222 | + |
|
223 | + $to = $this->buildTo(); |
|
224 | + $subject = $this->subject; |
|
225 | + $message = $this->buildMessage(); |
|
226 | + $headers = $this->buildHeaders(); |
|
227 | + |
|
228 | + return mail($to, $subject, $message, $headers); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Call to check the minimum required fields |
|
233 | + * |
|
234 | + * @return boolean whether or not the email meets the minimum required fields |
|
235 | + */ |
|
236 | + protected function checkRequiredFields() |
|
237 | + { |
|
238 | + if (empty($this->to)) { |
|
239 | + return false; |
|
240 | + } |
|
241 | + if (empty($this->subject)) { |
|
242 | + return false; |
|
243 | + } |
|
244 | + |
|
245 | + if ( |
|
246 | + empty($this->plainMessage) && |
|
247 | + empty($this->htmlMessage) && |
|
248 | + empty($this->attachments) |
|
249 | + ) { |
|
250 | + return false; |
|
251 | + } |
|
252 | + |
|
253 | + return true; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Build the recipients from 'to' |
|
258 | + * |
|
259 | + * @return string comma-separated lit of recipients |
|
260 | + */ |
|
261 | + protected function buildTo() |
|
262 | + { |
|
263 | + return implode(', ', $this->to); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see |
|
268 | + * |
|
269 | + * @return string email message |
|
270 | + */ |
|
271 | + protected function buildMessage() |
|
272 | + { |
|
273 | + $message = ''; |
|
275 | 274 | |
276 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
277 | - $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
|
275 | + if(isset($this->attachments) && count($this->attachments) > 0) |
|
276 | + $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
|
278 | 277 | |
279 | - if( |
|
280 | - isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
|
281 | - isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
282 | - { |
|
283 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
284 | - { |
|
285 | - $message .= "Content-Type: multipart/alternative; boundary={$this->getAlternativeBoundary()}" . self::LINE_BREAK; |
|
286 | - $message .= self::LINE_BREAK; |
|
287 | - } |
|
288 | - $message .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK; |
|
289 | - $message .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK; |
|
290 | - $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
291 | - $message .= self::LINE_BREAK; |
|
292 | - $message .= $this->plainMessage; |
|
293 | - $message .= self::LINE_BREAK; |
|
294 | - $message .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK; |
|
295 | - $message .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK; |
|
296 | - $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
297 | - $message .= self::LINE_BREAK; |
|
298 | - $message .= $this->htmlMessage; |
|
299 | - $message .= self::LINE_BREAK; |
|
300 | - $message .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK; |
|
301 | - $message .= self::LINE_BREAK; |
|
302 | - } |
|
303 | - else if(isset($this->plainMessage) && strlen($this->plainMessage)) |
|
304 | - { |
|
305 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
306 | - { |
|
307 | - $message .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK; |
|
308 | - $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
309 | - $message .= self::LINE_BREAK; |
|
310 | - } |
|
311 | - $message .= $this->plainMessage; |
|
312 | - $message .= self::LINE_BREAK; |
|
313 | - } |
|
314 | - else if(isset($this->htmlMessage) && strlen($this->htmlMessage)) |
|
315 | - { |
|
316 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
317 | - { |
|
318 | - $message .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK; |
|
319 | - $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
320 | - $message .= self::LINE_BREAK; |
|
321 | - } |
|
322 | - $message .= $this->htmlMessage; |
|
323 | - $message .= self::LINE_BREAK; |
|
324 | - } |
|
325 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
326 | - { |
|
327 | - foreach($this->attachments as $attachment) |
|
328 | - { |
|
329 | - $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
|
330 | - $message .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK; |
|
331 | - $message .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK; |
|
332 | - $message .= 'Content-Disposition: attachment' . self::LINE_BREAK; |
|
333 | - $message .= self::LINE_BREAK; |
|
334 | - $message .= $this->buildAttachmentContent($attachment); |
|
335 | - $message .= self::LINE_BREAK; |
|
336 | - } |
|
337 | - $message .= "--{$this->getBoundary()}--" . self::LINE_BREAK; |
|
338 | - } |
|
339 | - return $message; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Private holder for the boundry logic |
|
344 | - * Not called/created unless it's needed |
|
345 | - * |
|
346 | - * @return string boundary |
|
347 | - */ |
|
348 | - protected function getBoundary() |
|
349 | - { |
|
350 | - if (!isset($this->boundary)) { |
|
351 | - $this->boundary = sprintf('PHP-mixed-%s', uniqid()); |
|
352 | - } |
|
353 | - return $this->boundary; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Holder to create the alternative boundry logic |
|
358 | - * Not called/created unless it's needed |
|
359 | - * |
|
360 | - * @return string alternative boundary |
|
361 | - */ |
|
362 | - protected function getAlternativeBoundary() |
|
363 | - { |
|
364 | - if (!isset($this->alternativeBoundary)) { |
|
365 | - $this->alternativeBoundary = sprintf('PHP-alternative-%s', uniqid()); |
|
366 | - } |
|
367 | - return $this->alternativeBoundary; |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Fetcher for the additional headers needed for multipart emails |
|
372 | - * |
|
373 | - * @return string headers needed for multipart |
|
374 | - */ |
|
375 | - protected function buildHeaders() |
|
376 | - { |
|
377 | - $headers = ''; |
|
378 | - foreach($this->headers as $key => $value) |
|
379 | - { |
|
380 | - $headers .= "{$key}: {$value}" . self::LINE_BREAK; |
|
381 | - } |
|
278 | + if( |
|
279 | + isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
|
280 | + isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
281 | + { |
|
282 | + if(isset($this->attachments) && count($this->attachments) > 0) |
|
283 | + { |
|
284 | + $message .= "Content-Type: multipart/alternative; boundary={$this->getAlternativeBoundary()}" . self::LINE_BREAK; |
|
285 | + $message .= self::LINE_BREAK; |
|
286 | + } |
|
287 | + $message .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK; |
|
288 | + $message .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK; |
|
289 | + $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
290 | + $message .= self::LINE_BREAK; |
|
291 | + $message .= $this->plainMessage; |
|
292 | + $message .= self::LINE_BREAK; |
|
293 | + $message .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK; |
|
294 | + $message .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK; |
|
295 | + $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
296 | + $message .= self::LINE_BREAK; |
|
297 | + $message .= $this->htmlMessage; |
|
298 | + $message .= self::LINE_BREAK; |
|
299 | + $message .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK; |
|
300 | + $message .= self::LINE_BREAK; |
|
301 | + } |
|
302 | + else if(isset($this->plainMessage) && strlen($this->plainMessage)) |
|
303 | + { |
|
304 | + if(isset($this->attachments) && count($this->attachments) > 0) |
|
305 | + { |
|
306 | + $message .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK; |
|
307 | + $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
308 | + $message .= self::LINE_BREAK; |
|
309 | + } |
|
310 | + $message .= $this->plainMessage; |
|
311 | + $message .= self::LINE_BREAK; |
|
312 | + } |
|
313 | + else if(isset($this->htmlMessage) && strlen($this->htmlMessage)) |
|
314 | + { |
|
315 | + if(isset($this->attachments) && count($this->attachments) > 0) |
|
316 | + { |
|
317 | + $message .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK; |
|
318 | + $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
|
319 | + $message .= self::LINE_BREAK; |
|
320 | + } |
|
321 | + $message .= $this->htmlMessage; |
|
322 | + $message .= self::LINE_BREAK; |
|
323 | + } |
|
324 | + if(isset($this->attachments) && count($this->attachments) > 0) |
|
325 | + { |
|
326 | + foreach($this->attachments as $attachment) |
|
327 | + { |
|
328 | + $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
|
329 | + $message .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK; |
|
330 | + $message .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK; |
|
331 | + $message .= 'Content-Disposition: attachment' . self::LINE_BREAK; |
|
332 | + $message .= self::LINE_BREAK; |
|
333 | + $message .= $this->buildAttachmentContent($attachment); |
|
334 | + $message .= self::LINE_BREAK; |
|
335 | + } |
|
336 | + $message .= "--{$this->getBoundary()}--" . self::LINE_BREAK; |
|
337 | + } |
|
338 | + return $message; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Private holder for the boundry logic |
|
343 | + * Not called/created unless it's needed |
|
344 | + * |
|
345 | + * @return string boundary |
|
346 | + */ |
|
347 | + protected function getBoundary() |
|
348 | + { |
|
349 | + if (!isset($this->boundary)) { |
|
350 | + $this->boundary = sprintf('PHP-mixed-%s', uniqid()); |
|
351 | + } |
|
352 | + return $this->boundary; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Holder to create the alternative boundry logic |
|
357 | + * Not called/created unless it's needed |
|
358 | + * |
|
359 | + * @return string alternative boundary |
|
360 | + */ |
|
361 | + protected function getAlternativeBoundary() |
|
362 | + { |
|
363 | + if (!isset($this->alternativeBoundary)) { |
|
364 | + $this->alternativeBoundary = sprintf('PHP-alternative-%s', uniqid()); |
|
365 | + } |
|
366 | + return $this->alternativeBoundary; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Fetcher for the additional headers needed for multipart emails |
|
371 | + * |
|
372 | + * @return string headers needed for multipart |
|
373 | + */ |
|
374 | + protected function buildHeaders() |
|
375 | + { |
|
376 | + $headers = ''; |
|
377 | + foreach($this->headers as $key => $value) |
|
378 | + { |
|
379 | + $headers .= "{$key}: {$value}" . self::LINE_BREAK; |
|
380 | + } |
|
382 | 381 | |
383 | - if(count($this->cc) > 0) |
|
384 | - $headers .= 'CC: ' . implode(', ', $this->cc) . self::LINE_BREAK; |
|
385 | - if(count($this->bcc) > 0) |
|
386 | - $headers .= 'BCC: ' . implode(', ', $this->bcc) . self::LINE_BREAK; |
|
382 | + if(count($this->cc) > 0) |
|
383 | + $headers .= 'CC: ' . implode(', ', $this->cc) . self::LINE_BREAK; |
|
384 | + if(count($this->bcc) > 0) |
|
385 | + $headers .= 'BCC: ' . implode(', ', $this->bcc) . self::LINE_BREAK; |
|
387 | 386 | |
388 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
389 | - $headers .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\""; |
|
390 | - else if( |
|
391 | - isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
|
392 | - isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
393 | - { |
|
394 | - $headers .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\""; |
|
395 | - } |
|
396 | - else if(isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
397 | - $headers .= 'Content-type: text/html; charset="iso-8859-1"'; |
|
387 | + if(isset($this->attachments) && count($this->attachments) > 0) |
|
388 | + $headers .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\""; |
|
389 | + else if( |
|
390 | + isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
|
391 | + isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
392 | + { |
|
393 | + $headers .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\""; |
|
394 | + } |
|
395 | + else if(isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
396 | + $headers .= 'Content-type: text/html; charset="iso-8859-1"'; |
|
398 | 397 | |
399 | - return $headers; |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * File reader for attachments |
|
404 | - * |
|
405 | - * @return string binary representation of file, base64'd |
|
406 | - */ |
|
407 | - protected function buildAttachmentContent($attachment) |
|
408 | - { |
|
409 | - if (!file_exists($attachment['path'])) { |
|
410 | - return ''; // todo log error |
|
411 | - } |
|
412 | - |
|
413 | - $handle = fopen($attachment['path'], 'r'); |
|
414 | - $contents = fread($handle, filesize($attachment['path'])); |
|
415 | - fclose($handle); |
|
416 | - |
|
417 | - $contents = base64_encode($contents); |
|
418 | - $contents = chunk_split($contents); |
|
419 | - return $contents; |
|
420 | - } |
|
398 | + return $headers; |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * File reader for attachments |
|
403 | + * |
|
404 | + * @return string binary representation of file, base64'd |
|
405 | + */ |
|
406 | + protected function buildAttachmentContent($attachment) |
|
407 | + { |
|
408 | + if (!file_exists($attachment['path'])) { |
|
409 | + return ''; // todo log error |
|
410 | + } |
|
411 | + |
|
412 | + $handle = fopen($attachment['path'], 'r'); |
|
413 | + $contents = fread($handle, filesize($attachment['path'])); |
|
414 | + fclose($handle); |
|
415 | + |
|
416 | + $contents = base64_encode($contents); |
|
417 | + $contents = chunk_split($contents); |
|
418 | + return $contents; |
|
419 | + } |
|
421 | 420 | } |
@@ -273,14 +273,14 @@ discard block |
||
273 | 273 | { |
274 | 274 | $message = ''; |
275 | 275 | |
276 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
276 | + if (isset($this->attachments) && count($this->attachments) > 0) |
|
277 | 277 | $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
278 | 278 | |
279 | - if( |
|
279 | + if ( |
|
280 | 280 | isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
281 | 281 | isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
282 | 282 | { |
283 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
283 | + if (isset($this->attachments) && count($this->attachments) > 0) |
|
284 | 284 | { |
285 | 285 | $message .= "Content-Type: multipart/alternative; boundary={$this->getAlternativeBoundary()}" . self::LINE_BREAK; |
286 | 286 | $message .= self::LINE_BREAK; |
@@ -300,9 +300,9 @@ discard block |
||
300 | 300 | $message .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK; |
301 | 301 | $message .= self::LINE_BREAK; |
302 | 302 | } |
303 | - else if(isset($this->plainMessage) && strlen($this->plainMessage)) |
|
303 | + else if (isset($this->plainMessage) && strlen($this->plainMessage)) |
|
304 | 304 | { |
305 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
305 | + if (isset($this->attachments) && count($this->attachments) > 0) |
|
306 | 306 | { |
307 | 307 | $message .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK; |
308 | 308 | $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
@@ -311,9 +311,9 @@ discard block |
||
311 | 311 | $message .= $this->plainMessage; |
312 | 312 | $message .= self::LINE_BREAK; |
313 | 313 | } |
314 | - else if(isset($this->htmlMessage) && strlen($this->htmlMessage)) |
|
314 | + else if (isset($this->htmlMessage) && strlen($this->htmlMessage)) |
|
315 | 315 | { |
316 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
316 | + if (isset($this->attachments) && count($this->attachments) > 0) |
|
317 | 317 | { |
318 | 318 | $message .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK; |
319 | 319 | $message .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK; |
@@ -322,9 +322,9 @@ discard block |
||
322 | 322 | $message .= $this->htmlMessage; |
323 | 323 | $message .= self::LINE_BREAK; |
324 | 324 | } |
325 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
325 | + if (isset($this->attachments) && count($this->attachments) > 0) |
|
326 | 326 | { |
327 | - foreach($this->attachments as $attachment) |
|
327 | + foreach ($this->attachments as $attachment) |
|
328 | 328 | { |
329 | 329 | $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
330 | 330 | $message .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK; |
@@ -375,25 +375,25 @@ discard block |
||
375 | 375 | protected function buildHeaders() |
376 | 376 | { |
377 | 377 | $headers = ''; |
378 | - foreach($this->headers as $key => $value) |
|
378 | + foreach ($this->headers as $key => $value) |
|
379 | 379 | { |
380 | 380 | $headers .= "{$key}: {$value}" . self::LINE_BREAK; |
381 | 381 | } |
382 | 382 | |
383 | - if(count($this->cc) > 0) |
|
383 | + if (count($this->cc) > 0) |
|
384 | 384 | $headers .= 'CC: ' . implode(', ', $this->cc) . self::LINE_BREAK; |
385 | - if(count($this->bcc) > 0) |
|
385 | + if (count($this->bcc) > 0) |
|
386 | 386 | $headers .= 'BCC: ' . implode(', ', $this->bcc) . self::LINE_BREAK; |
387 | 387 | |
388 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
388 | + if (isset($this->attachments) && count($this->attachments) > 0) |
|
389 | 389 | $headers .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\""; |
390 | - else if( |
|
390 | + else if ( |
|
391 | 391 | isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
392 | 392 | isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
393 | 393 | { |
394 | 394 | $headers .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\""; |
395 | 395 | } |
396 | - else if(isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
396 | + else if (isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
397 | 397 | $headers .= 'Content-type: text/html; charset="iso-8859-1"'; |
398 | 398 | |
399 | 399 | return $headers; |
@@ -273,8 +273,9 @@ discard block |
||
273 | 273 | { |
274 | 274 | $message = ''; |
275 | 275 | |
276 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
277 | - $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
|
276 | + if(isset($this->attachments) && count($this->attachments) > 0) { |
|
277 | + $message .= "--{$this->getBoundary()}" . self::LINE_BREAK; |
|
278 | + } |
|
278 | 279 | |
279 | 280 | if( |
280 | 281 | isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
@@ -299,8 +300,7 @@ discard block |
||
299 | 300 | $message .= self::LINE_BREAK; |
300 | 301 | $message .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK; |
301 | 302 | $message .= self::LINE_BREAK; |
302 | - } |
|
303 | - else if(isset($this->plainMessage) && strlen($this->plainMessage)) |
|
303 | + } else if(isset($this->plainMessage) && strlen($this->plainMessage)) |
|
304 | 304 | { |
305 | 305 | if(isset($this->attachments) && count($this->attachments) > 0) |
306 | 306 | { |
@@ -310,8 +310,7 @@ discard block |
||
310 | 310 | } |
311 | 311 | $message .= $this->plainMessage; |
312 | 312 | $message .= self::LINE_BREAK; |
313 | - } |
|
314 | - else if(isset($this->htmlMessage) && strlen($this->htmlMessage)) |
|
313 | + } else if(isset($this->htmlMessage) && strlen($this->htmlMessage)) |
|
315 | 314 | { |
316 | 315 | if(isset($this->attachments) && count($this->attachments) > 0) |
317 | 316 | { |
@@ -380,21 +379,23 @@ discard block |
||
380 | 379 | $headers .= "{$key}: {$value}" . self::LINE_BREAK; |
381 | 380 | } |
382 | 381 | |
383 | - if(count($this->cc) > 0) |
|
384 | - $headers .= 'CC: ' . implode(', ', $this->cc) . self::LINE_BREAK; |
|
385 | - if(count($this->bcc) > 0) |
|
386 | - $headers .= 'BCC: ' . implode(', ', $this->bcc) . self::LINE_BREAK; |
|
382 | + if(count($this->cc) > 0) { |
|
383 | + $headers .= 'CC: ' . implode(', ', $this->cc) . self::LINE_BREAK; |
|
384 | + } |
|
385 | + if(count($this->bcc) > 0) { |
|
386 | + $headers .= 'BCC: ' . implode(', ', $this->bcc) . self::LINE_BREAK; |
|
387 | + } |
|
387 | 388 | |
388 | - if(isset($this->attachments) && count($this->attachments) > 0) |
|
389 | - $headers .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\""; |
|
390 | - else if( |
|
389 | + if(isset($this->attachments) && count($this->attachments) > 0) { |
|
390 | + $headers .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\""; |
|
391 | + } else if( |
|
391 | 392 | isset($this->plainMessage) && strlen($this->plainMessage) > 0 && |
392 | 393 | isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
393 | 394 | { |
394 | 395 | $headers .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\""; |
396 | + } else if(isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) { |
|
397 | + $headers .= 'Content-type: text/html; charset="iso-8859-1"'; |
|
395 | 398 | } |
396 | - else if(isset($this->htmlMessage) && strlen($this->htmlMessage) > 0) |
|
397 | - $headers .= 'Content-type: text/html; charset="iso-8859-1"'; |
|
398 | 399 | |
399 | 400 | return $headers; |
400 | 401 | } |