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 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AclComponent
  • AuthComponent
  • CookieComponent
  • DbAcl
  • EmailComponent
  • IniAcl
  • PaginatorComponent
  • RequestHandlerComponent
  • SecurityComponent
  • SessionComponent

Interfaces

  • AclInterface
  1: <?php
  2: /**
  3:  * Email Component
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       Cake.Controller.Component
 16:  * @since         CakePHP(tm) v 1.2.0.3467
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: App::uses('Component', 'Controller');
 21: App::uses('Multibyte', 'I18n');
 22: App::uses('CakeEmail', 'Network/Email');
 23: 
 24: /**
 25:  * EmailComponent
 26:  *
 27:  * This component is used for handling Internet Message Format based
 28:  * based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
 29:  *
 30:  * @package       Cake.Controller.Component
 31:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/email.html
 32:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
 33:  * @deprecated Use Network/CakeEmail
 34:  */
 35: class EmailComponent extends Component {
 36: 
 37: /**
 38:  * Recipient of the email
 39:  *
 40:  * @var string
 41:  */
 42:     public $to = null;
 43: 
 44: /**
 45:  * The mail which the email is sent from
 46:  *
 47:  * @var string
 48:  */
 49:     public $from = null;
 50: 
 51: /**
 52:  * The email the recipient will reply to
 53:  *
 54:  * @var string
 55:  */
 56:     public $replyTo = null;
 57: 
 58: /**
 59:  * The read receipt email
 60:  *
 61:  * @var string
 62:  */
 63:     public $readReceipt = null;
 64: 
 65: /**
 66:  * The mail that will be used in case of any errors like
 67:  * - Remote mailserver down
 68:  * - Remote user has exceeded his quota
 69:  * - Unknown user
 70:  *
 71:  * @var string
 72:  */
 73:     public $return = null;
 74: 
 75: /**
 76:  * Carbon Copy
 77:  *
 78:  * List of email's that should receive a copy of the email.
 79:  * The Recipient WILL be able to see this list
 80:  *
 81:  * @var array
 82:  */
 83:     public $cc = array();
 84: 
 85: /**
 86:  * Blind Carbon Copy
 87:  *
 88:  * List of email's that should receive a copy of the email.
 89:  * The Recipient WILL NOT be able to see this list
 90:  *
 91:  * @var array
 92:  */
 93:     public $bcc = array();
 94: 
 95: /**
 96:  * The date to put in the Date: header. This should be a date
 97:  * conforming with the RFC2822 standard. Leave null, to have
 98:  * today's date generated.
 99:  *
100:  * @var string
101:  */
102:     public $date = null;
103: 
104: /**
105:  * The subject of the email
106:  *
107:  * @var string
108:  */
109:     public $subject = null;
110: 
111: /**
112:  * Associative array of a user defined headers
113:  * Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
114:  *
115:  * @var array
116:  */
117:     public $headers = array();
118: 
119: /**
120:  * List of additional headers
121:  *
122:  * These will NOT be used if you are using safemode and mail()
123:  *
124:  * @var string
125:  */
126:     public $additionalParams = null;
127: 
128: /**
129:  * Layout for the View
130:  *
131:  * @var string
132:  */
133:     public $layout = 'default';
134: 
135: /**
136:  * Template for the view
137:  *
138:  * @var string
139:  */
140:     public $template = null;
141: 
142: /**
143:  * Line feed character(s) to be used when sending using mail() function
144:  * By default PHP_EOL is used.
145:  * RFC2822 requires it to be CRLF but some Unix
146:  * mail transfer agents replace LF by CRLF automatically
147:  * (which leads to doubling CR if CRLF is used).
148:  *
149:  * @var string
150:  */
151:     public $lineFeed = PHP_EOL;
152: 
153: /**
154:  * What format should the email be sent in
155:  *
156:  * Supported formats:
157:  * - text
158:  * - html
159:  * - both
160:  *
161:  * @var string
162:  */
163:     public $sendAs = 'text';
164: 
165: /**
166:  * What method should the email be sent by
167:  *
168:  * Supported methods:
169:  * - mail
170:  * - smtp
171:  * - debug
172:  *
173:  * @var string
174:  */
175:     public $delivery = 'mail';
176: 
177: /**
178:  * charset the email is sent in
179:  *
180:  * @var string
181:  */
182:     public $charset = 'utf-8';
183: 
184: /**
185:  * List of files that should be attached to the email.
186:  *
187:  * Can be both absolute and relative paths
188:  *
189:  * @var array
190:  */
191:     public $attachments = array();
192: 
193: /**
194:  * What mailer should EmailComponent identify itself as
195:  *
196:  * @var string
197:  */
198:     public $xMailer = 'CakePHP Email Component';
199: 
200: /**
201:  * The list of paths to search if an attachment isn't absolute
202:  *
203:  * @var array
204:  */
205:     public $filePaths = array();
206: 
207: /**
208:  * List of options to use for smtp mail method
209:  *
210:  * Options is:
211:  * - port
212:  * - host
213:  * - timeout
214:  * - username
215:  * - password
216:  * - client
217:  *
218:  * @var array
219:  */
220:     public $smtpOptions = array();
221: 
222: /**
223:  * Contains the rendered plain text message if one was sent.
224:  *
225:  * @var string
226:  */
227:     public $textMessage = null;
228: 
229: /**
230:  * Contains the rendered HTML message if one was sent.
231:  *
232:  * @var string
233:  */
234:     public $htmlMessage = null;
235: 
236: /**
237:  * Whether to generate a Message-ID header for the
238:  * e-mail. True to generate a Message-ID, False to let
239:  * it be handled by sendmail (or similar) or a string
240:  * to completely override the Message-ID.
241:  *
242:  * If you are sending Email from a shell, be sure to set this value.  As you
243:  * could encounter delivery issues if you do not.
244:  *
245:  * @var mixed
246:  */
247:     public $messageId = true;
248: 
249: /**
250:  * Controller reference
251:  *
252:  * @var Controller
253:  */
254:     protected $_controller = null;
255: 
256: /**
257:  * Constructor
258:  *
259:  * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
260:  * @param array $settings Array of configuration settings.
261:  */
262:     public function __construct(ComponentCollection $collection, $settings = array()) {
263:         $this->_controller = $collection->getController();
264:         parent::__construct($collection, $settings);
265:     }
266: 
267: /**
268:  * Initialize component
269:  *
270:  * @param Controller $controller Instantiating controller
271:  * @return void
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:  * Send an email using the specified content, template and layout
281:  *
282:  * @param mixed $content Either an array of text lines, or a string with contents
283:  *  If you are rendering a template this variable will be sent to the templates as `$content`
284:  * @param string $template Template to use when sending email
285:  * @param string $layout Layout to use to enclose email body
286:  * @return boolean Success
287:  */
288:     public function send($content = null, $template = null, $layout = null) {
289:         $lib = new CakeEmail();
290:         $lib->charset = $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($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 != false) {
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:  * Reset all EmailComponent internal variables to be able to send out a new email.
364:  *
365:  * @return void
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:  * Format the attach array
387:  *
388:  * @return array
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:  * Find the specified attachment in the list of file paths
406:  *
407:  * @param string $attachment Attachment file name to find
408:  * @return string Path to located file
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:  * Format addresses to be an array with email as key and alias as value
425:  *
426:  * @param array $addresses
427:  * @return array
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:  * Remove certain elements (such as bcc:, to:, %0a) from given value.
444:  * Helps prevent header injection / manipulation on user content.
445:  *
446:  * @param string $value Value to strip
447:  * @param boolean $message Set to true to indicate main message content
448:  * @return string Stripped value
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: 
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