1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('Component', 'Controller');
21: App::uses('Multibyte', 'I18n');
22: App::uses('CakeEmail', 'Network/Email');
23:
24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: class EmailComponent extends Component {
36:
37: 38: 39: 40: 41:
42: public $to = null;
43:
44: 45: 46: 47: 48:
49: public $from = null;
50:
51: 52: 53: 54: 55:
56: public $replyTo = null;
57:
58: 59: 60: 61: 62:
63: public $readReceipt = null;
64:
65: 66: 67: 68: 69: 70: 71: 72:
73: public $return = null;
74:
75: 76: 77: 78: 79: 80: 81: 82:
83: public $cc = array();
84:
85: 86: 87: 88: 89: 90: 91: 92:
93: public $bcc = array();
94:
95: 96: 97: 98: 99: 100: 101:
102: public $date = null;
103:
104: 105: 106: 107: 108:
109: public $subject = null;
110:
111: 112: 113: 114: 115: 116:
117: public $headers = array();
118:
119: 120: 121: 122: 123: 124: 125:
126: public $additionalParams = null;
127:
128: 129: 130: 131: 132:
133: public $layout = 'default';
134:
135: 136: 137: 138: 139:
140: public $template = null;
141:
142: 143: 144: 145: 146: 147: 148: 149: 150:
151: public $lineFeed = PHP_EOL;
152:
153: 154: 155: 156: 157: 158: 159: 160: 161: 162:
163: public $sendAs = 'text';
164:
165: 166: 167: 168: 169: 170: 171: 172: 173: 174:
175: public $delivery = 'mail';
176:
177: 178: 179: 180: 181:
182: public $charset = 'utf-8';
183:
184: 185: 186: 187: 188: 189: 190:
191: public $attachments = array();
192:
193: 194: 195: 196: 197:
198: public $xMailer = 'CakePHP Email Component';
199:
200: 201: 202: 203: 204:
205: public $filePaths = array();
206:
207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219:
220: public $smtpOptions = array();
221:
222: 223: 224: 225: 226:
227: public $textMessage = null;
228:
229: 230: 231: 232: 233:
234: public $htmlMessage = null;
235:
236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246:
247: public $messageId = true;
248:
249: 250: 251: 252: 253:
254: protected $_controller = null;
255:
256: 257: 258: 259: 260: 261:
262: public function __construct(ComponentCollection $collection, $settings = array()) {
263: $this->_controller = $collection->getController();
264: parent::__construct($collection, $settings);
265: }
266:
267: 268: 269: 270: 271: 272:
273: public function initialize($controller) {
274: if (Configure::read('App.encoding') !== null) {
275: $this->charset = Configure::read('App.encoding');
276: }
277: }
278:
279: 280: 281: 282: 283: 284: 285: 286: 287:
288: public function send($content = null, $template = null, $layout = null) {
289: $lib = new CakeEmail();
290: $lib->charset = $this->charset;
291:
292: $lib->from($this->_formatAddresses((array)$this->from));
293: if (!empty($this->to)) {
294: $lib->to($this->_formatAddresses((array)$this->to));
295: }
296: if (!empty($this->cc)) {
297: $lib->cc($this->_formatAddresses((array)$this->cc));
298: }
299: if (!empty($this->bcc)) {
300: $lib->bcc($this->_formatAddresses((array)$this->bcc));
301: }
302: if (!empty($this->replyTo)) {
303: $lib->replyTo($this->_formatAddresses((array)$this->replyTo));
304: }
305: if (!empty($this->return)) {
306: $lib->returnPath($this->_formatAddresses((array)$this->return));
307: }
308: if (!empty($readReceipt)) {
309: $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
310: }
311:
312: $lib->subject($this->subject)->messageID($this->messageId);
313: $lib->helpers($this->_controller->helpers);
314:
315: $headers = array('X-Mailer' => $this->xMailer);
316: foreach ($this->headers as $key => $value) {
317: $headers['X-' . $key] = $value;
318: }
319: if ($this->date != false) {
320: $headers['Date'] = $this->date;
321: }
322: $lib->setHeaders($headers);
323:
324: if ($template) {
325: $this->template = $template;
326: }
327: if ($layout) {
328: $this->layout = $layout;
329: }
330: $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
331:
332: if (!empty($this->attachments)) {
333: $lib->attachments($this->_formatAttachFiles());
334: }
335:
336: $lib->transport(ucfirst($this->delivery));
337: if ($this->delivery === 'mail') {
338: $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
339: } elseif ($this->delivery === 'smtp') {
340: $lib->config($this->smtpOptions);
341: } else {
342: $lib->config(array());
343: }
344:
345: $sent = $lib->send($content);
346:
347: $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
348: if (empty($this->htmlMessage)) {
349: $this->htmlMessage = null;
350: }
351: $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
352: if (empty($this->textMessage)) {
353: $this->textMessage = null;
354: }
355:
356: $this->_header = array();
357: $this->_message = array();
358:
359: return $sent;
360: }
361:
362: 363: 364: 365: 366:
367: public function reset() {
368: $this->template = null;
369: $this->to = array();
370: $this->from = null;
371: $this->replyTo = null;
372: $this->return = null;
373: $this->cc = array();
374: $this->bcc = array();
375: $this->subject = null;
376: $this->additionalParams = null;
377: $this->date = null;
378: $this->attachments = array();
379: $this->htmlMessage = null;
380: $this->textMessage = null;
381: $this->messageId = true;
382: $this->delivery = 'mail';
383: }
384:
385: 386: 387: 388: 389:
390: protected function _formatAttachFiles() {
391: $files = array();
392: foreach ($this->attachments as $filename => $attachment) {
393: $file = $this->_findFiles($attachment);
394: if (!empty($file)) {
395: if (is_int($filename)) {
396: $filename = basename($file);
397: }
398: $files[$filename] = $file;
399: }
400: }
401: return $files;
402: }
403:
404: 405: 406: 407: 408: 409:
410: protected function _findFiles($attachment) {
411: if (file_exists($attachment)) {
412: return $attachment;
413: }
414: foreach ($this->filePaths as $path) {
415: if (file_exists($path . DS . $attachment)) {
416: $file = $path . DS . $attachment;
417: return $file;
418: }
419: }
420: return null;
421: }
422:
423: 424: 425: 426: 427: 428:
429: protected function _encode($subject) {
430: $subject = $this->_strip($subject);
431:
432: $nl = "\r\n";
433: if ($this->delivery == 'mail') {
434: $nl = '';
435: }
436: $internalEncoding = function_exists('mb_internal_encoding');
437: if ($internalEncoding) {
438: $restore = mb_internal_encoding();
439: mb_internal_encoding($this->charset);
440: }
441: $return = mb_encode_mimeheader($subject, $this->charset, 'B', $nl);
442: if ($internalEncoding) {
443: mb_internal_encoding($restore);
444: }
445: return $return;
446: }
447:
448: 449: 450: 451: 452: 453:
454: protected function _formatAddresses($addresses) {
455: $formatted = array();
456: foreach ($addresses as $address) {
457: if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
458: $formatted[$this->_strip($matches[3])] = $this->_encode($matches[2]);
459: } else {
460: $address = $this->_strip($address);
461: $formatted[$address] = $address;
462: }
463: }
464: return $formatted;
465: }
466:
467: 468: 469: 470: 471: 472: 473: 474:
475: protected function _strip($value, $message = false) {
476: $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
477: $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
478:
479: if ($message !== true) {
480: $search .= '|\r|\n';
481: }
482: $search = '#(?:' . $search . ')#i';
483: while (preg_match($search, $value)) {
484: $value = preg_replace($search, '', $value);
485: }
486: return $value;
487: }
488:
489: }
490: