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 $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: $lib->headerCharset = $this->charset;
292:
293: $lib->from($this->_formatAddresses((array)$this->from));
294: if (!empty($this->to)) {
295: $lib->to($this->_formatAddresses((array)$this->to));
296: }
297: if (!empty($this->cc)) {
298: $lib->cc($this->_formatAddresses((array)$this->cc));
299: }
300: if (!empty($this->bcc)) {
301: $lib->bcc($this->_formatAddresses((array)$this->bcc));
302: }
303: if (!empty($this->replyTo)) {
304: $lib->replyTo($this->_formatAddresses((array)$this->replyTo));
305: }
306: if (!empty($this->return)) {
307: $lib->returnPath($this->_formatAddresses((array)$this->return));
308: }
309: if (!empty($this->readReceipt)) {
310: $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
311: }
312:
313: $lib->subject($this->subject)->messageID($this->messageId);
314: $lib->helpers($this->_controller->helpers);
315:
316: $headers = array('X-Mailer' => $this->xMailer);
317: foreach ($this->headers as $key => $value) {
318: $headers['X-' . $key] = $value;
319: }
320: if ($this->date != false) {
321: $headers['Date'] = $this->date;
322: }
323: $lib->setHeaders($headers);
324:
325: if ($template) {
326: $this->template = $template;
327: }
328: if ($layout) {
329: $this->layout = $layout;
330: }
331: $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
332:
333: if (!empty($this->attachments)) {
334: $lib->attachments($this->_formatAttachFiles());
335: }
336:
337: $lib->transport(ucfirst($this->delivery));
338: if ($this->delivery === 'mail') {
339: $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
340: } elseif ($this->delivery === 'smtp') {
341: $lib->config($this->smtpOptions);
342: } else {
343: $lib->config(array());
344: }
345:
346: $sent = $lib->send($content);
347:
348: $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
349: if (empty($this->htmlMessage)) {
350: $this->htmlMessage = null;
351: }
352: $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
353: if (empty($this->textMessage)) {
354: $this->textMessage = null;
355: }
356:
357: $this->_header = array();
358: $this->_message = array();
359:
360: return $sent;
361: }
362:
363: 364: 365: 366: 367:
368: public function reset() {
369: $this->template = null;
370: $this->to = array();
371: $this->from = null;
372: $this->replyTo = null;
373: $this->return = null;
374: $this->cc = array();
375: $this->bcc = array();
376: $this->subject = null;
377: $this->additionalParams = null;
378: $this->date = null;
379: $this->attachments = array();
380: $this->htmlMessage = null;
381: $this->textMessage = null;
382: $this->messageId = true;
383: $this->delivery = 'mail';
384: }
385:
386: 387: 388: 389: 390:
391: protected function _formatAttachFiles() {
392: $files = array();
393: foreach ($this->attachments as $filename => $attachment) {
394: $file = $this->_findFiles($attachment);
395: if (!empty($file)) {
396: if (is_int($filename)) {
397: $filename = basename($file);
398: }
399: $files[$filename] = $file;
400: }
401: }
402: return $files;
403: }
404:
405: 406: 407: 408: 409: 410:
411: protected function _findFiles($attachment) {
412: if (file_exists($attachment)) {
413: return $attachment;
414: }
415: foreach ($this->filePaths as $path) {
416: if (file_exists($path . DS . $attachment)) {
417: $file = $path . DS . $attachment;
418: return $file;
419: }
420: }
421: return null;
422: }
423:
424: 425: 426: 427: 428: 429:
430: protected function _formatAddresses($addresses) {
431: $formatted = array();
432: foreach ($addresses as $address) {
433: if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
434: $formatted[$this->_strip($matches[3])] = $matches[2];
435: } else {
436: $address = $this->_strip($address);
437: $formatted[$address] = $address;
438: }
439: }
440: return $formatted;
441: }
442:
443: 444: 445: 446: 447: 448: 449: 450:
451: protected function _strip($value, $message = false) {
452: $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
453: $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
454:
455: if ($message !== true) {
456: $search .= '|\r|\n';
457: }
458: $search = '#(?:' . $search . ')#i';
459: while (preg_match($search, $value)) {
460: $value = preg_replace($search, '', $value);
461: }
462: return $value;
463: }
464:
465: }
466: