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