PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/QuickApps/Cake/Network/Email/CakeEmail.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 1565 lines | 831 code | 125 blank | 609 comment | 148 complexity | f2007e1c9390b3cd79443af4d017e34c MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Cake E-Mail
  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.Network.Email
  16. * @since CakePHP(tm) v 2.0.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Validation', 'Utility');
  20. App::uses('Multibyte', 'I18n');
  21. App::uses('AbstractTransport', 'Network/Email');
  22. App::uses('String', 'Utility');
  23. App::uses('View', 'View');
  24. App::import('I18n', 'Multibyte');
  25. /**
  26. * Cake e-mail class.
  27. *
  28. * This class 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.Network.Email
  32. */
  33. class CakeEmail {
  34. /**
  35. * Default X-Mailer
  36. *
  37. * @constant EMAIL_CLIENT
  38. */
  39. const EMAIL_CLIENT = 'CakePHP Email';
  40. /**
  41. * Line length - no should more - RFC 2822 - 2.1.1
  42. *
  43. * @constant LINE_LENGTH_SHOULD
  44. */
  45. const LINE_LENGTH_SHOULD = 78;
  46. /**
  47. * Line length - no must more - RFC 2822 - 2.1.1
  48. *
  49. * @constant LINE_LENGTH_MUST
  50. */
  51. const LINE_LENGTH_MUST = 998;
  52. /**
  53. * Type of message - HTML
  54. *
  55. * @constant MESSAGE_HTML
  56. */
  57. const MESSAGE_HTML = 'html';
  58. /**
  59. * Type of message - TEXT
  60. *
  61. * @constant MESSAGE_TEXT
  62. */
  63. const MESSAGE_TEXT = 'text';
  64. /**
  65. * Recipient of the email
  66. *
  67. * @var array
  68. */
  69. protected $_to = array();
  70. /**
  71. * The mail which the email is sent from
  72. *
  73. * @var array
  74. */
  75. protected $_from = array();
  76. /**
  77. * The sender email
  78. *
  79. * @var array();
  80. */
  81. protected $_sender = array();
  82. /**
  83. * The email the recipient will reply to
  84. *
  85. * @var array
  86. */
  87. protected $_replyTo = array();
  88. /**
  89. * The read receipt email
  90. *
  91. * @var array
  92. */
  93. protected $_readReceipt = array();
  94. /**
  95. * The mail that will be used in case of any errors like
  96. * - Remote mailserver down
  97. * - Remote user has exceeded his quota
  98. * - Unknown user
  99. *
  100. * @var array
  101. */
  102. protected $_returnPath = array();
  103. /**
  104. * Carbon Copy
  105. *
  106. * List of email's that should receive a copy of the email.
  107. * The Recipient WILL be able to see this list
  108. *
  109. * @var array
  110. */
  111. protected $_cc = array();
  112. /**
  113. * Blind Carbon Copy
  114. *
  115. * List of email's that should receive a copy of the email.
  116. * The Recipient WILL NOT be able to see this list
  117. *
  118. * @var array
  119. */
  120. protected $_bcc = array();
  121. /**
  122. * Message ID
  123. *
  124. * @var mixed True to generate, False to ignore, String with value
  125. */
  126. protected $_messageId = true;
  127. /**
  128. * Domain for messageId generation.
  129. * Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
  130. *
  131. * @var string
  132. */
  133. protected $_domain = null;
  134. /**
  135. * The subject of the email
  136. *
  137. * @var string
  138. */
  139. protected $_subject = '';
  140. /**
  141. * Associative array of a user defined headers
  142. * Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
  143. *
  144. * @var array
  145. */
  146. protected $_headers = array();
  147. /**
  148. * Layout for the View
  149. *
  150. * @var string
  151. */
  152. protected $_layout = 'default';
  153. /**
  154. * Template for the view
  155. *
  156. * @var string
  157. */
  158. protected $_template = '';
  159. /**
  160. * View for render
  161. *
  162. * @var string
  163. */
  164. protected $_viewRender = 'View';
  165. /**
  166. * Vars to sent to render
  167. *
  168. * @var array
  169. */
  170. protected $_viewVars = array();
  171. /**
  172. * Theme for the View
  173. *
  174. * @var array
  175. */
  176. protected $_theme = null;
  177. /**
  178. * Helpers to be used in the render
  179. *
  180. * @var array
  181. */
  182. protected $_helpers = array('Html');
  183. /**
  184. * Text message
  185. *
  186. * @var string
  187. */
  188. protected $_textMessage = '';
  189. /**
  190. * Html message
  191. *
  192. * @var string
  193. */
  194. protected $_htmlMessage = '';
  195. /**
  196. * Final message to send
  197. *
  198. * @var array
  199. */
  200. protected $_message = array();
  201. /**
  202. * Available formats to be sent.
  203. *
  204. * @var array
  205. */
  206. protected $_emailFormatAvailable = array('text', 'html', 'both');
  207. /**
  208. * What format should the email be sent in
  209. *
  210. * @var string
  211. */
  212. protected $_emailFormat = 'text';
  213. /**
  214. * What method should the email be sent
  215. *
  216. * @var string
  217. */
  218. protected $_transportName = 'Mail';
  219. /**
  220. * Instance of transport class
  221. *
  222. * @var AbstractTransport
  223. */
  224. protected $_transportClass = null;
  225. /**
  226. * Charset the email body is sent in
  227. *
  228. * @var string
  229. */
  230. public $charset = 'utf-8';
  231. /**
  232. * Charset the email header is sent in
  233. * If null, the $charset property will be used as default
  234. *
  235. * @var string
  236. */
  237. public $headerCharset = null;
  238. /**
  239. * The application wide charset, used to encode headers and body
  240. *
  241. * @var string
  242. */
  243. protected $_appCharset = null;
  244. /**
  245. * List of files that should be attached to the email.
  246. *
  247. * Only absolute paths
  248. *
  249. * @var array
  250. */
  251. protected $_attachments = array();
  252. /**
  253. * If set, boundary to use for multipart mime messages
  254. *
  255. * @var string
  256. */
  257. protected $_boundary = null;
  258. /**
  259. * Configuration to transport
  260. *
  261. * @var mixed
  262. */
  263. protected $_config = array();
  264. /**
  265. * 8Bit character sets
  266. *
  267. * @var array
  268. */
  269. protected $_charset8bit = array('UTF-8', 'SHIFT_JIS');
  270. /**
  271. * Constructor
  272. * @param mixed $config Array of configs, or string to load configs from email.php
  273. *
  274. */
  275. public function __construct($config = null) {
  276. $this->_appCharset = Configure::read('App.encoding');
  277. if ($this->_appCharset !== null) {
  278. $this->charset = $this->_appCharset;
  279. }
  280. $this->_domain = env('HTTP_HOST');
  281. if (empty($this->_domain)) {
  282. $this->_domain = php_uname('n');
  283. }
  284. if ($config) {
  285. $this->config($config);
  286. }
  287. if (empty($this->headerCharset)) {
  288. $this->headerCharset = $this->charset;
  289. }
  290. }
  291. /**
  292. * From
  293. *
  294. * @param mixed $email
  295. * @param string $name
  296. * @return mixed
  297. * @throws SocketException
  298. */
  299. public function from($email = null, $name = null) {
  300. if ($email === null) {
  301. return $this->_from;
  302. }
  303. return $this->_setEmailSingle('_from', $email, $name, __d('cake_dev', 'From requires only 1 email address.'));
  304. }
  305. /**
  306. * Sender
  307. *
  308. * @param mixed $email
  309. * @param string $name
  310. * @return mixed
  311. * @throws SocketException
  312. */
  313. public function sender($email = null, $name = null) {
  314. if ($email === null) {
  315. return $this->_sender;
  316. }
  317. return $this->_setEmailSingle('_sender', $email, $name, __d('cake_dev', 'Sender requires only 1 email address.'));
  318. }
  319. /**
  320. * Reply-To
  321. *
  322. * @param mixed $email
  323. * @param string $name
  324. * @return mixed
  325. * @throws SocketException
  326. */
  327. public function replyTo($email = null, $name = null) {
  328. if ($email === null) {
  329. return $this->_replyTo;
  330. }
  331. return $this->_setEmailSingle('_replyTo', $email, $name, __d('cake_dev', 'Reply-To requires only 1 email address.'));
  332. }
  333. /**
  334. * Read Receipt (Disposition-Notification-To header)
  335. *
  336. * @param mixed $email
  337. * @param string $name
  338. * @return mixed
  339. * @throws SocketException
  340. */
  341. public function readReceipt($email = null, $name = null) {
  342. if ($email === null) {
  343. return $this->_readReceipt;
  344. }
  345. return $this->_setEmailSingle('_readReceipt', $email, $name, __d('cake_dev', 'Disposition-Notification-To requires only 1 email address.'));
  346. }
  347. /**
  348. * Return Path
  349. *
  350. * @param mixed $email
  351. * @param string $name
  352. * @return mixed
  353. * @throws SocketException
  354. */
  355. public function returnPath($email = null, $name = null) {
  356. if ($email === null) {
  357. return $this->_returnPath;
  358. }
  359. return $this->_setEmailSingle('_returnPath', $email, $name, __d('cake_dev', 'Return-Path requires only 1 email address.'));
  360. }
  361. /**
  362. * To
  363. *
  364. * @param mixed $email Null to get, String with email, Array with email as key, name as value or email as value (without name)
  365. * @param string $name
  366. * @return mixed
  367. */
  368. public function to($email = null, $name = null) {
  369. if ($email === null) {
  370. return $this->_to;
  371. }
  372. return $this->_setEmail('_to', $email, $name);
  373. }
  374. /**
  375. * Add To
  376. *
  377. * @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
  378. * @param string $name
  379. * @return CakeEmail $this
  380. */
  381. public function addTo($email, $name = null) {
  382. return $this->_addEmail('_to', $email, $name);
  383. }
  384. /**
  385. * Cc
  386. *
  387. * @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
  388. * @param string $name
  389. * @return mixed
  390. */
  391. public function cc($email = null, $name = null) {
  392. if ($email === null) {
  393. return $this->_cc;
  394. }
  395. return $this->_setEmail('_cc', $email, $name);
  396. }
  397. /**
  398. * Add Cc
  399. *
  400. * @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
  401. * @param string $name
  402. * @return CakeEmail $this
  403. */
  404. public function addCc($email, $name = null) {
  405. return $this->_addEmail('_cc', $email, $name);
  406. }
  407. /**
  408. * Bcc
  409. *
  410. * @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
  411. * @param string $name
  412. * @return mixed
  413. */
  414. public function bcc($email = null, $name = null) {
  415. if ($email === null) {
  416. return $this->_bcc;
  417. }
  418. return $this->_setEmail('_bcc', $email, $name);
  419. }
  420. /**
  421. * Add Bcc
  422. *
  423. * @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
  424. * @param string $name
  425. * @return CakeEmail $this
  426. */
  427. public function addBcc($email, $name = null) {
  428. return $this->_addEmail('_bcc', $email, $name);
  429. }
  430. /**
  431. * Charset setter/getter
  432. *
  433. * @param string $charset
  434. * @return string $this->charset
  435. */
  436. public function charset($charset = null) {
  437. if ($charset === null) {
  438. return $this->charset;
  439. }
  440. $this->charset = $charset;
  441. if (empty($this->headerCharset)) {
  442. $this->headerCharset = $charset;
  443. }
  444. return $this->charset;
  445. }
  446. /**
  447. * HeaderCharset setter/getter
  448. *
  449. * @param string $charset
  450. * @return string $this->charset
  451. */
  452. public function headerCharset($charset = null) {
  453. if ($charset === null) {
  454. return $this->headerCharset;
  455. }
  456. return $this->headerCharset = $charset;
  457. }
  458. /**
  459. * Set email
  460. *
  461. * @param string $varName
  462. * @param mixed $email
  463. * @param mixed $name
  464. * @return CakeEmail $this
  465. * @throws SocketException
  466. */
  467. protected function _setEmail($varName, $email, $name) {
  468. if (!is_array($email)) {
  469. if (!Validation::email($email)) {
  470. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
  471. }
  472. if ($name === null) {
  473. $name = $email;
  474. }
  475. $this->{$varName} = array($email => $name);
  476. return $this;
  477. }
  478. $list = array();
  479. foreach ($email as $key => $value) {
  480. if (is_int($key)) {
  481. $key = $value;
  482. }
  483. if (!Validation::email($key)) {
  484. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
  485. }
  486. $list[$key] = $value;
  487. }
  488. $this->{$varName} = $list;
  489. return $this;
  490. }
  491. /**
  492. * Set only 1 email
  493. *
  494. * @param string $varName
  495. * @param mixed $email
  496. * @param string $name
  497. * @param string $throwMessage
  498. * @return CakeEmail $this
  499. * @throws SocketException
  500. */
  501. protected function _setEmailSingle($varName, $email, $name, $throwMessage) {
  502. $current = $this->{$varName};
  503. $this->_setEmail($varName, $email, $name);
  504. if (count($this->{$varName}) !== 1) {
  505. $this->{$varName} = $current;
  506. throw new SocketException($throwMessage);
  507. }
  508. return $this;
  509. }
  510. /**
  511. * Add email
  512. *
  513. * @param string $varName
  514. * @param mixed $email
  515. * @param mixed $name
  516. * @return CakeEmail $this
  517. * @throws SocketException
  518. */
  519. protected function _addEmail($varName, $email, $name) {
  520. if (!is_array($email)) {
  521. if (!Validation::email($email)) {
  522. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
  523. }
  524. if ($name === null) {
  525. $name = $email;
  526. }
  527. $this->{$varName}[$email] = $name;
  528. return $this;
  529. }
  530. $list = array();
  531. foreach ($email as $key => $value) {
  532. if (is_int($key)) {
  533. $key = $value;
  534. }
  535. if (!Validation::email($key)) {
  536. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
  537. }
  538. $list[$key] = $value;
  539. }
  540. $this->{$varName} = array_merge($this->{$varName}, $list);
  541. return $this;
  542. }
  543. /**
  544. * Get/Set Subject.
  545. *
  546. * @param null|string $subject
  547. * @return mixed
  548. */
  549. public function subject($subject = null) {
  550. if ($subject === null) {
  551. return $this->_subject;
  552. }
  553. $this->_subject = $this->_encode((string)$subject);
  554. return $this;
  555. }
  556. /**
  557. * Sets headers for the message
  558. *
  559. * @param array $headers Associative array containing headers to be set.
  560. * @return CakeEmail $this
  561. * @throws SocketException
  562. */
  563. public function setHeaders($headers) {
  564. if (!is_array($headers)) {
  565. throw new SocketException(__d('cake_dev', '$headers should be an array.'));
  566. }
  567. $this->_headers = $headers;
  568. return $this;
  569. }
  570. /**
  571. * Add header for the message
  572. *
  573. * @param array $headers
  574. * @return object $this
  575. * @throws SocketException
  576. */
  577. public function addHeaders($headers) {
  578. if (!is_array($headers)) {
  579. throw new SocketException(__d('cake_dev', '$headers should be an array.'));
  580. }
  581. $this->_headers = array_merge($this->_headers, $headers);
  582. return $this;
  583. }
  584. /**
  585. * Get list of headers
  586. *
  587. * ### Includes:
  588. *
  589. * - `from`
  590. * - `replyTo`
  591. * - `readReceipt`
  592. * - `returnPath`
  593. * - `to`
  594. * - `cc`
  595. * - `bcc`
  596. * - `subject`
  597. *
  598. * @param array $include
  599. * @return array
  600. */
  601. public function getHeaders($include = array()) {
  602. if ($include == array_values($include)) {
  603. $include = array_fill_keys($include, true);
  604. }
  605. $defaults = array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), false);
  606. $include += $defaults;
  607. $headers = array();
  608. $relation = array(
  609. 'from' => 'From',
  610. 'replyTo' => 'Reply-To',
  611. 'readReceipt' => 'Disposition-Notification-To',
  612. 'returnPath' => 'Return-Path'
  613. );
  614. foreach ($relation as $var => $header) {
  615. if ($include[$var]) {
  616. $var = '_' . $var;
  617. $headers[$header] = current($this->_formatAddress($this->{$var}));
  618. }
  619. }
  620. if ($include['sender']) {
  621. if (key($this->_sender) === key($this->_from)) {
  622. $headers['Sender'] = '';
  623. } else {
  624. $headers['Sender'] = current($this->_formatAddress($this->_sender));
  625. }
  626. }
  627. foreach (array('to', 'cc', 'bcc') as $var) {
  628. if ($include[$var]) {
  629. $classVar = '_' . $var;
  630. $headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar}));
  631. }
  632. }
  633. $headers += $this->_headers;
  634. if (!isset($headers['X-Mailer'])) {
  635. $headers['X-Mailer'] = self::EMAIL_CLIENT;
  636. }
  637. if (!isset($headers['Date'])) {
  638. $headers['Date'] = date(DATE_RFC2822);
  639. }
  640. if ($this->_messageId !== false) {
  641. if ($this->_messageId === true) {
  642. $headers['Message-ID'] = '<' . str_replace('-', '', String::UUID()) . '@' . $this->_domain . '>';
  643. } else {
  644. $headers['Message-ID'] = $this->_messageId;
  645. }
  646. }
  647. if ($include['subject']) {
  648. $headers['Subject'] = $this->_subject;
  649. }
  650. $headers['MIME-Version'] = '1.0';
  651. if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
  652. $headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
  653. } elseif ($this->_emailFormat === 'text') {
  654. $headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
  655. } elseif ($this->_emailFormat === 'html') {
  656. $headers['Content-Type'] = 'text/html; charset=' . $this->charset;
  657. }
  658. $headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
  659. return $headers;
  660. }
  661. /**
  662. * Format addresses
  663. *
  664. * @param array $address
  665. * @return array
  666. */
  667. protected function _formatAddress($address) {
  668. $return = array();
  669. foreach ($address as $email => $alias) {
  670. if ($email === $alias) {
  671. $return[] = $email;
  672. } else {
  673. if (strpos($alias, ',') !== false) {
  674. $alias = '"' . $alias . '"';
  675. }
  676. $return[] = sprintf('%s <%s>', $this->_encode($alias), $email);
  677. }
  678. }
  679. return $return;
  680. }
  681. /**
  682. * Template and layout
  683. *
  684. * @param mixed $template Template name or null to not use
  685. * @param mixed $layout Layout name or null to not use
  686. * @return mixed
  687. */
  688. public function template($template = false, $layout = false) {
  689. if ($template === false) {
  690. return array(
  691. 'template' => $this->_template,
  692. 'layout' => $this->_layout
  693. );
  694. }
  695. $this->_template = $template;
  696. if ($layout !== false) {
  697. $this->_layout = $layout;
  698. }
  699. return $this;
  700. }
  701. /**
  702. * View class for render
  703. *
  704. * @param string $viewClass
  705. * @return mixed
  706. */
  707. public function viewRender($viewClass = null) {
  708. if ($viewClass === null) {
  709. return $this->_viewRender;
  710. }
  711. $this->_viewRender = $viewClass;
  712. return $this;
  713. }
  714. /**
  715. * Variables to be set on render
  716. *
  717. * @param array $viewVars
  718. * @return mixed
  719. */
  720. public function viewVars($viewVars = null) {
  721. if ($viewVars === null) {
  722. return $this->_viewVars;
  723. }
  724. $this->_viewVars = array_merge($this->_viewVars, (array)$viewVars);
  725. return $this;
  726. }
  727. /**
  728. * Theme to use when rendering
  729. *
  730. * @param string $theme
  731. * @return mixed
  732. */
  733. public function theme($theme = null) {
  734. if ($theme === null) {
  735. return $this->_theme;
  736. }
  737. $this->_theme = $theme;
  738. return $this;
  739. }
  740. /**
  741. * Helpers to be used in render
  742. *
  743. * @param array $helpers
  744. * @return mixed
  745. */
  746. public function helpers($helpers = null) {
  747. if ($helpers === null) {
  748. return $this->_helpers;
  749. }
  750. $this->_helpers = (array)$helpers;
  751. return $this;
  752. }
  753. /**
  754. * Email format
  755. *
  756. * @param string $format
  757. * @return mixed
  758. * @throws SocketException
  759. */
  760. public function emailFormat($format = null) {
  761. if ($format === null) {
  762. return $this->_emailFormat;
  763. }
  764. if (!in_array($format, $this->_emailFormatAvailable)) {
  765. throw new SocketException(__d('cake_dev', 'Format not available.'));
  766. }
  767. $this->_emailFormat = $format;
  768. return $this;
  769. }
  770. /**
  771. * Transport name
  772. *
  773. * @param string $name
  774. * @return mixed
  775. */
  776. public function transport($name = null) {
  777. if ($name === null) {
  778. return $this->_transportName;
  779. }
  780. $this->_transportName = (string)$name;
  781. $this->_transportClass = null;
  782. return $this;
  783. }
  784. /**
  785. * Return the transport class
  786. *
  787. * @return CakeEmail
  788. * @throws SocketException
  789. */
  790. public function transportClass() {
  791. if ($this->_transportClass) {
  792. return $this->_transportClass;
  793. }
  794. list($plugin, $transportClassname) = pluginSplit($this->_transportName, true);
  795. $transportClassname .= 'Transport';
  796. App::uses($transportClassname, $plugin . 'Network/Email');
  797. if (!class_exists($transportClassname)) {
  798. throw new SocketException(__d('cake_dev', 'Class "%s" not found.', $transportClassname));
  799. } elseif (!method_exists($transportClassname, 'send')) {
  800. throw new SocketException(__d('cake_dev', 'The "%s" do not have send method.', $transportClassname));
  801. }
  802. return $this->_transportClass = new $transportClassname();
  803. }
  804. /**
  805. * Message-ID
  806. *
  807. * @param mixed $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID
  808. * @return mixed
  809. * @throws SocketException
  810. */
  811. public function messageId($message = null) {
  812. if ($message === null) {
  813. return $this->_messageId;
  814. }
  815. if (is_bool($message)) {
  816. $this->_messageId = $message;
  817. } else {
  818. if (!preg_match('/^\<.+@.+\>$/', $message)) {
  819. throw new SocketException(__d('cake_dev', 'Invalid format for Message-ID. The text should be something like "<uuid@server.com>"'));
  820. }
  821. $this->_messageId = $message;
  822. }
  823. return $this;
  824. }
  825. /**
  826. * Domain as top level (the part after @)
  827. *
  828. * @param string $domain Manually set the domain for CLI mailing
  829. * @return mixed
  830. */
  831. public function domain($domain = null) {
  832. if ($domain === null) {
  833. return $this->_domain;
  834. }
  835. $this->_domain = $domain;
  836. return $this;
  837. }
  838. /**
  839. * Add attachments to the email message
  840. *
  841. * Attachments can be defined in a few forms depending on how much control you need:
  842. *
  843. * Attach a single file:
  844. *
  845. * {{{
  846. * $email->attachments('path/to/file');
  847. * }}}
  848. *
  849. * Attach a file with a different filename:
  850. *
  851. * {{{
  852. * $email->attachments(array('custom_name.txt' => 'path/to/file.txt'));
  853. * }}}
  854. *
  855. * Attach a file and specify additional properties:
  856. *
  857. * {{{
  858. * $email->attachments(array('custom_name.png' => array(
  859. * 'file' => 'path/to/file',
  860. * 'mimetype' => 'image/png',
  861. * 'contentId' => 'abc123'
  862. * ));
  863. * }}}
  864. *
  865. * The `contentId` key allows you to specify an inline attachment. In your email text, you
  866. * can use `<img src="cid:abc123" />` to display the image inline.
  867. *
  868. * @param mixed $attachments String with the filename or array with filenames
  869. * @return mixed Either the array of attachments when getting or $this when setting.
  870. * @throws SocketException
  871. */
  872. public function attachments($attachments = null) {
  873. if ($attachments === null) {
  874. return $this->_attachments;
  875. }
  876. $attach = array();
  877. foreach ((array)$attachments as $name => $fileInfo) {
  878. if (!is_array($fileInfo)) {
  879. $fileInfo = array('file' => $fileInfo);
  880. }
  881. if (!isset($fileInfo['file'])) {
  882. throw new SocketException(__d('cake_dev', 'File not specified.'));
  883. }
  884. $fileInfo['file'] = realpath($fileInfo['file']);
  885. if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
  886. throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileInfo['file']));
  887. }
  888. if (is_int($name)) {
  889. $name = basename($fileInfo['file']);
  890. }
  891. if (!isset($fileInfo['mimetype'])) {
  892. $fileInfo['mimetype'] = 'application/octet-stream';
  893. }
  894. $attach[$name] = $fileInfo;
  895. }
  896. $this->_attachments = $attach;
  897. return $this;
  898. }
  899. /**
  900. * Add attachments
  901. *
  902. * @param mixed $attachments String with the filename or array with filenames
  903. * @return CakeEmail $this
  904. * @throws SocketException
  905. */
  906. public function addAttachments($attachments) {
  907. $current = $this->_attachments;
  908. $this->attachments($attachments);
  909. $this->_attachments = array_merge($current, $this->_attachments);
  910. return $this;
  911. }
  912. /**
  913. * Get generated message (used by transport classes)
  914. *
  915. * @param mixed $type Use MESSAGE_* constants or null to return the full message as array
  916. * @return mixed String if have type, array if type is null
  917. */
  918. public function message($type = null) {
  919. switch ($type) {
  920. case self::MESSAGE_HTML:
  921. return $this->_htmlMessage;
  922. case self::MESSAGE_TEXT:
  923. return $this->_textMessage;
  924. }
  925. return $this->_message;
  926. }
  927. /**
  928. * Configuration to use when send email
  929. *
  930. * @param mixed $config String with configuration name (from email.php), array with config or null to return current config
  931. * @return mixed
  932. */
  933. public function config($config = null) {
  934. if ($config === null) {
  935. return $this->_config;
  936. }
  937. if (!is_array($config)) {
  938. $config = (string)$config;
  939. }
  940. $this->_applyConfig($config);
  941. return $this;
  942. }
  943. /**
  944. * Send an email using the specified content, template and layout
  945. *
  946. * @param mixed $content String with message or array with messages
  947. * @return array
  948. * @throws SocketException
  949. */
  950. public function send($content = null) {
  951. if (empty($this->_from)) {
  952. throw new SocketException(__d('cake_dev', 'From is not specified.'));
  953. }
  954. if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
  955. throw new SocketException(__d('cake_dev', 'You need to specify at least one destination for to, cc or bcc.'));
  956. }
  957. if (is_array($content)) {
  958. $content = implode("\n", $content) . "\n";
  959. }
  960. $this->_textMessage = $this->_htmlMessage = '';
  961. $this->_createBoundary();
  962. $this->_message = $this->_render($this->_wrap($content));
  963. $contents = $this->transportClass()->send($this);
  964. if (!empty($this->_config['log'])) {
  965. $level = LOG_DEBUG;
  966. if ($this->_config['log'] !== true) {
  967. $level = $this->_config['log'];
  968. }
  969. CakeLog::write($level, PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message']);
  970. }
  971. return $contents;
  972. }
  973. /**
  974. * Static method to fast create an instance of CakeEmail
  975. *
  976. * @param mixed $to Address to send (see CakeEmail::to()). If null, will try to use 'to' from transport config
  977. * @param mixed $subject String of subject or null to use 'subject' from transport config
  978. * @param mixed $message String with message or array with variables to be used in render
  979. * @param mixed $transportConfig String to use config from EmailConfig or array with configs
  980. * @param boolean $send Send the email or just return the instance pre-configured
  981. * @return CakeEmail Instance of CakeEmail
  982. * @throws SocketException
  983. */
  984. public static function deliver($to = null, $subject = null, $message = null, $transportConfig = 'fast', $send = true) {
  985. $class = __CLASS__;
  986. $instance = new $class($transportConfig);
  987. if ($to !== null) {
  988. $instance->to($to);
  989. }
  990. if ($subject !== null) {
  991. $instance->subject($subject);
  992. }
  993. if (is_array($message)) {
  994. $instance->viewVars($message);
  995. $message = null;
  996. } elseif ($message === null && array_key_exists('message', $config = $instance->config())) {
  997. $message = $config['message'];
  998. }
  999. if ($send === true) {
  1000. $instance->send($message);
  1001. }
  1002. return $instance;
  1003. }
  1004. /**
  1005. * Apply the config to an instance
  1006. *
  1007. * @param CakeEmail $obj CakeEmail
  1008. * @param array $config
  1009. * @return void
  1010. * @throws ConfigureException When configuration file cannot be found, or is missing
  1011. * the named config.
  1012. */
  1013. protected function _applyConfig($config) {
  1014. if (is_string($config)) {
  1015. if (!class_exists('EmailConfig') && !config('email')) {
  1016. throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
  1017. }
  1018. $configs = new EmailConfig();
  1019. if (!isset($configs->{$config})) {
  1020. throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config));
  1021. }
  1022. $config = $configs->{$config};
  1023. }
  1024. $this->_config += $config;
  1025. if (!empty($config['charset'])) {
  1026. $this->charset = $config['charset'];
  1027. }
  1028. if (!empty($config['headerCharset'])) {
  1029. $this->headerCharset = $config['headerCharset'];
  1030. }
  1031. if (empty($this->headerCharset)) {
  1032. $this->headerCharset = $this->charset;
  1033. }
  1034. $simpleMethods = array(
  1035. 'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
  1036. 'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
  1037. 'transport', 'emailFormat', 'theme',
  1038. );
  1039. foreach ($simpleMethods as $method) {
  1040. if (isset($config[$method])) {
  1041. $this->$method($config[$method]);
  1042. unset($config[$method]);
  1043. }
  1044. }
  1045. if (isset($config['headers'])) {
  1046. $this->setHeaders($config['headers']);
  1047. unset($config['headers']);
  1048. }
  1049. if (array_key_exists('template', $config)) {
  1050. $layout = false;
  1051. if (array_key_exists('layout', $config)) {
  1052. $layout = $config['layout'];
  1053. unset($config['layout']);
  1054. }
  1055. $this->template($config['template'], $layout);
  1056. unset($config['template']);
  1057. }
  1058. $this->transportClass()->config($config);
  1059. }
  1060. /**
  1061. * Reset all EmailComponent internal variables to be able to send out a new email.
  1062. *
  1063. * @return CakeEmail $this
  1064. */
  1065. public function reset() {
  1066. $this->_to = array();
  1067. $this->_from = array();
  1068. $this->_sender = array();
  1069. $this->_replyTo = array();
  1070. $this->_readReceipt = array();
  1071. $this->_returnPath = array();
  1072. $this->_cc = array();
  1073. $this->_bcc = array();
  1074. $this->_messageId = true;
  1075. $this->_subject = '';
  1076. $this->_headers = array();
  1077. $this->_layout = 'default';
  1078. $this->_template = '';
  1079. $this->_viewRender = 'View';
  1080. $this->_viewVars = array();
  1081. $this->_theme = null;
  1082. $this->_helpers = array('Html');
  1083. $this->_textMessage = '';
  1084. $this->_htmlMessage = '';
  1085. $this->_message = '';
  1086. $this->_emailFormat = 'text';
  1087. $this->_transportName = 'Mail';
  1088. $this->_transportClass = null;
  1089. $this->charset = 'utf-8';
  1090. $this->headerCharset = null;
  1091. $this->_attachments = array();
  1092. $this->_config = array();
  1093. return $this;
  1094. }
  1095. /**
  1096. * Encode the specified string using the current charset
  1097. *
  1098. * @param string $text String to encode
  1099. * @return string Encoded string
  1100. */
  1101. protected function _encode($text) {
  1102. $internalEncoding = function_exists('mb_internal_encoding');
  1103. if ($internalEncoding) {
  1104. $restore = mb_internal_encoding();
  1105. mb_internal_encoding($this->_appCharset);
  1106. }
  1107. if (empty($this->headerCharset)) {
  1108. $this->headerCharset = $this->charset;
  1109. }
  1110. $return = mb_encode_mimeheader($text, $this->headerCharset, 'B');
  1111. if ($internalEncoding) {
  1112. mb_internal_encoding($restore);
  1113. }
  1114. return $return;
  1115. }
  1116. /**
  1117. * Translates a string for one charset to another if the App.encoding value
  1118. * differs and the mb_convert_encoding function exists
  1119. *
  1120. * @param string $text The text to be converted
  1121. * @param string $charset the target encoding
  1122. * @return string
  1123. */
  1124. protected function _encodeString($text, $charset) {
  1125. if ($this->_appCharset === $charset || !function_exists('mb_convert_encoding')) {
  1126. return $text;
  1127. }
  1128. return mb_convert_encoding($text, $charset, $this->_appCharset);
  1129. }
  1130. /**
  1131. * Wrap the message to follow the RFC 2822 - 2.1.1
  1132. *
  1133. * @param string $message Message to wrap
  1134. * @return array Wrapped message
  1135. */
  1136. protected function _wrap($message) {
  1137. $message = str_replace(array("\r\n", "\r"), "\n", $message);
  1138. $lines = explode("\n", $message);
  1139. $formatted = array();
  1140. foreach ($lines as $line) {
  1141. if (empty($line)) {
  1142. $formatted[] = '';
  1143. continue;
  1144. }
  1145. if ($line[0] === '.') {
  1146. $line = '.' . $line;
  1147. }
  1148. if (!preg_match('/\<[a-z]/i', $line)) {
  1149. $formatted = array_merge($formatted, explode("\n", wordwrap($line, self::LINE_LENGTH_SHOULD, "\n")));
  1150. continue;
  1151. }
  1152. $tagOpen = false;
  1153. $tmpLine = $tag = '';
  1154. $tmpLineLength = 0;
  1155. for ($i = 0, $count = strlen($line); $i < $count; $i++) {
  1156. $char = $line[$i];
  1157. if ($tagOpen) {
  1158. $tag .= $char;
  1159. if ($char === '>') {
  1160. $tagLength = strlen($tag);
  1161. if ($tagLength + $tmpLineLength < self::LINE_LENGTH_SHOULD) {
  1162. $tmpLine .= $tag;
  1163. $tmpLineLength += $tagLength;
  1164. } else {
  1165. if ($tmpLineLength > 0) {
  1166. $formatted[] = trim($tmpLine);
  1167. $tmpLine = '';
  1168. $tmpLineLength = 0;
  1169. }
  1170. if ($tagLength > self::LINE_LENGTH_SHOULD) {
  1171. $formatted[] = $tag;
  1172. } else {
  1173. $tmpLine = $tag;
  1174. $tmpLineLength = $tagLength;
  1175. }
  1176. }
  1177. $tag = '';
  1178. $tagOpen = false;
  1179. }
  1180. continue;
  1181. }
  1182. if ($char === '<') {
  1183. $tagOpen = true;
  1184. $tag = '<';
  1185. continue;
  1186. }
  1187. if ($char === ' ' && $tmpLineLength >= self::LINE_LENGTH_SHOULD) {
  1188. $formatted[] = $tmpLine;
  1189. $tmpLineLength = 0;
  1190. continue;
  1191. }
  1192. $tmpLine .= $char;
  1193. $tmpLineLength++;
  1194. if ($tmpLineLength === self::LINE_LENGTH_SHOULD) {
  1195. $nextChar = $line[$i + 1];
  1196. if ($nextChar === ' ' || $nextChar === '<') {
  1197. $formatted[] = trim($tmpLine);
  1198. $tmpLine = '';
  1199. $tmpLineLength = 0;
  1200. if ($nextChar === ' ') {
  1201. $i++;
  1202. }
  1203. } else {
  1204. $lastSpace = strrpos($tmpLine, ' ');
  1205. if ($lastSpace === false) {
  1206. continue;
  1207. }
  1208. $formatted[] = trim(substr($tmpLine, 0, $lastSpace));
  1209. $tmpLine = substr($tmpLine, $lastSpace + 1);
  1210. $tmpLineLength = strlen($tmpLine);
  1211. }
  1212. }
  1213. }
  1214. if (!empty($tmpLine)) {
  1215. $formatted[] = $tmpLine;
  1216. }
  1217. }
  1218. $formatted[] = '';
  1219. return $formatted;
  1220. }
  1221. /**
  1222. * Create unique boundary identifier
  1223. *
  1224. * @return void
  1225. */
  1226. protected function _createBoundary() {
  1227. if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
  1228. $this->_boundary = md5(uniqid(time()));
  1229. }
  1230. }
  1231. /**
  1232. * Attach non-embedded files by adding file contents inside boundaries.
  1233. *
  1234. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  1235. * @return array An array of lines to add to the message
  1236. */
  1237. protected function _attachFiles($boundary = null) {
  1238. if ($boundary === null) {
  1239. $boundary = $this->_boundary;
  1240. }
  1241. $msg = array();
  1242. foreach ($this->_attachments as $filename => $fileInfo) {
  1243. if (!empty($fileInfo['contentId'])) {
  1244. continue;
  1245. }
  1246. $data = $this->_readFile($fileInfo['file']);
  1247. $msg[] = '--' . $boundary;
  1248. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  1249. $msg[] = 'Content-Transfer-Encoding: base64';
  1250. $msg[] = 'Content-Disposition: attachment; filename="' . $filename . '"';
  1251. $msg[] = '';
  1252. $msg[] = $data;
  1253. $msg[] = '';
  1254. }
  1255. return $msg;
  1256. }
  1257. /**
  1258. * Read the file contents and return a base64 version of the file contents.
  1259. *
  1260. * @param string $file The file to read.
  1261. * @return string File contents in base64 encoding
  1262. */
  1263. protected function _readFile($file) {
  1264. $handle = fopen($file, 'rb');
  1265. $data = fread($handle, filesize($file));
  1266. $data = chunk_split(base64_encode($data));
  1267. fclose($handle);
  1268. return $data;
  1269. }
  1270. /**
  1271. * Attach inline/embedded files to the message.
  1272. *
  1273. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  1274. * @return array An array of lines to add to the message
  1275. */
  1276. protected function _attachInlineFiles($boundary = null) {
  1277. if ($boundary === null) {
  1278. $boundary = $this->_boundary;
  1279. }
  1280. $msg = array();
  1281. foreach ($this->_attachments as $filename => $fileInfo) {
  1282. if (empty($fileInfo['contentId'])) {
  1283. continue;
  1284. }
  1285. $data = $this->_readFile($fileInfo['file']);
  1286. $msg[] = '--' . $boundary;
  1287. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  1288. $msg[] = 'Content-Transfer-Encoding: base64';
  1289. $msg[] = 'Content-ID: <' . $fileInfo['contentId'] . '>';
  1290. $msg[] = 'Content-Disposition: inline; filename="' . $filename . '"';
  1291. $msg[] = '';
  1292. $msg[] = $data;
  1293. $msg[] = '';
  1294. }
  1295. return $msg;
  1296. }
  1297. /**
  1298. * Render the body of the email.
  1299. *
  1300. * @param string $content Content to render
  1301. * @return array Email body ready to be sent
  1302. */
  1303. protected function _render($content) {
  1304. $content = implode("\n", $content);
  1305. $rendered = $this->_renderTemplates($content);
  1306. $msg = array();
  1307. $contentIds = array_filter((array)Hash::extract($this->_attachments, '{s}.contentId'));
  1308. $hasInlineAttachments = count($contentIds) > 0;
  1309. $hasAttachments = !empty($this->_attachments);
  1310. $hasMultipleTypes = count($rendered) > 1;
  1311. $boundary = $relBoundary = $textBoundary = $this->_boundary;
  1312. if ($hasInlineAttachments) {
  1313. $msg[] = '--' . $boundary;
  1314. $msg[] = 'Content-Type: multipart/related; boundary="rel-' . $boundary . '"';
  1315. $msg[] = '';
  1316. $relBoundary = $textBoundary = 'rel-' . $boundary;
  1317. }
  1318. if ($hasMultipleTypes) {
  1319. $msg[] = '--' . $relBoundary;
  1320. $msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $boundary . '"';
  1321. $msg[] = '';
  1322. $textBoundary = 'alt-' . $boundary;
  1323. }
  1324. if (isset($rendered['text'])) {
  1325. if ($textBoundary !== $boundary || $hasAttachments) {
  1326. $msg[] = '--' . $textBoundary;
  1327. $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
  1328. $msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
  1329. $msg[] = '';
  1330. }
  1331. $this->_textMessage = $rendered['text'];
  1332. $content = explode("\n", $this->_textMessage);
  1333. $msg = array_merge($msg, $content);
  1334. $msg[] = '';
  1335. }
  1336. if (isset($rendered['html'])) {
  1337. if ($textBoundary !== $boundary || $hasAttachments) {
  1338. $msg[] = '--' . $textBoundary;
  1339. $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
  1340. $msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
  1341. $msg[] = '';
  1342. }
  1343. $this->_htmlMessage = $rendered['html'];
  1344. $content = explode("\n", $this->_htmlMessage);
  1345. $msg = array_merge($msg, $content);
  1346. $msg[] = '';
  1347. }
  1348. if ($hasMultipleTypes) {
  1349. $msg[] = '--' . $textBoundary . '--';
  1350. $msg[] = '';
  1351. }
  1352. if ($hasInlineAttachments) {
  1353. $attachments = $this->_attachInlineFiles($relBoundary);
  1354. $msg = array_merge($msg, $attachments);
  1355. $msg[] = '';
  1356. $msg[] = '--' . $relBoundary . '--';
  1357. $msg[] = '';
  1358. }
  1359. if ($hasAttachments) {
  1360. $attachments = $this->_attachFiles($boundary);
  1361. $msg = array_merge($msg, $attachments);
  1362. }
  1363. if ($hasAttachments || $hasMultipleTypes) {
  1364. $msg[] = '';
  1365. $msg[] = '--' . $boundary . '--';
  1366. $msg[] = '';
  1367. }
  1368. return $msg;
  1369. }
  1370. /**
  1371. * Gets the text body types that are in this email message
  1372. *
  1373. * @return array Array of types. Valid types are 'text' and 'html'
  1374. */
  1375. protected function _getTypes() {
  1376. $types = array($this->_emailFormat);
  1377. if ($this->_emailFormat == 'both') {
  1378. $types = array('html', 'text');
  1379. }
  1380. return $types;
  1381. }
  1382. /**
  1383. * Build and set all the view properties needed to render the templated emails.
  1384. * If there is no template set, the $content will be returned in a hash
  1385. * of the text content types for the email.
  1386. *
  1387. * @param string $content The content passed in from send() in most cases.
  1388. * @return array The rendered content with html and text keys.
  1389. */
  1390. protected function _renderTemplates($content) {
  1391. $types = $this->_getTypes();
  1392. $rendered = array();
  1393. if (empty($this->_template)) {
  1394. foreach ($types as $type) {
  1395. $rendered[$type] = $this->_encodeString($content, $this->charset);
  1396. }
  1397. return $rendered;
  1398. }
  1399. $viewClass = $this->_viewRender;
  1400. if ($viewClass !== 'View') {
  1401. list($plugin, $viewClass) = pluginSplit($viewClass, true);
  1402. $viewClass .= 'View';
  1403. App::uses($viewClass, $plugin . 'View');
  1404. }
  1405. $View = new $viewClass(null);
  1406. $View->viewVars = $this->_viewVars;
  1407. $View->helpers = $this->_helpers;
  1408. list($templatePlugin, $template) = pluginSplit($this->_template);
  1409. list($layoutPlugin, $layout) = pluginSplit($this->_layout);
  1410. if ($templatePlugin) {
  1411. $View->plugin = $templatePlugin;
  1412. } elseif ($layoutPlugin) {
  1413. $View->plugin = $layoutPlugin;
  1414. }
  1415. if ($this->_theme) {
  1416. $View->theme = $this->_theme;
  1417. }
  1418. foreach ($types as $type) {
  1419. $View->set('content', $content);
  1420. $View->hasRendered = false;
  1421. $View->viewPath = $View->layoutPath = 'Emails' . DS . $type;
  1422. $render = $View->render($template, $layout);
  1423. $render = str_replace(array("\r\n", "\r"), "\n", $render);
  1424. $rendered[$type] = $this->_encodeString($render, $this->charset);
  1425. }
  1426. return $rendered;
  1427. }
  1428. /**
  1429. * Return the Content-Transfer Encoding value based on the set charset
  1430. *
  1431. * @return void
  1432. */
  1433. protected function _getContentTransferEncoding() {
  1434. $charset = strtoupper($this->charset);
  1435. if (in_array($charset, $this->_charset8bit)) {
  1436. return '8bit';
  1437. }
  1438. return '7bit';
  1439. }
  1440. }