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

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

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 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 string|array $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:         $lib->headerCharset = $this->charset;
292: 
293:         $lib->from($this->_formatAddresses((array)$this->from));
294:         if (!empty($this->to)) {
295:             $lib->to($this->_formatAddresses((array)$this->to));
296:         }
297:         if (!empty($this->cc)) {
298:             $lib->cc($this->_formatAddresses((array)$this->cc));
299:         }
300:         if (!empty($this->bcc)) {
301:             $lib->bcc($this->_formatAddresses((array)$this->bcc));
302:         }
303:         if (!empty($this->replyTo)) {
304:             $lib->replyTo($this->_formatAddresses((array)$this->replyTo));
305:         }
306:         if (!empty($this->return)) {
307:             $lib->returnPath($this->_formatAddresses((array)$this->return));
308:         }
309:         if (!empty($this->readReceipt)) {
310:             $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
311:         }
312: 
313:         $lib->subject($this->subject)->messageID($this->messageId);
314:         $lib->helpers($this->_controller->helpers);
315: 
316:         $headers = array('X-Mailer' => $this->xMailer);
317:         foreach ($this->headers as $key => $value) {
318:             $headers['X-' . $key] = $value;
319:         }
320:         if ($this->date != false) {
321:             $headers['Date'] = $this->date;
322:         }
323:         $lib->setHeaders($headers);
324: 
325:         if ($template) {
326:             $this->template = $template;
327:         }
328:         if ($layout) {
329:             $this->layout = $layout;
330:         }
331:         $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
332: 
333:         if (!empty($this->attachments)) {
334:             $lib->attachments($this->_formatAttachFiles());
335:         }
336: 
337:         $lib->transport(ucfirst($this->delivery));
338:         if ($this->delivery === 'mail') {
339:             $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
340:         } elseif ($this->delivery === 'smtp') {
341:             $lib->config($this->smtpOptions);
342:         } else {
343:             $lib->config(array());
344:         }
345: 
346:         $sent = $lib->send($content);
347: 
348:         $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
349:         if (empty($this->htmlMessage)) {
350:             $this->htmlMessage = null;
351:         }
352:         $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
353:         if (empty($this->textMessage)) {
354:             $this->textMessage = null;
355:         }
356: 
357:         $this->_header = array();
358:         $this->_message = array();
359: 
360:         return $sent;
361:     }
362: 
363: /**
364:  * Reset all EmailComponent internal variables to be able to send out a new email.
365:  *
366:  * @return void
367:  */
368:     public function reset() {
369:         $this->template = null;
370:         $this->to = array();
371:         $this->from = null;
372:         $this->replyTo = null;
373:         $this->return = null;
374:         $this->cc = array();
375:         $this->bcc = array();
376:         $this->subject = null;
377:         $this->additionalParams = null;
378:         $this->date = null;
379:         $this->attachments = array();
380:         $this->htmlMessage = null;
381:         $this->textMessage = null;
382:         $this->messageId = true;
383:         $this->delivery = 'mail';
384:     }
385: 
386: /**
387:  * Format the attach array
388:  *
389:  * @return array
390:  */
391:     protected function _formatAttachFiles() {
392:         $files = array();
393:         foreach ($this->attachments as $filename => $attachment) {
394:             $file = $this->_findFiles($attachment);
395:             if (!empty($file)) {
396:                 if (is_int($filename)) {
397:                     $filename = basename($file);
398:                 }
399:                 $files[$filename] = $file;
400:             }
401:         }
402:         return $files;
403:     }
404: 
405: /**
406:  * Find the specified attachment in the list of file paths
407:  *
408:  * @param string $attachment Attachment file name to find
409:  * @return string Path to located file
410:  */
411:     protected function _findFiles($attachment) {
412:         if (file_exists($attachment)) {
413:             return $attachment;
414:         }
415:         foreach ($this->filePaths as $path) {
416:             if (file_exists($path . DS . $attachment)) {
417:                 $file = $path . DS . $attachment;
418:                 return $file;
419:             }
420:         }
421:         return null;
422:     }
423: 
424: /**
425:  * Format addresses to be an array with email as key and alias as value
426:  *
427:  * @param array $addresses
428:  * @return array
429:  */
430:     protected function _formatAddresses($addresses) {
431:         $formatted = array();
432:         foreach ($addresses as $address) {
433:             if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
434:                 $formatted[$this->_strip($matches[3])] = $matches[2];
435:             } else {
436:                 $address = $this->_strip($address);
437:                 $formatted[$address] = $address;
438:             }
439:         }
440:         return $formatted;
441:     }
442: 
443: /**
444:  * Remove certain elements (such as bcc:, to:, %0a) from given value.
445:  * Helps prevent header injection / manipulation on user content.
446:  *
447:  * @param string $value Value to strip
448:  * @param boolean $message Set to true to indicate main message content
449:  * @return string Stripped value
450:  */
451:     protected function _strip($value, $message = false) {
452:         $search  = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
453:         $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
454: 
455:         if ($message !== true) {
456:             $search .= '|\r|\n';
457:         }
458:         $search = '#(?:' . $search . ')#i';
459:         while (preg_match($search, $value)) {
460:             $value = preg_replace($search, '', $value);
461:         }
462:         return $value;
463:     }
464: 
465: }
466: 
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