1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17: 
 18: 
 19: App::uses('Component', 'Controller');
 20: App::uses('Multibyte', 'I18n');
 21: App::uses('CakeEmail', 'Network/Email');
 22: 
 23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33: 
 34: class EmailComponent extends Component {
 35: 
 36:  37:  38:  39:  40: 
 41:     public $to = null;
 42: 
 43:  44:  45:  46:  47: 
 48:     public $from = null;
 49: 
 50:  51:  52:  53:  54: 
 55:     public $replyTo = null;
 56: 
 57:  58:  59:  60:  61: 
 62:     public $readReceipt = null;
 63: 
 64:  65:  66:  67:  68:  69:  70:  71: 
 72:     public $return = null;
 73: 
 74:  75:  76:  77:  78:  79:  80:  81: 
 82:     public $cc = array();
 83: 
 84:  85:  86:  87:  88:  89:  90:  91: 
 92:     public $bcc = array();
 93: 
 94:  95:  96:  97:  98:  99: 100: 
101:     public $date = null;
102: 
103: 104: 105: 106: 107: 
108:     public $subject = null;
109: 
110: 111: 112: 113: 114: 115: 
116:     public $headers = array();
117: 
118: 119: 120: 121: 122: 123: 124: 
125:     public $additionalParams = null;
126: 
127: 128: 129: 130: 131: 
132:     public $layout = 'default';
133: 
134: 135: 136: 137: 138: 
139:     public $template = null;
140: 
141: 142: 143: 144: 145: 146: 147: 148: 149: 
150:     public $lineFeed = PHP_EOL;
151: 
152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 
162:     public $sendAs = 'text';
163: 
164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 
174:     public $delivery = 'mail';
175: 
176: 177: 178: 179: 180: 
181:     public $charset = 'utf-8';
182: 
183: 184: 185: 186: 187: 188: 189: 
190:     public $attachments = array();
191: 
192: 193: 194: 195: 196: 
197:     public $xMailer = 'CakePHP Email Component';
198: 
199: 200: 201: 202: 203: 
204:     public $filePaths = array();
205: 
206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 
219:     public $smtpOptions = array();
220: 
221: 222: 223: 224: 225: 
226:     public $textMessage = null;
227: 
228: 229: 230: 231: 232: 
233:     public $htmlMessage = null;
234: 
235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 
246:     public $messageId = true;
247: 
248: 249: 250: 251: 252: 
253:     protected $_controller = null;
254: 
255: 256: 257: 258: 259: 260: 
261:     public function __construct(ComponentCollection $collection, $settings = array()) {
262:         $this->_controller = $collection->getController();
263:         parent::__construct($collection, $settings);
264:     }
265: 
266: 267: 268: 269: 270: 271: 
272:     public function initialize(Controller $controller) {
273:         if (Configure::read('App.encoding') !== null) {
274:             $this->charset = Configure::read('App.encoding');
275:         }
276:     }
277: 
278: 279: 280: 281: 282: 283: 284: 285: 286: 
287:     public function send($content = null, $template = null, $layout = null) {
288:         $lib = new CakeEmail();
289:         $lib->charset = $this->charset;
290:         $lib->headerCharset = $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($this->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) {
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 _formatAddresses($addresses) {
430:         $formatted = array();
431:         foreach ($addresses as $address) {
432:             if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
433:                 $formatted[$this->_strip($matches[3])] = $matches[2];
434:             } else {
435:                 $address = $this->_strip($address);
436:                 $formatted[$address] = $address;
437:             }
438:         }
439:         return $formatted;
440:     }
441: 
442: 443: 444: 445: 446: 447: 448: 449: 
450:     protected function _strip($value, $message = false) {
451:         $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
452:         $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
453: 
454:         if ($message !== true) {
455:             $search .= '|\r|\n';
456:         }
457:         $search = '#(?:' . $search . ')#i';
458:         while (preg_match($search, $value)) {
459:             $value = preg_replace($search, '', $value);
460:         }
461:         return $value;
462:     }
463: 
464: }
465: