00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 App::import('Core', 'Multibyte');
00038 class EmailComponent extends Object{
00039
00040
00041
00042
00043
00044
00045 var $to = null;
00046
00047
00048
00049
00050
00051
00052 var $from = null;
00053
00054
00055
00056
00057
00058
00059 var $replyTo = null;
00060
00061
00062
00063
00064
00065
00066 var $readReceipt = null;
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 var $return = null;
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 var $cc = array();
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 var $bcc = array();
00097
00098
00099
00100
00101
00102
00103 var $subject = null;
00104
00105
00106
00107
00108
00109
00110
00111 var $headers = array();
00112
00113
00114
00115
00116
00117
00118
00119
00120 var $additionalParams = null;
00121
00122
00123
00124
00125
00126
00127 var $layout = 'default';
00128
00129
00130
00131
00132
00133
00134 var $template = null;
00135
00136
00137
00138
00139
00140
00141 var $lineLength = 70;
00142
00143
00144
00145 var $_lineLength = null;
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 var $sendAs = 'text';
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 var $delivery = 'mail';
00170
00171
00172
00173
00174
00175
00176 var $charset = 'utf-8';
00177
00178
00179
00180
00181
00182
00183
00184
00185 var $attachments = array();
00186
00187
00188
00189
00190
00191
00192 var $xMailer = 'CakePHP Email Component';
00193
00194
00195
00196
00197
00198
00199 var $filePaths = array();
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 var $smtpOptions = array(
00214 'port'=> 25, 'host' => 'localhost', 'timeout' => 30
00215 );
00216
00217
00218
00219
00220
00221
00222
00223 var $smtpError = null;
00224
00225
00226
00227
00228
00229
00230 var $_debug = false;
00231
00232
00233
00234
00235
00236
00237 var $__header = array();
00238
00239
00240
00241
00242
00243
00244 var $__boundary = null;
00245
00246
00247
00248
00249
00250
00251 var $__message = array();
00252
00253
00254
00255
00256
00257
00258 var $__smtpConnection = null;
00259
00260
00261
00262
00263
00264
00265 function initialize(&$controller, $settings = array()) {
00266 $this->Controller =& $controller;
00267 if (Configure::read('App.encoding') !== null) {
00268 $this->charset = Configure::read('App.encoding');
00269 }
00270 $this->_set($settings);
00271 }
00272
00273
00274
00275
00276
00277
00278 function startup(&$controller) {}
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 function send($content = null, $template = null, $layout = null) {
00289 $this->__createHeader();
00290
00291 if ($template) {
00292 $this->template = $template;
00293 }
00294
00295 if ($layout) {
00296 $this->layout = $layout;
00297 }
00298
00299 if (is_array($content)) {
00300 $content = implode("\n", $content) . "\n";
00301 }
00302
00303 $message = $this->__wrap($content);
00304 if ($this->template === null) {
00305 $message = $this->__formatMessage($message);
00306 } else {
00307 $message = $this->__renderTemplate($message);
00308 }
00309 $message[] = '';
00310 $this->__message = $message;
00311
00312 if (!empty($this->attachments)) {
00313 $this->__attachFiles();
00314 }
00315
00316 if (!is_null($this->__boundary)) {
00317 $this->__message[] = '';
00318 $this->__message[] = '--' . $this->__boundary . '--';
00319 $this->__message[] = '';
00320 }
00321
00322 if ($this->_debug) {
00323 return $this->__debug();
00324 }
00325 $__method = '__' . $this->delivery;
00326 $sent = $this->$__method();
00327
00328 $this->__header = array();
00329 $this->__message = array();
00330
00331 return $sent;
00332 }
00333
00334
00335
00336
00337
00338 function reset() {
00339 $this->template = null;
00340 $this->to = null;
00341 $this->from = null;
00342 $this->replyTo = null;
00343 $this->return = null;
00344 $this->cc = array();
00345 $this->bcc = array();
00346 $this->subject = null;
00347 $this->additionalParams = null;
00348 $this->smtpError = null;
00349 $this->attachments = array();
00350 $this->__header = array();
00351 $this->__boundary = null;
00352 $this->__message = array();
00353 }
00354
00355
00356
00357
00358
00359
00360
00361 function __renderTemplate($content) {
00362 $viewClass = $this->Controller->view;
00363
00364 if ($viewClass != 'View') {
00365 if (strpos($viewClass, '.') !== false) {
00366 list($plugin, $viewClass) = explode('.', $viewClass);
00367 }
00368 $viewClass = $viewClass . 'View';
00369 App::import('View', $this->Controller->view);
00370 }
00371 $View = new $viewClass($this->Controller, false);
00372 $View->layout = $this->layout;
00373 $msg = array();
00374
00375 $content = implode("\n", $content);
00376
00377 if ($this->sendAs === 'both') {
00378 $htmlContent = $content;
00379 if (!empty($this->attachments)) {
00380 $msg[] = '--' . $this->__boundary;
00381 $msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
00382 $msg[] = '';
00383 }
00384 $msg[] = '--alt-' . $this->__boundary;
00385 $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
00386 $msg[] = 'Content-Transfer-Encoding: 7bit';
00387 $msg[] = '';
00388
00389 $content = $View->element('email' . DS . 'text' . DS . $this->template, array('content' => $content), true);
00390 $View->layoutPath = 'email' . DS . 'text';
00391 $content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
00392 $msg = array_merge($msg, $content);
00393
00394 $msg[] = '';
00395 $msg[] = '--alt-' . $this->__boundary;
00396 $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
00397 $msg[] = 'Content-Transfer-Encoding: 7bit';
00398 $msg[] = '';
00399
00400 $htmlContent = $View->element('email' . DS . 'html' . DS . $this->template, array('content' => $htmlContent), true);
00401 $View->layoutPath = 'email' . DS . 'html';
00402 $htmlContent = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
00403 $msg = array_merge($msg, $htmlContent);
00404 $msg[] = '';
00405 $msg[] = '--alt-' . $this->__boundary . '--';
00406 $msg[] = '';
00407
00408 return $msg;
00409 }
00410
00411 if (!empty($this->attachments)) {
00412 if ($this->sendAs === 'html') {
00413 $msg[] = '';
00414 $msg[] = '--' . $this->__boundary;
00415 $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
00416 $msg[] = 'Content-Transfer-Encoding: 7bit';
00417 $msg[] = '';
00418 } else {
00419 $msg[] = '--' . $this->__boundary;
00420 $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
00421 $msg[] = 'Content-Transfer-Encoding: 7bit';
00422 $msg[] = '';
00423 }
00424 }
00425
00426 $content = $View->element('email' . DS . $this->sendAs . DS . $this->template, array('content' => $content), true);
00427 $View->layoutPath = 'email' . DS . $this->sendAs;
00428 $content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
00429 $msg = array_merge($msg, $content);
00430
00431 return $msg;
00432 }
00433
00434
00435
00436
00437
00438 function __createBoundary() {
00439 $this->__boundary = md5(uniqid(time()));
00440 }
00441
00442
00443
00444
00445
00446
00447 function __createHeader() {
00448 if ($this->delivery == 'smtp') {
00449 $this->__header[] = 'To: ' . $this->__formatAddress($this->to);
00450 }
00451 $this->__header[] = 'From: ' . $this->__formatAddress($this->from);
00452
00453 if (!empty($this->replyTo)) {
00454 $this->__header[] = 'Reply-To: ' . $this->__formatAddress($this->replyTo);
00455 }
00456 if (!empty($this->return)) {
00457 $this->__header[] = 'Return-Path: ' . $this->__formatAddress($this->return);
00458 }
00459 if (!empty($this->readReceipt)) {
00460 $this->__header[] = 'Disposition-Notification-To: ' . $this->__formatAddress($this->readReceipt);
00461 }
00462
00463 if (!empty($this->cc)) {
00464 $this->__header[] = 'cc: ' .implode(', ', array_map(array($this, '__formatAddress'), $this->cc));
00465 }
00466
00467 if (!empty($this->bcc) && $this->delivery != 'smtp') {
00468 $this->__header[] = 'Bcc: ' .implode(', ', array_map(array($this, '__formatAddress'), $this->bcc));
00469 }
00470 if ($this->delivery == 'smtp') {
00471 $this->__header[] = 'Subject: ' . $this->__encode($this->subject);
00472 }
00473 $this->__header[] = 'X-Mailer: ' . $this->xMailer;
00474
00475 if (!empty($this->headers)) {
00476 foreach ($this->headers as $key => $val) {
00477 $this->__header[] = 'X-' . $key . ': ' . $val;
00478 }
00479 }
00480
00481 if (!empty($this->attachments)) {
00482 $this->__createBoundary();
00483 $this->__header[] = 'MIME-Version: 1.0';
00484 $this->__header[] = 'Content-Type: multipart/mixed; boundary="' . $this->__boundary . '"';
00485 $this->__header[] = 'This part of the E-mail should never be seen. If';
00486 $this->__header[] = 'you are reading this, consider upgrading your e-mail';
00487 $this->__header[] = 'client to a MIME-compatible client.';
00488 } elseif ($this->sendAs === 'text') {
00489 $this->__header[] = 'Content-Type: text/plain; charset=' . $this->charset;
00490 } elseif ($this->sendAs === 'html') {
00491 $this->__header[] = 'Content-Type: text/html; charset=' . $this->charset;
00492 } elseif ($this->sendAs === 'both') {
00493 $this->__header[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
00494 }
00495
00496 $this->__header[] = 'Content-Transfer-Encoding: 7bit';
00497 }
00498
00499
00500
00501
00502
00503
00504 function __formatMessage($message) {
00505 if (!empty($this->attachments)) {
00506 $prefix = array('--' . $this->__boundary);
00507 if ($this->sendAs === 'text') {
00508 $prefix[] = 'Content-Type: text/plain; charset=' . $this->charset;
00509 } elseif ($this->sendAs === 'html') {
00510 $prefix[] = 'Content-Type: text/html; charset=' . $this->charset;
00511 } elseif ($this->sendAs === 'both') {
00512 $prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
00513 }
00514 $prefix[] = 'Content-Transfer-Encoding: 7bit';
00515 $prefix[] = '';
00516 $message = array_merge($prefix, $message);
00517 }
00518 return $message;
00519 }
00520
00521
00522
00523
00524
00525
00526 function __attachFiles() {
00527 $files = array();
00528 foreach ($this->attachments as $attachment) {
00529 $file = $this->__findFiles($attachment);
00530 if (!empty($file)) {
00531 $files[] = $file;
00532 }
00533 }
00534
00535 foreach ($files as $file) {
00536 $handle = fopen($file, 'rb');
00537 $data = fread($handle, filesize($file));
00538 $data = chunk_split(base64_encode($data)) ;
00539 fclose($handle);
00540
00541 $this->__message[] = '--' . $this->__boundary;
00542 $this->__message[] = 'Content-Type: application/octet-stream';
00543 $this->__message[] = 'Content-Transfer-Encoding: base64';
00544 $this->__message[] = 'Content-Disposition: attachment; filename="' . basename($file) . '"';
00545 $this->__message[] = '';
00546 $this->__message[] = $data;
00547 $this->__message[] = '';
00548 }
00549 }
00550
00551
00552
00553
00554
00555
00556
00557 function __findFiles($attachment) {
00558 if (file_exists($attachment)) {
00559 return $attachment;
00560 }
00561 foreach ($this->filePaths as $path) {
00562 if (file_exists($path . DS . $attachment)) {
00563 $file = $path . DS . $attachment;
00564 return $file;
00565 }
00566 }
00567 return null;
00568 }
00569
00570
00571
00572
00573
00574
00575
00576 function __wrap($message) {
00577 $message = $this->__strip($message, true);
00578 $message = str_replace(array("\r\n","\r"), "\n", $message);
00579 $lines = explode("\n", $message);
00580 $formatted = array();
00581
00582 if ($this->_lineLength !== null) {
00583 trigger_error('_lineLength cannot be accessed please use lineLength', E_USER_WARNING);
00584 $this->lineLength = $this->_lineLength;
00585 }
00586
00587 foreach ($lines as $line) {
00588 if (substr($line, 0, 1) == '.') {
00589 $line = '.' . $line;
00590 }
00591 $formatted = array_merge($formatted, explode("\n", wordwrap($line, $this->lineLength, "\n", true)));
00592 }
00593 $formatted[] = '';
00594 return $formatted;
00595 }
00596
00597
00598
00599
00600
00601
00602
00603 function __encode($subject) {
00604 $subject = $this->__strip($subject);
00605
00606 $nl = "\r\n";
00607 if ($this->delivery == 'mail') {
00608 $nl = '';
00609 }
00610 return mb_encode_mimeheader($subject, $this->charset, 'B', $nl);
00611 }
00612
00613
00614
00615
00616
00617
00618
00619 function __formatAddress($string, $smtp = false) {
00620 if (strpos($string, '<') !== false) {
00621 $value = explode('<', $string);
00622 if ($smtp) {
00623 $string = '<' . $value[1];
00624 } else {
00625 $string = $this->__encode($value[0]) . ' <' . $value[1];
00626 }
00627 }
00628 return $this->__strip($string);
00629 }
00630
00631
00632
00633
00634
00635
00636
00637
00638 function __strip($value, $message = false) {
00639 $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
00640 $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
00641
00642 if ($message !== true) {
00643 $search .= '|\r|\n';
00644 }
00645 $search = '#(?:' . $search . ')#i';
00646 while (preg_match($search, $value)) {
00647 $value = preg_replace($search, '', $value);
00648 }
00649 return $value;
00650 }
00651
00652
00653
00654
00655
00656
00657 function __mail() {
00658 $header = implode("\n", $this->__header);
00659 $message = implode("\n", $this->__message);
00660 if (ini_get('safe_mode')) {
00661 return @mail($this->to, $this->__encode($this->subject), $message, $header);
00662 }
00663 return @mail($this->to, $this->__encode($this->subject), $message, $header, $this->additionalParams);
00664 }
00665
00666
00667
00668
00669
00670
00671 function __smtp() {
00672 App::import('Core', array('Socket'));
00673
00674 $this->__smtpConnection =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->smtpOptions));
00675
00676 if (!$this->__smtpConnection->connect()) {
00677 $this->smtpError = $this->__smtpConnection->lastError();
00678 return false;
00679 } elseif (!$this->__smtpSend(null, '220')) {
00680 return false;
00681 }
00682
00683 if (isset($this->smtpOptions['client'])) {
00684 $host = $this->smtpOptions['client'];
00685 } else {
00686 $host = env('HTTP_HOST');
00687 }
00688
00689 if (!$this->__smtpSend("HELO {$host}", '250')) {
00690 return false;
00691 }
00692
00693 if (isset($this->smtpOptions['username']) && isset($this->smtpOptions['password'])) {
00694 $authRequired = $this->__smtpSend('AUTH LOGIN', '334|503');
00695 if ($authRequired == '334') {
00696 if (!$this->__smtpSend(base64_encode($this->smtpOptions['username']), '334')) {
00697 return false;
00698 }
00699 if (!$this->__smtpSend(base64_encode($this->smtpOptions['password']), '235')) {
00700 return false;
00701 }
00702 } elseif ($authRequired != '503') {
00703 return false;
00704 }
00705 }
00706
00707 if (!$this->__smtpSend('MAIL FROM: ' . $this->__formatAddress($this->from, true))) {
00708 return false;
00709 }
00710
00711 if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($this->to, true))) {
00712 return false;
00713 }
00714
00715 foreach ($this->cc as $cc) {
00716 if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($cc, true))) {
00717 return false;
00718 }
00719 }
00720 foreach ($this->bcc as $bcc) {
00721 if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($bcc, true))) {
00722 return false;
00723 }
00724 }
00725
00726 if (!$this->__smtpSend('DATA', '354')) {
00727 return false;
00728 }
00729
00730 $header = implode("\r\n", $this->__header);
00731 $message = implode("\r\n", $this->__message);
00732 if (!$this->__smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) {
00733 return false;
00734 }
00735 $this->__smtpSend('QUIT', false);
00736
00737 $this->__smtpConnection->disconnect();
00738 return true;
00739 }
00740
00741
00742
00743
00744
00745
00746
00747
00748 function __smtpSend($data, $checkCode = '250') {
00749 if (!is_null($data)) {
00750 $this->__smtpConnection->write($data . "\r\n");
00751 }
00752 if ($checkCode !== false) {
00753 $response = $this->__smtpConnection->read();
00754
00755 if (preg_match('/^(' . $checkCode . ')/', $response, $code)) {
00756 return $code[0];
00757 }
00758 $this->smtpError = $response;
00759 return false;
00760 }
00761 return true;
00762 }
00763
00764
00765
00766
00767
00768
00769 function __debug() {
00770 $nl = "\n";
00771 $header = implode($nl, $this->__header);
00772 $message = implode($nl, $this->__message);
00773 $fm = '<pre>';
00774
00775 if ($this->delivery == 'smtp') {
00776 $fm .= sprintf('%s %s%s', 'Host:', $this->smtpOptions['host'], $nl);
00777 $fm .= sprintf('%s %s%s', 'Port:', $this->smtpOptions['port'], $nl);
00778 $fm .= sprintf('%s %s%s', 'Timeout:', $this->smtpOptions['timeout'], $nl);
00779 }
00780 $fm .= sprintf('%s %s%s', 'To:', $this->to, $nl);
00781 $fm .= sprintf('%s %s%s', 'From:', $this->from, $nl);
00782 $fm .= sprintf('%s %s%s', 'Subject:', $this->__encode($this->subject), $nl);
00783 $fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);
00784 $fm .= sprintf('%s%3$s%3$s%s', 'Parameters:', $this->additionalParams, $nl);
00785 $fm .= sprintf('%s%3$s%3$s%s', 'Message:', $message, $nl);
00786 $fm .= '</pre>';
00787
00788 $this->Controller->Session->setFlash($fm, 'default', null, 'email');
00789 return true;
00790 }
00791
00792 }
00793 ?>