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.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 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
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AclComponent
  • AuthComponent
  • CookieComponent
  • EmailComponent
  • PaginatorComponent
  • RequestHandlerComponent
  • SecurityComponent
  • SessionComponent
  1: <?php
  2: /**
  3:  * Email Component
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * For full copyright and license information, please see the LICENSE.txt
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @package       Cake.Controller.Component
 17:  * @since         CakePHP(tm) v 1.2.0.3467
 18:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 19:  */
 20: 
 21: App::uses('Component', 'Controller');
 22: App::uses('Multibyte', 'I18n');
 23: App::uses('CakeEmail', 'Network/Email');
 24: 
 25: /**
 26:  * EmailComponent
 27:  *
 28:  * This component is used for handling Internet Message Format based
 29:  * based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
 30:  *
 31:  * @package       Cake.Controller.Component
 32:  * @link          http://book.cakephp.org/2.0/en/core-libraries/components/email.html
 33:  * @link          http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
 34:  * @deprecated    Will be removed in 3.0. Use Network/CakeEmail instead
 35:  */
 36: class EmailComponent extends Component {
 37: 
 38: /**
 39:  * Recipient of the email
 40:  *
 41:  * @var string
 42:  */
 43:     public $to = null;
 44: 
 45: /**
 46:  * The mail which the email is sent from
 47:  *
 48:  * @var string
 49:  */
 50:     public $from = null;
 51: 
 52: /**
 53:  * The email the recipient will reply to
 54:  *
 55:  * @var string
 56:  */
 57:     public $replyTo = null;
 58: 
 59: /**
 60:  * The read receipt email
 61:  *
 62:  * @var string
 63:  */
 64:     public $readReceipt = null;
 65: 
 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:  */
 74:     public $return = null;
 75: 
 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:  */
 84:     public $cc = array();
 85: 
 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:  */
 94:     public $bcc = array();
 95: 
 96: /**
 97:  * The date to put in the Date: header. This should be a date
 98:  * conforming with the RFC2822 standard. Leave null, to have
 99:  * today's date generated.
100:  *
101:  * @var string
102:  */
103:     public $date = null;
104: 
105: /**
106:  * The subject of the email
107:  *
108:  * @var string
109:  */
110:     public $subject = null;
111: 
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:  */
118:     public $headers = array();
119: 
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:  */
127:     public $additionalParams = null;
128: 
129: /**
130:  * Layout for the View
131:  *
132:  * @var string
133:  */
134:     public $layout = 'default';
135: 
136: /**
137:  * Template for the view
138:  *
139:  * @var string
140:  */
141:     public $template = null;
142: 
143: /**
144:  * Line feed character(s) to be used when sending using mail() function
145:  * By default PHP_EOL is used.
146:  * RFC2822 requires it to be CRLF but some Unix
147:  * mail transfer agents replace LF by CRLF automatically
148:  * (which leads to doubling CR if CRLF is used).
149:  *
150:  * @var string
151:  */
152:     public $lineFeed = PHP_EOL;
153: 
154: /**
155:  * What format should the email be sent in
156:  *
157:  * Supported formats:
158:  * - text
159:  * - html
160:  * - both
161:  *
162:  * @var string
163:  */
164:     public $sendAs = 'text';
165: 
166: /**
167:  * What method should the email be sent by
168:  *
169:  * Supported methods:
170:  * - mail
171:  * - smtp
172:  * - debug
173:  *
174:  * @var string
175:  */
176:     public $delivery = 'mail';
177: 
178: /**
179:  * charset the email is sent in
180:  *
181:  * @var string
182:  */
183:     public $charset = 'utf-8';
184: 
185: /**
186:  * List of files that should be attached to the email.
187:  *
188:  * Can be both absolute and relative paths
189:  *
190:  * @var array
191:  */
192:     public $attachments = array();
193: 
194: /**
195:  * What mailer should EmailComponent identify itself as
196:  *
197:  * @var string
198:  */
199:     public $xMailer = 'CakePHP Email Component';
200: 
201: /**
202:  * The list of paths to search if an attachment isn't absolute
203:  *
204:  * @var array
205:  */
206:     public $filePaths = array();
207: 
208: /**
209:  * List of options to use for smtp mail method
210:  *
211:  * Options is:
212:  * - port
213:  * - host
214:  * - timeout
215:  * - username
216:  * - password
217:  * - client
218:  *
219:  * @var array
220:  */
221:     public $smtpOptions = array();
222: 
223: /**
224:  * Contains the rendered plain text message if one was sent.
225:  *
226:  * @var string
227:  */
228:     public $textMessage = null;
229: 
230: /**
231:  * Contains the rendered HTML message if one was sent.
232:  *
233:  * @var string
234:  */
235:     public $htmlMessage = null;
236: 
237: /**
238:  * Whether to generate a Message-ID header for the
239:  * e-mail. True to generate a Message-ID, False to let
240:  * it be handled by sendmail (or similar) or a string
241:  * to completely override the Message-ID.
242:  *
243:  * If you are sending Email from a shell, be sure to set this value. As you
244:  * could encounter delivery issues if you do not.
245:  *
246:  * @var mixed
247:  */
248:     public $messageId = true;
249: 
250: /**
251:  * Controller reference
252:  *
253:  * @var Controller
254:  */
255:     protected $_controller = null;
256: 
257: /**
258:  * Constructor
259:  *
260:  * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
261:  * @param array $settings Array of configuration settings.
262:  */
263:     public function __construct(ComponentCollection $collection, $settings = array()) {
264:         $this->_controller = $collection->getController();
265:         parent::__construct($collection, $settings);
266:     }
267: 
268: /**
269:  * Initialize component
270:  *
271:  * @param Controller $controller Instantiating controller
272:  * @return void
273:  */
274:     public function initialize(Controller $controller) {
275:         if (Configure::read('App.encoding') !== null) {
276:             $this->charset = Configure::read('App.encoding');
277:         }
278:     }
279: 
280: /**
281:  * Send an email using the specified content, template and layout
282:  *
283:  * @param string|array $content Either an array of text lines, or a string with contents
284:  *  If you are rendering a template this variable will be sent to the templates as `$content`
285:  * @param string $template Template to use when sending email
286:  * @param string $layout Layout to use to enclose email body
287:  * @return boolean Success
288:  */
289:     public function send($content = null, $template = null, $layout = null) {
290:         $lib = new CakeEmail();
291:         $lib->charset = $this->charset;
292:         $lib->headerCharset = $this->charset;
293: 
294:         $lib->from($this->_formatAddresses((array)$this->from));
295:         if (!empty($this->to)) {
296:             $lib->to($this->_formatAddresses((array)$this->to));
297:         }
298:         if (!empty($this->cc)) {
299:             $lib->cc($this->_formatAddresses((array)$this->cc));
300:         }
301:         if (!empty($this->bcc)) {
302:             $lib->bcc($this->_formatAddresses((array)$this->bcc));
303:         }
304:         if (!empty($this->replyTo)) {
305:             $lib->replyTo($this->_formatAddresses((array)$this->replyTo));
306:         }
307:         if (!empty($this->return)) {
308:             $lib->returnPath($this->_formatAddresses((array)$this->return));
309:         }
310:         if (!empty($this->readReceipt)) {
311:             $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
312:         }
313: 
314:         $lib->subject($this->subject)->messageID($this->messageId);
315:         $lib->helpers($this->_controller->helpers);
316: 
317:         $headers = array('X-Mailer' => $this->xMailer);
318:         foreach ($this->headers as $key => $value) {
319:             $headers['X-' . $key] = $value;
320:         }
321:         if ($this->date) {
322:             $headers['Date'] = $this->date;
323:         }
324:         $lib->setHeaders($headers);
325: 
326:         if ($template) {
327:             $this->template = $template;
328:         }
329:         if ($layout) {
330:             $this->layout = $layout;
331:         }
332:         $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
333: 
334:         if (!empty($this->attachments)) {
335:             $lib->attachments($this->_formatAttachFiles());
336:         }
337: 
338:         $lib->transport(ucfirst($this->delivery));
339:         if ($this->delivery === 'mail') {
340:             $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
341:         } elseif ($this->delivery === 'smtp') {
342:             $lib->config($this->smtpOptions);
343:         } else {
344:             $lib->config(array());
345:         }
346: 
347:         $sent = $lib->send($content);
348: 
349:         $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
350:         if (empty($this->htmlMessage)) {
351:             $this->htmlMessage = null;
352:         }
353:         $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
354:         if (empty($this->textMessage)) {
355:             $this->textMessage = null;
356:         }
357: 
358:         $this->_header = array();
359:         $this->_message = array();
360: 
361:         return $sent;
362:     }
363: 
364: /**
365:  * Reset all EmailComponent internal variables to be able to send out a new email.
366:  *
367:  * @return void
368:  */
369:     public function reset() {
370:         $this->template = null;
371:         $this->to = array();
372:         $this->from = null;
373:         $this->replyTo = null;
374:         $this->return = null;
375:         $this->cc = array();
376:         $this->bcc = array();
377:         $this->subject = null;
378:         $this->additionalParams = null;
379:         $this->date = null;
380:         $this->attachments = array();
381:         $this->htmlMessage = null;
382:         $this->textMessage = null;
383:         $this->messageId = true;
384:         $this->delivery = 'mail';
385:     }
386: 
387: /**
388:  * Format the attach array
389:  *
390:  * @return array
391:  */
392:     protected function _formatAttachFiles() {
393:         $files = array();
394:         foreach ($this->attachments as $filename => $attachment) {
395:             $file = $this->_findFiles($attachment);
396:             if (!empty($file)) {
397:                 if (is_int($filename)) {
398:                     $filename = basename($file);
399:                 }
400:                 $files[$filename] = $file;
401:             }
402:         }
403:         return $files;
404:     }
405: 
406: /**
407:  * Find the specified attachment in the list of file paths
408:  *
409:  * @param string $attachment Attachment file name to find
410:  * @return string Path to located file
411:  */
412:     protected function _findFiles($attachment) {
413:         if (file_exists($attachment)) {
414:             return $attachment;
415:         }
416:         foreach ($this->filePaths as $path) {
417:             if (file_exists($path . DS . $attachment)) {
418:                 $file = $path . DS . $attachment;
419:                 return $file;
420:             }
421:         }
422:         return null;
423:     }
424: 
425: /**
426:  * Format addresses to be an array with email as key and alias as value
427:  *
428:  * @param array $addresses
429:  * @return array
430:  */
431:     protected function _formatAddresses($addresses) {
432:         $formatted = array();
433:         foreach ($addresses as $address) {
434:             if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
435:                 $formatted[$this->_strip($matches[3])] = $matches[2];
436:             } else {
437:                 $address = $this->_strip($address);
438:                 $formatted[$address] = $address;
439:             }
440:         }
441:         return $formatted;
442:     }
443: 
444: /**
445:  * Remove certain elements (such as bcc:, to:, %0a) from given value.
446:  * Helps prevent header injection / manipulation on user content.
447:  *
448:  * @param string $value Value to strip
449:  * @param boolean $message Set to true to indicate main message content
450:  * @return string Stripped value
451:  */
452:     protected function _strip($value, $message = false) {
453:         $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
454:         $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
455: 
456:         if ($message !== true) {
457:             $search .= '|\r|\n';
458:         }
459:         $search = '#(?:' . $search . ')#i';
460:         while (preg_match($search, $value)) {
461:             $value = preg_replace($search, '', $value);
462:         }
463:         return $value;
464:     }
465: 
466: }
467: 
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