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