CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  3: /**
  4:  * Short description for file.
  5:  *
  6:  * Long description for file
  7:  *
  8:  * PHP versions 4 and 5
  9:  *
 10:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 11:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 12:  *
 13:  * Licensed under The MIT License
 14:  * Redistributions of files must retain the above copyright notice.
 15:  *
 16:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 17:  * @link          http://cakephp.org CakePHP(tm) Project
 18:  * @package       cake
 19:  * @subpackage    cake.cake.libs.controller.components
 20:  * @since         CakePHP(tm) v 1.2.0.3467
 21:  * @version       $Revision$
 22:  * @modifiedby    $LastChangedBy$
 23:  * @lastmodified  $Date$
 24:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 25:  */
 26: /**
 27:  * EmailComponent
 28:  *
 29:  * This component is used for handling Internet Message Format based
 30:  * based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
 31:  *
 32:  * @package       cake
 33:  * @subpackage    cake.cake.libs.controller.components
 34:  *
 35:  */
 36: App::import('Core', 'Multibyte');
 37: class EmailComponent extends Object{
 38: /**
 39:  * Recipient of the email
 40:  *
 41:  * @var string
 42:  * @access public
 43:  */
 44:     var $to = null;
 45: /**
 46:  * The mail which the email is sent from
 47:  *
 48:  * @var string
 49:  * @access public
 50:  */
 51:     var $from = null;
 52: /**
 53:  * The email the recipient will reply to
 54:  *
 55:  * @var string
 56:  * @access public
 57:  */
 58:     var $replyTo = null;
 59: /**
 60:  * The read receipt email
 61:  *
 62:  * @var string
 63:  * @access public
 64:  */
 65:     var $readReceipt = null;
 66: /**
 67:  * The mail that will be used in case of any errors like
 68:  * - Remote mailserver down
 69:  * - Remote user has exceeded his quota
 70:  * - Unknown user
 71:  *
 72:  * @var string
 73:  * @access public
 74:  */
 75:     var $return = null;
 76: /**
 77:  * Carbon Copy
 78:  *
 79:  * List of email's that should receive a copy of the email.
 80:  * The Recipient WILL be able to see this list
 81:  *
 82:  * @var array
 83:  * @access public
 84:  */
 85:     var $cc = array();
 86: /**
 87:  * Blind Carbon Copy
 88:  *
 89:  * List of email's that should receive a copy of the email.
 90:  * The Recipient WILL NOT be able to see this list
 91:  *
 92:  * @var array
 93:  * @access public
 94:  */
 95:     var $bcc = array();
 96: /**
 97:  * The date to put in the Date: header.  This should be a date
 98:  * conformant with the RFC2822 standard.  Leave null, to have
 99:  * today's date generated.
100:  *
101:  * @var string
102:  */
103:     var $date = null;
104: 
105: /**
106:  * The subject of the email
107:  *
108:  * @var string
109:  * @access public
110:  */
111:     var $subject = null;
112: /**
113:  * Associative array of a user defined headers
114:  * Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
115:  *
116:  * @var array
117:  * @access public
118:  */
119:     var $headers = array();
120: /**
121:  * List of additional headers
122:  *
123:  * These will NOT be used if you are using safemode and mail()
124:  *
125:  * @var string
126:  * @access public
127:  */
128:     var $additionalParams = null;
129: /**
130:  * Layout for the View
131:  *
132:  * @var string
133:  * @access public
134:  */
135:     var $layout = 'default';
136: /**
137:  * Template for the view
138:  *
139:  * @var string
140:  * @access public
141:  */
142:     var $template = null;
143: /**
144:  * as per RFC2822 Section 2.1.1
145:  *
146:  * @var integer
147:  * @access public
148:  */
149:     var $lineLength = 70;
150: 
151: /**
152:  * Line feed character(s) to be used when sending using mail() function
153:  * If null PHP_EOL is used.
154:  * RFC2822 requires it to be CRLF but some Unix
155:  * mail transfer agents replace LF by CRLF automatically
156:  * (which leads to doubling CR if CRLF is used).
157:  *
158:  * @var string
159:  * @access public
160:  */
161:     var $lineFeed = null;
162: 
163: /**
164:  * @deprecated see lineLength
165:  */
166:     var $_lineLength = null;
167: /**
168:  * What format should the email be sent in
169:  *
170:  * Supported formats:
171:  * - text
172:  * - html
173:  * - both
174:  *
175:  * @var string
176:  * @access public
177:  */
178:     var $sendAs = 'text';
179: /**
180:  * What method should the email be sent by
181:  *
182:  * Supported methods:
183:  * - mail
184:  * - smtp
185:  * - debug
186:  *
187:  * @var string
188:  * @access public
189:  */
190:     var $delivery = 'mail';
191: /**
192:  * charset the email is sent in
193:  *
194:  * @var string
195:  * @access public
196:  */
197:     var $charset = 'utf-8';
198: /**
199:  * List of files that should be attached to the email.
200:  *
201:  * Can be both absolute and relative paths
202:  *
203:  * @var array
204:  * @access public
205:  */
206:     var $attachments = array();
207: /**
208:  * What mailer should EmailComponent identify itself as
209:  *
210:  * @var string
211:  * @access public
212:  */
213:     var $xMailer = 'CakePHP Email Component';
214: /**
215:  * The list of paths to search if an attachment isnt absolute
216:  *
217:  * @var array
218:  * @access public
219:  */
220:     var $filePaths = array();
221: /**
222:  * List of options to use for smtp mail method
223:  *
224:  * Options is:
225:  * - port
226:  * - host
227:  * - timeout
228:  * - username
229:  * - password
230:  * - client
231:  *
232:  * @var array
233:  * @access public
234:  */
235:     var $smtpOptions = array(
236:         'port'=> 25, 'host' => 'localhost', 'timeout' => 30
237:     );
238: /**
239:  * Placeholder for any errors that might happen with the
240:  * smtp mail methods
241:  *
242:  * @var string
243:  * @access public
244:  */
245:     var $smtpError = null;
246: /**
247:  * If set to true, the mail method will be auto-set to 'debug'
248:  *
249:  * @var string
250:  * @access protected
251:  */
252:     var $_debug = false;
253: /**
254:  * Temporary store of message header lines
255:  *
256:  * @var array
257:  * @access private
258:  */
259:     var $__header = array();
260: /**
261:  * If set, boundary to use for multipart mime messages
262:  *
263:  * @var string
264:  * @access private
265:  */
266:     var $__boundary = null;
267: /**
268:  * Temporary store of message lines
269:  *
270:  * @var array
271:  * @access private
272:  */
273:     var $__message = array();
274: /**
275:  * Variable that holds SMTP connection
276:  *
277:  * @var resource
278:  * @access private
279:  */
280:     var $__smtpConnection = null;
281: /**
282:  * Initialize component
283:  *
284:  * @param object $controller Instantiating controller
285:  * @access public
286:  */
287:     function initialize(&$controller, $settings = array()) {
288:         $this->Controller =& $controller;
289:         if (Configure::read('App.encoding') !== null) {
290:             $this->charset = Configure::read('App.encoding');
291:         }
292:         $this->_set($settings);
293:     }
294: /**
295:  * Startup component
296:  *
297:  * @param object $controller Instantiating controller
298:  * @access public
299:  */
300:     function startup(&$controller) {}
301: /**
302:  * Send an email using the specified content, template and layout
303:  *
304:  * @param mixed $content Either an array of text lines, or a string with contents
305:  * @param string $template Template to use when sending email
306:  * @param string $layout Layout to use to enclose email body
307:  * @return boolean Success
308:  * @access public
309:  */
310:     function send($content = null, $template = null, $layout = null) {
311:         $this->__createHeader();
312: 
313:         if ($template) {
314:             $this->template = $template;
315:         }
316: 
317:         if ($layout) {
318:             $this->layout = $layout;
319:         }
320: 
321:         if (is_array($content)) {
322:             $content = implode("\n", $content) . "\n";
323:         }
324: 
325:         $message = $this->__wrap($content);
326:         if ($this->template === null) {
327:             $message = $this->__formatMessage($message);
328:         } else {
329:             $message = $this->__renderTemplate($message);
330:         }
331:         $message[] = '';
332:         $this->__message = $message;
333: 
334:         if (!empty($this->attachments)) {
335:             $this->__attachFiles();
336:         }
337: 
338:         if (!empty($this->attachments)) {
339:             $this->__message[] = '';
340:             $this->__message[] = '--' . $this->__boundary . '--';
341:             $this->__message[] = '';
342:         }
343: 
344:         if ($this->_debug) {
345:             return $this->__debug();
346:         }
347:         $__method = '__' . $this->delivery;
348:         $sent = $this->$__method();
349: 
350:         $this->__header = array();
351:         $this->__message = array();
352: 
353:         return $sent;
354:     }
355: /**
356:  * Reset all EmailComponent internal variables to be able to send out a new email.
357:  *
358:  * @access public
359:  */
360:     function reset() {
361:         $this->template = null;
362:         $this->to = null;
363:         $this->from = null;
364:         $this->replyTo = null;
365:         $this->return = null;
366:         $this->cc = array();
367:         $this->bcc = array();
368:         $this->subject = null;
369:         $this->additionalParams = null;
370:         $this->date = null;
371:         $this->smtpError = null;
372:         $this->attachments = array();
373:         $this->__header = array();
374:         $this->__boundary = null;
375:         $this->__message = array();
376:     }
377: /**
378:  * Render the contents using the current layout and template.
379:  *
380:  * @param string $content Content to render
381:  * @return array Email ready to be sent
382:  * @access private
383:  */
384:     function __renderTemplate($content) {
385:         $viewClass = $this->Controller->view;
386: 
387:         if ($viewClass != 'View') {
388:             if (strpos($viewClass, '.') !== false) {
389:                 list($plugin, $viewClass) = explode('.', $viewClass);
390:             }
391:             $viewClass = $viewClass . 'View';
392:             App::import('View', $this->Controller->view);
393:         }
394:         $View = new $viewClass($this->Controller);
395:         $View->layout = $this->layout;
396:         $msg = array();
397: 
398:         $content = implode("\n", $content);
399: 
400:         if ($this->sendAs === 'both') {
401:             $htmlContent = $content;
402:             if (!empty($this->attachments)) {
403:                 $msg[] = '--' . $this->__boundary;
404:                 $msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
405:                 $msg[] = '';
406:             }
407:             $msg[] = '--alt-' . $this->__boundary;
408:             $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
409:             $msg[] = 'Content-Transfer-Encoding: 7bit';
410:             $msg[] = '';
411: 
412:             $content = $View->element('email' . DS . 'text' . DS . $this->template, array('content' => $content), true);
413:             $View->layoutPath = 'email' . DS . 'text';
414:             $content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
415:             $msg = array_merge($msg, $content);
416: 
417:             $msg[] = '';
418:             $msg[] = '--alt-' . $this->__boundary;
419:             $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
420:             $msg[] = 'Content-Transfer-Encoding: 7bit';
421:             $msg[] = '';
422: 
423:             $htmlContent = $View->element('email' . DS . 'html' . DS . $this->template, array('content' => $htmlContent), true);
424:             $View->layoutPath = 'email' . DS . 'html';
425:             $htmlContent = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
426:             $msg = array_merge($msg, $htmlContent);
427: 
428:             $msg[] = '';
429:             $msg[] = '--alt-' . $this->__boundary . '--';
430:             $msg[] = '';
431: 
432:             ClassRegistry::removeObject('view');
433:             return $msg;
434:         }
435: 
436:         if (!empty($this->attachments)) {
437:             if ($this->sendAs === 'html') {
438:                 $msg[] = '';
439:                 $msg[] = '--' . $this->__boundary;
440:                 $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
441:                 $msg[] = 'Content-Transfer-Encoding: 7bit';
442:                 $msg[] = '';
443:             } else {
444:                 $msg[] = '--' . $this->__boundary;
445:                 $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
446:                 $msg[] = 'Content-Transfer-Encoding: 7bit';
447:                 $msg[] = '';
448:             }
449:         }
450: 
451:         $content = $View->element('email' . DS . $this->sendAs . DS . $this->template, array('content' => $content), true);
452:         $View->layoutPath = 'email' . DS . $this->sendAs;
453:         $content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
454:         $msg = array_merge($msg, $content);
455:         ClassRegistry::removeObject('view');
456: 
457:         return $msg;
458:     }
459: /**
460:  * Create unique boundary identifier
461:  *
462:  * @access private
463:  */
464:     function __createBoundary() {
465:         $this->__boundary = md5(uniqid(time()));
466:     }
467: /**
468:  * Create emails headers including (but not limited to) from email address, reply to,
469:  * bcc and cc.
470:  *
471:  * @access private
472:  */
473:     function __createHeader() {
474:         if ($this->delivery == 'smtp') {
475:             $this->__header[] = 'To: ' . $this->__formatAddress($this->to);
476:         }
477:         $this->__header[] = 'From: ' . $this->__formatAddress($this->from);
478: 
479:         if (!empty($this->replyTo)) {
480:             $this->__header[] = 'Reply-To: ' . $this->__formatAddress($this->replyTo);
481:         }
482:         if (!empty($this->return)) {
483:             $this->__header[] = 'Return-Path: ' . $this->__formatAddress($this->return);
484:         }
485:         if (!empty($this->readReceipt)) {
486:             $this->__header[] = 'Disposition-Notification-To: ' . $this->__formatAddress($this->readReceipt);
487:         }
488: 
489:         if (!empty($this->cc)) {
490:             $this->__header[] = 'cc: ' .implode(', ', array_map(array($this, '__formatAddress'), $this->cc));
491:         }
492: 
493:         if (!empty($this->bcc) && $this->delivery != 'smtp') {
494:             $this->__header[] = 'Bcc: ' .implode(', ', array_map(array($this, '__formatAddress'), $this->bcc));
495:         }
496:         if ($this->delivery == 'smtp') {
497:             $this->__header[] = 'Subject: ' . $this->__encode($this->subject);
498:         }
499: 
500:         $date = $this->date;
501:         if ($date == false) {
502:             $date = date(DATE_RFC2822);
503:         }
504:         $this->__header[] = 'Date: ' . $date;
505: 
506:         $this->__header[] = 'X-Mailer: ' . $this->xMailer;
507: 
508:         if (!empty($this->headers)) {
509:             foreach ($this->headers as $key => $val) {
510:                 $this->__header[] = 'X-' . $key . ': ' . $val;
511:             }
512:         }
513: 
514:         if (!empty($this->attachments) || $this->sendAs === 'both') {
515:             $this->__createBoundary();
516:         }
517: 
518:         if (!empty($this->attachments)) {
519:             $this->__header[] = 'MIME-Version: 1.0';
520:             $this->__header[] = 'Content-Type: multipart/mixed; boundary="' . $this->__boundary . '"';
521:             $this->__header[] = 'This part of the E-mail should never be seen. If';
522:             $this->__header[] = 'you are reading this, consider upgrading your e-mail';
523:             $this->__header[] = 'client to a MIME-compatible client.';
524:         } elseif ($this->sendAs === 'text') {
525:             $this->__header[] = 'Content-Type: text/plain; charset=' . $this->charset;
526:         } elseif ($this->sendAs === 'html') {
527:             $this->__header[] = 'Content-Type: text/html; charset=' . $this->charset;
528:         } elseif ($this->sendAs === 'both') {
529:             $this->__header[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
530:         }
531: 
532:         $this->__header[] = 'Content-Transfer-Encoding: 7bit';
533:     }
534: /**
535:  * Format the message by seeing if it has attachments.
536:  *
537:  * @param string $message Message to format
538:  * @access private
539:  */
540:     function __formatMessage($message) {
541:         if (!empty($this->attachments)) {
542:             $prefix = array('--' . $this->__boundary);
543:             if ($this->sendAs === 'text') {
544:                 $prefix[] = 'Content-Type: text/plain; charset=' . $this->charset;
545:             } elseif ($this->sendAs === 'html') {
546:                 $prefix[] = 'Content-Type: text/html; charset=' . $this->charset;
547:             } elseif ($this->sendAs === 'both') {
548:                 $prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
549:             }
550:             $prefix[] = 'Content-Transfer-Encoding: 7bit';
551:             $prefix[] = '';
552:             $message = array_merge($prefix, $message);
553:         }
554:         return $message;
555:     }
556: /**
557:  * Attach files by adding file contents inside boundaries.
558:  *
559:  * @access private
560:  * @TODO: modify to use the core File class?
561:  */
562:     function __attachFiles() {
563:         $files = array();
564:         foreach ($this->attachments as $attachment) {
565:             $file = $this->__findFiles($attachment);
566:             if (!empty($file)) {
567:                 $files[] = $file;
568:             }
569:         }
570: 
571:         foreach ($files as $file) {
572:             $handle = fopen($file, 'rb');
573:             $data = fread($handle, filesize($file));
574:             $data = chunk_split(base64_encode($data)) ;
575:             fclose($handle);
576: 
577:             $this->__message[] = '--' . $this->__boundary;
578:             $this->__message[] = 'Content-Type: application/octet-stream';
579:             $this->__message[] = 'Content-Transfer-Encoding: base64';
580:             $this->__message[] = 'Content-Disposition: attachment; filename="' . basename($file) . '"';
581:             $this->__message[] = '';
582:             $this->__message[] = $data;
583:             $this->__message[] = '';
584:         }
585:     }
586: /**
587:  * Find the specified attachment in the list of file paths
588:  *
589:  * @param string $attachment Attachment file name to find
590:  * @return string Path to located file
591:  * @access private
592:  */
593:     function __findFiles($attachment) {
594:         if (file_exists($attachment)) {
595:             return $attachment;
596:         }
597:         foreach ($this->filePaths as $path) {
598:             if (file_exists($path . DS . $attachment)) {
599:                 $file = $path . DS . $attachment;
600:                 return $file;
601:             }
602:         }
603:         return null;
604:     }
605: /**
606:  * Wrap the message using EmailComponent::$lineLength
607:  *
608:  * @param string $message Message to wrap
609:  * @return array Wrapped message
610:  * @access private
611:  */
612:     function __wrap($message) {
613:         $message = $this->__strip($message, true);
614:         $message = str_replace(array("\r\n","\r"), "\n", $message);
615:         $lines = explode("\n", $message);
616:         $formatted = array();
617: 
618:         if ($this->_lineLength !== null) {
619:             trigger_error('_lineLength cannot be accessed please use lineLength', E_USER_WARNING);
620:             $this->lineLength = $this->_lineLength;
621:         }
622: 
623:         foreach ($lines as $line) {
624:             if (substr($line, 0, 1) == '.') {
625:                 $line = '.' . $line;
626:             }
627:             $formatted = array_merge($formatted, explode("\n", wordwrap($line, $this->lineLength, "\n", true)));
628:         }
629:         $formatted[] = '';
630:         return $formatted;
631:     }
632: /**
633:  * Encode the specified string using the current charset
634:  *
635:  * @param string $subject String to encode
636:  * @return string Encoded string
637:  * @access private
638:  */
639:     function __encode($subject) {
640:         $subject = $this->__strip($subject);
641: 
642:         $nl = "\r\n";
643:         if ($this->delivery == 'mail') {
644:             $nl = '';
645:         }
646:         return mb_encode_mimeheader($subject, $this->charset, 'B', $nl);
647:     }
648: /**
649:  * Format a string as an email address
650:  *
651:  * @param string $string String representing an email address
652:  * @return string Email address suitable for email headers or smtp pipe
653:  * @access private
654:  */
655:     function __formatAddress($string, $smtp = false) {
656:         $hasAlias = preg_match('/((.*))?\s?<(.+)>/', $string, $matches);
657:         if ($smtp && $hasAlias) {
658:             return $this->__strip('<' .  $matches[3] . '>');
659:         } elseif ($smtp) {
660:             return $this->__strip('<' . $string . '>');
661:         }
662: 
663:         if ($hasAlias && !empty($matches[2])) {
664:             return $this->__encode($matches[2]) . $this->__strip(' <' . $matches[3] . '>');
665:         }
666:         return $this->__strip($string);
667:     }
668: /**
669:  * Remove certain elements (such as bcc:, to:, %0a) from given value.
670:  * Helps prevent header injection / mainipulation on user content.
671:  *
672:  * @param string $value Value to strip
673:  * @param boolean $message Set to true to indicate main message content
674:  * @return string Stripped value
675:  * @access private
676:  */
677:     function __strip($value, $message = false) {
678:         $search  = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
679:         $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
680: 
681:         if ($message !== true) {
682:             $search .= '|\r|\n';
683:         }
684:         $search = '#(?:' . $search . ')#i';
685:         while (preg_match($search, $value)) {
686:             $value = preg_replace($search, '', $value);
687:         }
688:         return $value;
689:     }
690: /**
691:  * Wrapper for PHP mail function used for sending out emails
692:  *
693:  * @return bool Success
694:  * @access private
695:  */
696:     function __mail() {
697:         if ($this->lineFeed === null) {
698:             $lineFeed = PHP_EOL;
699:         } else {
700:             $lineFeed = $this->lineFeed;
701:         }
702:         $header = implode($lineFeed, $this->__header);
703:         $message = implode($lineFeed, $this->__message);
704:         if (ini_get('safe_mode')) {
705:             return @mail($this->to, $this->__encode($this->subject), $message, $header);
706:         }
707:         return @mail($this->to, $this->__encode($this->subject), $message, $header, $this->additionalParams);
708:     }
709: /**
710:  * Helper method to get socket, overridden in tests
711:  *
712:  * @param array $config Config data for the socket.
713:  * @return void
714:  * @access protected
715:  */
716:     function _getSocket($config) {
717:         $this->__smtpConnection =& new CakeSocket($config);
718:     }
719: /**
720:  * Sends out email via SMTP
721:  *
722:  * @return bool Success
723:  * @access private
724:  */
725:     function __smtp() {
726:         App::import('Core', array('Socket'));
727: 
728:         $defaults = array(
729:             'host' => 'localhost',
730:             'port' => 25,
731:             'protocol' => 'smtp',
732:             'timeout' => 30
733:         );
734:         $this->smtpOptions = array_merge($defaults, $this->smtpOptions);
735:         $this->_getSocket($this->smtpOptions);
736: 
737:         if (!$this->__smtpConnection->connect()) {
738:             $this->smtpError = $this->__smtpConnection->lastError();
739:             return false;
740:         } elseif (!$this->__smtpSend(null, '220')) {
741:             return false;
742:         }
743: 
744:         $httpHost = env('HTTP_HOST');
745: 
746:         if (isset($this->smtpOptions['client'])) {
747:             $host = $this->smtpOptions['client'];
748:         } elseif (!empty($httpHost)) {
749:             list($host) = explode(':', $httpHost);
750:         } else {
751:             $host = 'localhost';
752:         }
753: 
754:         if (!$this->__smtpSend("HELO {$host}", '250')) {
755:             return false;
756:         }
757: 
758:         if (isset($this->smtpOptions['username']) && isset($this->smtpOptions['password'])) {
759:             $authRequired = $this->__smtpSend('AUTH LOGIN', '334|503');
760:             if ($authRequired == '334') {
761:                 if (!$this->__smtpSend(base64_encode($this->smtpOptions['username']), '334')) {
762:                     return false;
763:                 }
764:                 if (!$this->__smtpSend(base64_encode($this->smtpOptions['password']), '235')) {
765:                     return false;
766:                 }
767:             } elseif ($authRequired != '503') {
768:                 return false;
769:             }
770:         }
771: 
772:         if (!$this->__smtpSend('MAIL FROM: ' . $this->__formatAddress($this->from, true))) {
773:             return false;
774:         }
775: 
776:         if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($this->to, true))) {
777:             return false;
778:         }
779: 
780:         foreach ($this->cc as $cc) {
781:             if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($cc, true))) {
782:                 return false;
783:             }
784:         }
785:         foreach ($this->bcc as $bcc) {
786:             if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($bcc, true))) {
787:                 return false;
788:             }
789:         }
790: 
791:         if (!$this->__smtpSend('DATA', '354')) {
792:             return false;
793:         }
794: 
795:         $header = implode("\r\n", $this->__header);
796:         $message = implode("\r\n", $this->__message);
797:         if (!$this->__smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) {
798:             return false;
799:         }
800:         $this->__smtpSend('QUIT', false);
801: 
802:         $this->__smtpConnection->disconnect();
803:         return true;
804:     }
805: /**
806:  * Private method for sending data to SMTP connection
807:  *
808:  * @param string $data data to be sent to SMTP server
809:  * @param mixed $checkCode code to check for in server response, false to skip
810:  * @return bool Success
811:  * @access private
812:  */
813:     function __smtpSend($data, $checkCode = '250') {
814:         if (!is_null($data)) {
815:             $this->__smtpConnection->write($data . "\r\n");
816:         }
817:         if ($checkCode !== false) {
818:             $response = $this->__smtpConnection->read();
819: 
820:             if (preg_match('/^(' . $checkCode . ')/', $response, $code)) {
821:                 return $code[0];
822:             }
823:             $this->smtpError = $response;
824:             return false;
825:         }
826:         return true;
827:     }
828: /**
829:  * Set as controller flash message a debug message showing current settings in component
830:  *
831:  * @return boolean Success
832:  * @access private
833:  */
834:     function __debug() {
835:         $nl = "\n";
836:         $header = implode($nl, $this->__header);
837:         $message = implode($nl, $this->__message);
838:         $fm = '<pre>';
839: 
840:         if ($this->delivery == 'smtp') {
841:             $fm .= sprintf('%s %s%s', 'Host:', $this->smtpOptions['host'], $nl);
842:             $fm .= sprintf('%s %s%s', 'Port:', $this->smtpOptions['port'], $nl);
843:             $fm .= sprintf('%s %s%s', 'Timeout:', $this->smtpOptions['timeout'], $nl);
844:         }
845:         $fm .= sprintf('%s %s%s', 'To:', $this->to, $nl);
846:         $fm .= sprintf('%s %s%s', 'From:', $this->from, $nl);
847:         $fm .= sprintf('%s %s%s', 'Subject:', $this->__encode($this->subject), $nl);
848:         $fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);
849:         $fm .= sprintf('%s%3$s%3$s%s', 'Parameters:', $this->additionalParams, $nl);
850:         $fm .= sprintf('%s%3$s%3$s%s', 'Message:', $message, $nl);
851:         $fm .= '</pre>';
852: 
853:         $this->Controller->Session->setFlash($fm, 'default', null, 'email');
854:         return true;
855:     }
856: 
857: }
858: ?>
859: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs