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

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