| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: email.php 8004 2009-01-16 20:15:21Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Short description for file.
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs.controller.components
|
|
|
21 |
* @since CakePHP(tm) v 1.2.0.3467
|
|
|
22 |
* @version $Revision: 8004 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2009-01-16 12:15:21 -0800 (Fri, 16 Jan 2009) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
/**
|
|
|
28 |
* EmailComponent
|
|
|
29 |
*
|
|
|
30 |
* This component is used for handling Internet Message Format based
|
|
|
31 |
* based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
|
|
|
32 |
*
|
|
|
33 |
* @package cake
|
|
|
34 |
* @subpackage cake.cake.libs.controller.components
|
|
|
35 |
*
|
|
|
36 |
*/
|
|
|
37 |
App::import('Core', 'Multibyte');
|
|
|
38 |
class EmailComponent extends Object{
|
|
|
39 |
/**
|
|
|
40 |
* Recipient of the email
|
|
|
41 |
*
|
|
|
42 |
* @var string
|
|
|
43 |
* @access public
|
|
|
44 |
*/
|
|
|
45 |
var $to = null;
|
|
|
46 |
/**
|
|
|
47 |
* The mail which the email is sent from
|
|
|
48 |
*
|
|
|
49 |
* @var string
|
|
|
50 |
* @access public
|
|
|
51 |
*/
|
|
|
52 |
var $from = null;
|
|
|
53 |
/**
|
|
|
54 |
* The email the recipient will reply to
|
|
|
55 |
*
|
|
|
56 |
* @var string
|
|
|
57 |
* @access public
|
|
|
58 |
*/
|
|
|
59 |
var $replyTo = null;
|
|
|
60 |
/**
|
|
|
61 |
* The read receipt email
|
|
|
62 |
*
|
|
|
63 |
* @var string
|
|
|
64 |
* @access public
|
|
|
65 |
*/
|
|
|
66 |
var $readReceipt = null;
|
|
|
67 |
/**
|
|
|
68 |
* The mail that will be used in case of any errors like
|
|
|
69 |
* - Remote mailserver down
|
|
|
70 |
* - Remote user has exceeded his quota
|
|
|
71 |
* - Unknown user
|
|
|
72 |
*
|
|
|
73 |
* @var string
|
|
|
74 |
* @access public
|
|
|
75 |
*/
|
|
|
76 |
var $return = null;
|
|
|
77 |
/**
|
|
|
78 |
* Carbon Copy
|
|
|
79 |
*
|
|
|
80 |
* List of email's that should receive a copy of the email.
|
|
|
81 |
* The Recipient WILL be able to see this list
|
|
|
82 |
*
|
|
|
83 |
* @var array
|
|
|
84 |
* @access public
|
|
|
85 |
*/
|
|
|
86 |
var $cc = array();
|
|
|
87 |
/**
|
|
|
88 |
* Blind Carbon Copy
|
|
|
89 |
*
|
|
|
90 |
* List of email's that should receive a copy of the email.
|
|
|
91 |
* The Recipient WILL NOT be able to see this list
|
|
|
92 |
*
|
|
|
93 |
* @var array
|
|
|
94 |
* @access public
|
|
|
95 |
*/
|
|
|
96 |
var $bcc = array();
|
|
|
97 |
/**
|
|
|
98 |
* The subject of the email
|
|
|
99 |
*
|
|
|
100 |
* @var string
|
|
|
101 |
* @access public
|
|
|
102 |
*/
|
|
|
103 |
var $subject = null;
|
|
|
104 |
/**
|
|
|
105 |
* Associative array of a user defined headers
|
|
|
106 |
* Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
|
|
|
107 |
*
|
|
|
108 |
* @var array
|
|
|
109 |
* @access public
|
|
|
110 |
*/
|
|
|
111 |
var $headers = array();
|
|
|
112 |
/**
|
|
|
113 |
* List of additional headers
|
|
|
114 |
*
|
|
|
115 |
* These will NOT be used if you are using safemode and mail()
|
|
|
116 |
*
|
|
|
117 |
* @var string
|
|
|
118 |
* @access public
|
|
|
119 |
*/
|
|
|
120 |
var $additionalParams = null;
|
|
|
121 |
/**
|
|
|
122 |
* Layout for the View
|
|
|
123 |
*
|
|
|
124 |
* @var string
|
|
|
125 |
* @access public
|
|
|
126 |
*/
|
|
|
127 |
var $layout = 'default';
|
|
|
128 |
/**
|
|
|
129 |
* Template for the view
|
|
|
130 |
*
|
|
|
131 |
* @var string
|
|
|
132 |
* @access public
|
|
|
133 |
*/
|
|
|
134 |
var $template = null;
|
|
|
135 |
/**
|
|
|
136 |
* as per RFC2822 Section 2.1.1
|
|
|
137 |
*
|
|
|
138 |
* @var integer
|
|
|
139 |
* @access public
|
|
|
140 |
*/
|
|
|
141 |
var $lineLength = 70;
|
|
|
142 |
/**
|
|
|
143 |
* @deprecated see lineLength
|
|
|
144 |
*/
|
|
|
145 |
var $_lineLength = null;
|
|
|
146 |
/**
|
|
|
147 |
* What format should the email be sent in
|
|
|
148 |
*
|
|
|
149 |
* Supported formats:
|
|
|
150 |
* - text
|
|
|
151 |
* - html
|
|
|
152 |
* - both
|
|
|
153 |
*
|
|
|
154 |
* @var string
|
|
|
155 |
* @access public
|
|
|
156 |
*/
|
|
|
157 |
var $sendAs = 'text';
|
|
|
158 |
/**
|
|
|
159 |
* What method should the email be sent by
|
|
|
160 |
*
|
|
|
161 |
* Supported methods:
|
|
|
162 |
* - mail
|
|
|
163 |
* - smtp
|
|
|
164 |
* - debug
|
|
|
165 |
*
|
|
|
166 |
* @var string
|
|
|
167 |
* @access public
|
|
|
168 |
*/
|
|
|
169 |
var $delivery = 'mail';
|
|
|
170 |
/**
|
|
|
171 |
* charset the email is sent in
|
|
|
172 |
*
|
|
|
173 |
* @var string
|
|
|
174 |
* @access public
|
|
|
175 |
*/
|
|
|
176 |
var $charset = 'utf-8';
|
|
|
177 |
/**
|
|
|
178 |
* List of files that should be attached to the email.
|
|
|
179 |
*
|
|
|
180 |
* Can be both absolute and relative paths
|
|
|
181 |
*
|
|
|
182 |
* @var array
|
|
|
183 |
* @access public
|
|
|
184 |
*/
|
|
|
185 |
var $attachments = array();
|
|
|
186 |
/**
|
|
|
187 |
* What mailer should EmailComponent identify itself as
|
|
|
188 |
*
|
|
|
189 |
* @var string
|
|
|
190 |
* @access public
|
|
|
191 |
*/
|
|
|
192 |
var $xMailer = 'CakePHP Email Component';
|
|
|
193 |
/**
|
|
|
194 |
* The list of paths to search if an attachment isnt absolute
|
|
|
195 |
*
|
|
|
196 |
* @var array
|
|
|
197 |
* @access public
|
|
|
198 |
*/
|
|
|
199 |
var $filePaths = array();
|
|
|
200 |
/**
|
|
|
201 |
* List of options to use for smtp mail method
|
|
|
202 |
*
|
|
|
203 |
* Options is:
|
|
|
204 |
* - port
|
|
|
205 |
* - host
|
|
|
206 |
* - timeout
|
|
|
207 |
* - username
|
|
|
208 |
* - password
|
|
|
209 |
*
|
|
|
210 |
* @var array
|
|
|
211 |
* @access public
|
|
|
212 |
*/
|
|
|
213 |
var $smtpOptions = array(
|
|
|
214 |
'port'=> 25, 'host' => 'localhost', 'timeout' => 30
|
|
|
215 |
);
|
|
|
216 |
/**
|
|
|
217 |
* Placeholder for any errors that might happen with the
|
|
|
218 |
* smtp mail methods
|
|
|
219 |
*
|
|
|
220 |
* @var string
|
|
|
221 |
* @access public
|
|
|
222 |
*/
|
|
|
223 |
var $smtpError = null;
|
|
|
224 |
/**
|
|
|
225 |
* If set to true, the mail method will be auto-set to 'debug'
|
|
|
226 |
*
|
|
|
227 |
* @var string
|
|
|
228 |
* @access protected
|
|
|
229 |
*/
|
|
|
230 |
var $_debug = false;
|
|
|
231 |
/**
|
|
|
232 |
* Temporary store of message header lines
|
|
|
233 |
*
|
|
|
234 |
* @var array
|
|
|
235 |
* @access private
|
|
|
236 |
*/
|
|
|
237 |
var $__header = array();
|
|
|
238 |
/**
|
|
|
239 |
* If set, boundary to use for multipart mime messages
|
|
|
240 |
*
|
|
|
241 |
* @var string
|
|
|
242 |
* @access private
|
|
|
243 |
*/
|
|
|
244 |
var $__boundary = null;
|
|
|
245 |
/**
|
|
|
246 |
* Temporary store of message lines
|
|
|
247 |
*
|
|
|
248 |
* @var array
|
|
|
249 |
* @access private
|
|
|
250 |
*/
|
|
|
251 |
var $__message = array();
|
|
|
252 |
/**
|
|
|
253 |
* Variable that holds SMTP connection
|
|
|
254 |
*
|
|
|
255 |
* @var resource
|
|
|
256 |
* @access private
|
|
|
257 |
*/
|
|
|
258 |
var $__smtpConnection = null;
|
|
|
259 |
/**
|
|
|
260 |
* Initialize component
|
|
|
261 |
*
|
|
|
262 |
* @param object $controller Instantiating controller
|
|
|
263 |
* @access public
|
|
|
264 |
*/
|
|
|
265 |
function initialize(&$controller, $settings = array()) {
|
|
|
266 |
$this->Controller =& $controller;
|
|
|
267 |
if (Configure::read('App.encoding') !== null) {
|
|
|
268 |
$this->charset = Configure::read('App.encoding');
|
|
|
269 |
}
|
|
|
270 |
$this->_set($settings);
|
|
|
271 |
}
|
|
|
272 |
/**
|
|
|
273 |
* Startup component
|
|
|
274 |
*
|
|
|
275 |
* @param object $controller Instantiating controller
|
|
|
276 |
* @access public
|
|
|
277 |
*/
|
|
|
278 |
function startup(&$controller) {}
|
|
|
279 |
/**
|
|
|
280 |
* Send an email using the specified content, template and layout
|
|
|
281 |
*
|
|
|
282 |
* @param mixed $content Either an array of text lines, or a string with contents
|
|
|
283 |
* @param string $template Template to use when sending email
|
|
|
284 |
* @param string $layout Layout to use to enclose email body
|
|
|
285 |
* @return boolean Success
|
|
|
286 |
* @access public
|
|
|
287 |
*/
|
|
|
288 |
function send($content = null, $template = null, $layout = null) {
|
|
|
289 |
$this->__createHeader();
|
|
|
290 |
|
|
|
291 |
if ($template) {
|
|
|
292 |
$this->template = $template;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
if ($layout) {
|
|
|
296 |
$this->layout = $layout;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
if (is_array($content)) {
|
|
|
300 |
$content = implode("\n", $content) . "\n";
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
$message = $this->__wrap($content);
|
|
|
304 |
if ($this->template === null) {
|
|
|
305 |
$message = $this->__formatMessage($message);
|
|
|
306 |
} else {
|
|
|
307 |
$message = $this->__renderTemplate($message);
|
|
|
308 |
}
|
|
|
309 |
$message[] = '';
|
|
|
310 |
$this->__message = $message;
|
|
|
311 |
|
|
|
312 |
if (!empty($this->attachments)) {
|
|
|
313 |
$this->__attachFiles();
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
if (!is_null($this->__boundary)) {
|
|
|
317 |
$this->__message[] = '';
|
|
|
318 |
$this->__message[] = '--' . $this->__boundary . '--';
|
|
|
319 |
$this->__message[] = '';
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
if ($this->_debug) {
|
|
|
323 |
return $this->__debug();
|
|
|
324 |
}
|
|
|
325 |
$__method = '__' . $this->delivery;
|
|
|
326 |
$sent = $this->$__method();
|
|
|
327 |
|
|
|
328 |
$this->__header = array();
|
|
|
329 |
$this->__message = array();
|
|
|
330 |
|
|
|
331 |
return $sent;
|
|
|
332 |
}
|
|
|
333 |
/**
|
|
|
334 |
* Reset all EmailComponent internal variables to be able to send out a new email.
|
|
|
335 |
*
|
|
|
336 |
* @access public
|
|
|
337 |
*/
|
|
|
338 |
function reset() {
|
|
|
339 |
$this->template = null;
|
|
|
340 |
$this->to = null;
|
|
|
341 |
$this->from = null;
|
|
|
342 |
$this->replyTo = null;
|
|
|
343 |
$this->return = null;
|
|
|
344 |
$this->cc = array();
|
|
|
345 |
$this->bcc = array();
|
|
|
346 |
$this->subject = null;
|
|
|
347 |
$this->additionalParams = null;
|
|
|
348 |
$this->__header = array();
|
|
|
349 |
$this->__boundary = null;
|
|
|
350 |
$this->__message = array();
|
|
|
351 |
}
|
|
|
352 |
/**
|
|
|
353 |
* Render the contents using the current layout and template.
|
|
|
354 |
*
|
|
|
355 |
* @param string $content Content to render
|
|
|
356 |
* @return array Email ready to be sent
|
|
|
357 |
* @access private
|
|
|
358 |
*/
|
|
|
359 |
function __renderTemplate($content) {
|
|
|
360 |
$viewClass = $this->Controller->view;
|
|
|
361 |
|
|
|
362 |
if ($viewClass != 'View') {
|
|
|
363 |
if (strpos($viewClass, '.') !== false) {
|
|
|
364 |
list($plugin, $viewClass) = explode('.', $viewClass);
|
|
|
365 |
}
|
|
|
366 |
$viewClass = $viewClass . 'View';
|
|
|
367 |
App::import('View', $this->Controller->view);
|
|
|
368 |
}
|
|
|
369 |
$View = new $viewClass($this->Controller, false);
|
|
|
370 |
$View->layout = $this->layout;
|
|
|
371 |
$msg = array();
|
|
|
372 |
|
|
|
373 |
$content = implode("\n", $content);
|
|
|
374 |
|
|
|
375 |
if ($this->sendAs === 'both') {
|
|
|
376 |
$htmlContent = $content;
|
|
|
377 |
if (!empty($this->attachments)) {
|
|
|
378 |
$msg[] = '--' . $this->__boundary;
|
|
|
379 |
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
|
|
|
380 |
$msg[] = '';
|
|
|
381 |
}
|
|
|
382 |
$msg[] = '--alt-' . $this->__boundary;
|
|
|
383 |
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
384 |
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
385 |
$msg[] = '';
|
|
|
386 |
|
|
|
387 |
$content = $View->element('email' . DS . 'text' . DS . $this->template, array('content' => $content), true);
|
|
|
388 |
$View->layoutPath = 'email' . DS . 'text';
|
|
|
389 |
$content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
|
|
|
390 |
$msg = array_merge($msg, $content);
|
|
|
391 |
|
|
|
392 |
$msg[] = '';
|
|
|
393 |
$msg[] = '--alt-' . $this->__boundary;
|
|
|
394 |
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
395 |
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
396 |
$msg[] = '';
|
|
|
397 |
|
|
|
398 |
$htmlContent = $View->element('email' . DS . 'html' . DS . $this->template, array('content' => $htmlContent), true);
|
|
|
399 |
$View->layoutPath = 'email' . DS . 'html';
|
|
|
400 |
$htmlContent = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
|
|
|
401 |
$msg = array_merge($msg, $htmlContent);
|
|
|
402 |
$msg[] = '';
|
|
|
403 |
$msg[] = '--alt-' . $this->__boundary . '--';
|
|
|
404 |
$msg[] = '';
|
|
|
405 |
|
|
|
406 |
return $msg;
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
if (!empty($this->attachments)) {
|
|
|
410 |
if ($this->sendAs === 'html') {
|
|
|
411 |
$msg[] = '';
|
|
|
412 |
$msg[] = '--' . $this->__boundary;
|
|
|
413 |
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
414 |
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
415 |
$msg[] = '';
|
|
|
416 |
} else {
|
|
|
417 |
$msg[] = '--' . $this->__boundary;
|
|
|
418 |
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
419 |
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
420 |
$msg[] = '';
|
|
|
421 |
}
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
$content = $View->element('email' . DS . $this->sendAs . DS . $this->template, array('content' => $content), true);
|
|
|
425 |
$View->layoutPath = 'email' . DS . $this->sendAs;
|
|
|
426 |
$content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
|
|
|
427 |
$msg = array_merge($msg, $content);
|
|
|
428 |
|
|
|
429 |
return $msg;
|
|
|
430 |
}
|
|
|
431 |
/**
|
|
|
432 |
* Create unique boundary identifier
|
|
|
433 |
*
|
|
|
434 |
* @access private
|
|
|
435 |
*/
|
|
|
436 |
function __createBoundary() {
|
|
|
437 |
$this->__boundary = md5(uniqid(time()));
|
|
|
438 |
}
|
|
|
439 |
/**
|
|
|
440 |
* Create emails headers including (but not limited to) from email address, reply to,
|
|
|
441 |
* bcc and cc.
|
|
|
442 |
*
|
|
|
443 |
* @access private
|
|
|
444 |
*/
|
|
|
445 |
function __createHeader() {
|
|
|
446 |
if ($this->delivery == 'smtp') {
|
|
|
447 |
$this->__header[] = 'To: ' . $this->__formatAddress($this->to);
|
|
|
448 |
}
|
|
|
449 |
$this->__header[] = 'From: ' . $this->__formatAddress($this->from);
|
|
|
450 |
|
|
|
451 |
if (!empty($this->replyTo)) {
|
|
|
452 |
$this->__header[] = 'Reply-To: ' . $this->__formatAddress($this->replyTo);
|
|
|
453 |
}
|
|
|
454 |
if (!empty($this->return)) {
|
|
|
455 |
$this->__header[] = 'Return-Path: ' . $this->__formatAddress($this->return);
|
|
|
456 |
}
|
|
|
457 |
if (!empty($this->readReceipt)) {
|
|
|
458 |
$this->__header[] = 'Disposition-Notification-To: ' . $this->__formatAddress($this->readReceipt);
|
|
|
459 |
}
|
|
|
460 |
|
|
|
461 |
if (!empty($this->cc)) {
|
|
|
462 |
$this->__header[] = 'cc: ' .implode(', ', array_map(array($this, '__formatAddress'), $this->cc));
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
if (!empty($this->bcc) && $this->delivery != 'smtp') {
|
|
|
466 |
$this->__header[] = 'Bcc: ' .implode(', ', array_map(array($this, '__formatAddress'), $this->bcc));
|
|
|
467 |
}
|
|
|
468 |
if ($this->delivery == 'smtp') {
|
|
|
469 |
$this->__header[] = 'Subject: ' . $this->__encode($this->subject);
|
|
|
470 |
}
|
|
|
471 |
$this->__header[] = 'X-Mailer: ' . $this->xMailer;
|
|
|
472 |
|
|
|
473 |
if (!empty($this->headers)) {
|
|
|
474 |
foreach ($this->headers as $key => $val) {
|
|
|
475 |
$this->__header[] = 'X-' . $key . ': ' . $val;
|
|
|
476 |
}
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
if (!empty($this->attachments)) {
|
|
|
480 |
$this->__createBoundary();
|
|
|
481 |
$this->__header[] = 'MIME-Version: 1.0';
|
|
|
482 |
$this->__header[] = 'Content-Type: multipart/mixed; boundary="' . $this->__boundary . '"';
|
|
|
483 |
$this->__header[] = 'This part of the E-mail should never be seen. If';
|
|
|
484 |
$this->__header[] = 'you are reading this, consider upgrading your e-mail';
|
|
|
485 |
$this->__header[] = 'client to a MIME-compatible client.';
|
|
|
486 |
} elseif ($this->sendAs === 'text') {
|
|
|
487 |
$this->__header[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
488 |
} elseif ($this->sendAs === 'html') {
|
|
|
489 |
$this->__header[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
490 |
} elseif ($this->sendAs === 'both') {
|
|
|
491 |
$this->__header[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
|
|
|
492 |
$this->__header[] = '';
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
$this->__header[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
496 |
}
|
|
|
497 |
/**
|
|
|
498 |
* Format the message by seeing if it has attachments.
|
|
|
499 |
*
|
|
|
500 |
* @param string $message Message to format
|
|
|
501 |
* @access private
|
|
|
502 |
*/
|
|
|
503 |
function __formatMessage($message) {
|
|
|
504 |
if (!empty($this->attachments)) {
|
|
|
505 |
$prefix = array(
|
|
|
506 |
'--' . $this->__boundary,
|
|
|
507 |
'Content-Type: text/plain; charset=' . $this->charset,
|
|
|
508 |
'Content-Transfer-Encoding: 7bit',
|
|
|
509 |
''
|
|
|
510 |
);
|
|
|
511 |
$message = array_merge($prefix, $message);
|
|
|
512 |
}
|
|
|
513 |
return $message;
|
|
|
514 |
}
|
|
|
515 |
/**
|
|
|
516 |
* Attach files by adding file contents inside boundaries.
|
|
|
517 |
*
|
|
|
518 |
* @access private
|
|
|
519 |
* @TODO: modify to use the core File class?
|
|
|
520 |
*/
|
|
|
521 |
function __attachFiles() {
|
|
|
522 |
$files = array();
|
|
|
523 |
foreach ($this->attachments as $attachment) {
|
|
|
524 |
$file = $this->__findFiles($attachment);
|
|
|
525 |
if (!empty($file)) {
|
|
|
526 |
$files[] = $file;
|
|
|
527 |
}
|
|
|
528 |
}
|
|
|
529 |
|
|
|
530 |
foreach ($files as $file) {
|
|
|
531 |
$handle = fopen($file, 'rb');
|
|
|
532 |
$data = fread($handle, filesize($file));
|
|
|
533 |
$data = chunk_split(base64_encode($data)) ;
|
|
|
534 |
fclose($handle);
|
|
|
535 |
|
|
|
536 |
$this->__message[] = '--' . $this->__boundary;
|
|
|
537 |
$this->__message[] = 'Content-Type: application/octet-stream';
|
|
|
538 |
$this->__message[] = 'Content-Transfer-Encoding: base64';
|
|
|
539 |
$this->__message[] = 'Content-Disposition: attachment; filename="' . basename($file) . '"';
|
|
|
540 |
$this->__message[] = '';
|
|
|
541 |
$this->__message[] = $data;
|
|
|
542 |
$this->__message[] = '';
|
|
|
543 |
}
|
|
|
544 |
}
|
|
|
545 |
/**
|
|
|
546 |
* Find the specified attachment in the list of file paths
|
|
|
547 |
*
|
|
|
548 |
* @param string $attachment Attachment file name to find
|
|
|
549 |
* @return string Path to located file
|
|
|
550 |
* @access private
|
|
|
551 |
*/
|
|
|
552 |
function __findFiles($attachment) {
|
|
|
553 |
if (file_exists($attachment)) {
|
|
|
554 |
return $attachment;
|
|
|
555 |
}
|
|
|
556 |
foreach ($this->filePaths as $path) {
|
|
|
557 |
if (file_exists($path . DS . $attachment)) {
|
|
|
558 |
$file = $path . DS . $attachment;
|
|
|
559 |
return $file;
|
|
|
560 |
}
|
|
|
561 |
}
|
|
|
562 |
return null;
|
|
|
563 |
}
|
|
|
564 |
/**
|
|
|
565 |
* Wrap the message using EmailComponent::$lineLength
|
|
|
566 |
*
|
|
|
567 |
* @param string $message Message to wrap
|
|
|
568 |
* @return array Wrapped message
|
|
|
569 |
* @access private
|
|
|
570 |
*/
|
|
|
571 |
function __wrap($message) {
|
|
|
572 |
$message = $this->__strip($message, true);
|
|
|
573 |
$message = str_replace(array("\r\n","\r"), "\n", $message);
|
|
|
574 |
$lines = explode("\n", $message);
|
|
|
575 |
$formatted = array();
|
|
|
576 |
|
|
|
577 |
if ($this->_lineLength !== null) {
|
|
|
578 |
trigger_error('_lineLength cannot be accessed please use lineLength', E_USER_WARNING);
|
|
|
579 |
$this->lineLength = $this->_lineLength;
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
foreach ($lines as $line) {
|
|
|
583 |
if (substr($line, 0, 1) == '.') {
|
|
|
584 |
$line = '.' . $line;
|
|
|
585 |
}
|
|
|
586 |
$formatted = array_merge($formatted, explode("\n", wordwrap($line, $this->lineLength, "\n", true)));
|
|
|
587 |
}
|
|
|
588 |
$formatted[] = '';
|
|
|
589 |
return $formatted;
|
|
|
590 |
}
|
|
|
591 |
/**
|
|
|
592 |
* Encode the specified string using the current charset
|
|
|
593 |
*
|
|
|
594 |
* @param string $subject String to encode
|
|
|
595 |
* @return string Encoded string
|
|
|
596 |
* @access private
|
|
|
597 |
*/
|
|
|
598 |
function __encode($subject) {
|
|
|
599 |
$subject = $this->__strip($subject);
|
|
|
600 |
|
|
|
601 |
$nl = "\r\n";
|
|
|
602 |
if ($this->delivery == 'mail') {
|
|
|
603 |
$nl = '';
|
|
|
604 |
}
|
|
|
605 |
return mb_encode_mimeheader($subject, $this->charset, 'B', $nl);
|
|
|
606 |
}
|
|
|
607 |
/**
|
|
|
608 |
* Format a string as an email address
|
|
|
609 |
*
|
|
|
610 |
* @param string $string String representing an email address
|
|
|
611 |
* @return string Email address suitable for email headers or smtp pipe
|
|
|
612 |
* @access private
|
|
|
613 |
*/
|
|
|
614 |
function __formatAddress($string, $smtp = false) {
|
|
|
615 |
if (strpos($string, '<') !== false) {
|
|
|
616 |
$value = explode('<', $string);
|
|
|
617 |
if ($smtp) {
|
|
|
618 |
$string = '<' . $value[1];
|
|
|
619 |
} else {
|
|
|
620 |
$string = $this->__encode($value[0]) . ' <' . $value[1];
|
|
|
621 |
}
|
|
|
622 |
}
|
|
|
623 |
return $this->__strip($string);
|
|
|
624 |
}
|
|
|
625 |
/**
|
|
|
626 |
* Remove certain elements (such as bcc:, to:, %0a) from given value
|
|
|
627 |
*
|
|
|
628 |
* @param string $value Value to strip
|
|
|
629 |
* @param boolean $message Set to true to indicate main message content
|
|
|
630 |
* @return string Stripped value
|
|
|
631 |
* @access private
|
|
|
632 |
*/
|
|
|
633 |
function __strip($value, $message = false) {
|
|
|
634 |
$search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:|charset\=|mime-version\:|multipart/mixed|(?:to|b?cc)\:.*';
|
|
|
635 |
if ($message !== true) {
|
|
|
636 |
$search .= '|\r|\n';
|
|
|
637 |
}
|
|
|
638 |
$search = '#(?:' . $search . ')#i';
|
|
|
639 |
while (preg_match($search, $value)) {
|
|
|
640 |
$value = preg_replace($search, '', $value);
|
|
|
641 |
}
|
|
|
642 |
return $value;
|
|
|
643 |
}
|
|
|
644 |
/**
|
|
|
645 |
* Wrapper for PHP mail function used for sending out emails
|
|
|
646 |
*
|
|
|
647 |
* @return bool Success
|
|
|
648 |
* @access private
|
|
|
649 |
*/
|
|
|
650 |
function __mail() {
|
|
|
651 |
$header = implode("\n", $this->__header);
|
|
|
652 |
$message = implode("\n", $this->__message);
|
|
|
653 |
if (ini_get('safe_mode')) {
|
|
|
654 |
return @mail($this->to, $this->__encode($this->subject), $message, $header);
|
|
|
655 |
}
|
|
|
656 |
return @mail($this->to, $this->__encode($this->subject), $message, $header, $this->additionalParams);
|
|
|
657 |
}
|
|
|
658 |
/**
|
|
|
659 |
* Sends out email via SMTP
|
|
|
660 |
*
|
|
|
661 |
* @return bool Success
|
|
|
662 |
* @access private
|
|
|
663 |
*/
|
|
|
664 |
function __smtp() {
|
|
|
665 |
App::import('Core', array('Socket'));
|
|
|
666 |
|
|
|
667 |
$this->__smtpConnection =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->smtpOptions));
|
|
|
668 |
|
|
|
669 |
if (!$this->__smtpConnection->connect()) {
|
|
|
670 |
$this->smtpError = $this->__smtpConnection->lastError();
|
|
|
671 |
return false;
|
|
|
672 |
} elseif (!$this->__smtpSend(null, '220')) {
|
|
|
673 |
return false;
|
|
|
674 |
}
|
|
|
675 |
|
|
|
676 |
if (!$this->__smtpSend('HELO cake', '250')) {
|
|
|
677 |
return false;
|
|
|
678 |
}
|
|
|
679 |
|
|
|
680 |
if (isset($this->smtpOptions['username']) && isset($this->smtpOptions['password'])) {
|
|
|
681 |
$authRequired = $this->__smtpSend('AUTH LOGIN', '334|503');
|
|
|
682 |
if ($authRequired == '334') {
|
|
|
683 |
if (!$this->__smtpSend(base64_encode($this->smtpOptions['username']), '334')) {
|
|
|
684 |
return false;
|
|
|
685 |
}
|
|
|
686 |
if (!$this->__smtpSend(base64_encode($this->smtpOptions['password']), '235')) {
|
|
|
687 |
return false;
|
|
|
688 |
}
|
|
|
689 |
} elseif ($authRequired != '503') {
|
|
|
690 |
return false;
|
|
|
691 |
}
|
|
|
692 |
}
|
|
|
693 |
|
|
|
694 |
if (!$this->__smtpSend('MAIL FROM: ' . $this->__formatAddress($this->from, true))) {
|
|
|
695 |
return false;
|
|
|
696 |
}
|
|
|
697 |
|
|
|
698 |
if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($this->to, true))) {
|
|
|
699 |
return false;
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
foreach ($this->cc as $cc) {
|
|
|
703 |
if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($cc, true))) {
|
|
|
704 |
return false;
|
|
|
705 |
}
|
|
|
706 |
}
|
|
|
707 |
foreach ($this->bcc as $bcc) {
|
|
|
708 |
if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($bcc, true))) {
|
|
|
709 |
return false;
|
|
|
710 |
}
|
|
|
711 |
}
|
|
|
712 |
|
|
|
713 |
if (!$this->__smtpSend('DATA', '354')) {
|
|
|
714 |
return false;
|
|
|
715 |
}
|
|
|
716 |
|
|
|
717 |
$header = implode("\r\n", $this->__header);
|
|
|
718 |
$message = implode("\r\n", $this->__message);
|
|
|
719 |
if (!$this->__smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) {
|
|
|
720 |
return false;
|
|
|
721 |
}
|
|
|
722 |
$this->__smtpSend('QUIT', false);
|
|
|
723 |
|
|
|
724 |
$this->__smtpConnection->disconnect();
|
|
|
725 |
return true;
|
|
|
726 |
}
|
|
|
727 |
/**
|
|
|
728 |
* Private method for sending data to SMTP connection
|
|
|
729 |
*
|
|
|
730 |
* @param string $data data to be sent to SMTP server
|
|
|
731 |
* @param mixed $checkCode code to check for in server response, false to skip
|
|
|
732 |
* @return bool Success
|
|
|
733 |
* @access private
|
|
|
734 |
*/
|
|
|
735 |
function __smtpSend($data, $checkCode = '250') {
|
|
|
736 |
if (!is_null($data)) {
|
|
|
737 |
$this->__smtpConnection->write($data . "\r\n");
|
|
|
738 |
}
|
|
|
739 |
if ($checkCode !== false) {
|
|
|
740 |
$response = $this->__smtpConnection->read();
|
|
|
741 |
|
|
|
742 |
if (preg_match('/^(' . $checkCode . ')/', $response, $code)) {
|
|
|
743 |
return $code[0];
|
|
|
744 |
}
|
|
|
745 |
$this->smtpError = $response;
|
|
|
746 |
return false;
|
|
|
747 |
}
|
|
|
748 |
return true;
|
|
|
749 |
}
|
|
|
750 |
/**
|
|
|
751 |
* Set as controller flash message a debug message showing current settings in component
|
|
|
752 |
*
|
|
|
753 |
* @return boolean Success
|
|
|
754 |
* @access private
|
|
|
755 |
*/
|
|
|
756 |
function __debug() {
|
|
|
757 |
$nl = "\n";
|
|
|
758 |
$header = implode($nl, $this->__header);
|
|
|
759 |
$message = implode($nl, $this->__message);
|
|
|
760 |
$fm = '<pre>';
|
|
|
761 |
|
|
|
762 |
if ($this->delivery == 'smtp') {
|
|
|
763 |
$fm .= sprintf('%s %s%s', 'Host:', $this->smtpOptions['host'], $nl);
|
|
|
764 |
$fm .= sprintf('%s %s%s', 'Port:', $this->smtpOptions['port'], $nl);
|
|
|
765 |
$fm .= sprintf('%s %s%s', 'Timeout:', $this->smtpOptions['timeout'], $nl);
|
|
|
766 |
}
|
|
|
767 |
$fm .= sprintf('%s %s%s', 'To:', $this->to, $nl);
|
|
|
768 |
$fm .= sprintf('%s %s%s', 'From:', $this->from, $nl);
|
|
|
769 |
$fm .= sprintf('%s %s%s', 'Subject:', $this->__encode($this->subject), $nl);
|
|
|
770 |
$fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);
|
|
|
771 |
$fm .= sprintf('%s%3$s%3$s%s', 'Parameters:', $this->additionalParams, $nl);
|
|
|
772 |
$fm .= sprintf('%s%3$s%3$s%s', 'Message:', $message, $nl);
|
|
|
773 |
$fm .= '</pre>';
|
|
|
774 |
|
|
|
775 |
$this->Controller->Session->setFlash($fm, 'default', null, 'email');
|
|
|
776 |
return true;
|
|
|
777 |
}
|
|
|
778 |
|
|
|
779 |
}
|
|
|
780 |
?>
|