PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Controller/Component/EmailComponent.php

https://bitbucket.org/koronios/cakephp
PHP | 466 lines | 170 code | 47 blank | 249 comment | 24 complexity | 648b4a711e4fe208343b6e344f0e188d MD5 | raw file
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('Component', 'Controller');
  21. App::uses('Multibyte', 'I18n');
  22. App::uses('CakeEmail', 'Network/Email');
  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 Use Network/CakeEmail
  33. */
  34. class EmailComponent extends Component {
  35. /**
  36. * Recipient of the email
  37. *
  38. * @var string
  39. */
  40. public $to = null;
  41. /**
  42. * The mail which the email is sent from
  43. *
  44. * @var string
  45. */
  46. public $from = null;
  47. /**
  48. * The email the recipient will reply to
  49. *
  50. * @var string
  51. */
  52. public $replyTo = null;
  53. /**
  54. * The read receipt email
  55. *
  56. * @var string
  57. */
  58. public $readReceipt = null;
  59. /**
  60. * The mail that will be used in case of any errors like
  61. * - Remote mailserver down
  62. * - Remote user has exceeded his quota
  63. * - Unknown user
  64. *
  65. * @var string
  66. */
  67. public $return = null;
  68. /**
  69. * Carbon Copy
  70. *
  71. * List of email's that should receive a copy of the email.
  72. * The Recipient WILL be able to see this list
  73. *
  74. * @var array
  75. */
  76. public $cc = array();
  77. /**
  78. * Blind Carbon Copy
  79. *
  80. * List of email's that should receive a copy of the email.
  81. * The Recipient WILL NOT be able to see this list
  82. *
  83. * @var array
  84. */
  85. public $bcc = array();
  86. /**
  87. * The date to put in the Date: header. This should be a date
  88. * conforming with the RFC2822 standard. Leave null, to have
  89. * today's date generated.
  90. *
  91. * @var string
  92. */
  93. public $date = null;
  94. /**
  95. * The subject of the email
  96. *
  97. * @var string
  98. */
  99. public $subject = null;
  100. /**
  101. * Associative array of a user defined headers
  102. * Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
  103. *
  104. * @var array
  105. */
  106. public $headers = array();
  107. /**
  108. * List of additional headers
  109. *
  110. * These will NOT be used if you are using safemode and mail()
  111. *
  112. * @var string
  113. */
  114. public $additionalParams = null;
  115. /**
  116. * Layout for the View
  117. *
  118. * @var string
  119. */
  120. public $layout = 'default';
  121. /**
  122. * Template for the view
  123. *
  124. * @var string
  125. */
  126. public $template = null;
  127. /**
  128. * Line feed character(s) to be used when sending using mail() function
  129. * By default PHP_EOL is used.
  130. * RFC2822 requires it to be CRLF but some Unix
  131. * mail transfer agents replace LF by CRLF automatically
  132. * (which leads to doubling CR if CRLF is used).
  133. *
  134. * @var string
  135. */
  136. public $lineFeed = PHP_EOL;
  137. /**
  138. * What format should the email be sent in
  139. *
  140. * Supported formats:
  141. * - text
  142. * - html
  143. * - both
  144. *
  145. * @var string
  146. */
  147. public $sendAs = 'text';
  148. /**
  149. * What method should the email be sent by
  150. *
  151. * Supported methods:
  152. * - mail
  153. * - smtp
  154. * - debug
  155. *
  156. * @var string
  157. */
  158. public $delivery = 'mail';
  159. /**
  160. * charset the email is sent in
  161. *
  162. * @var string
  163. */
  164. public $charset = 'utf-8';
  165. /**
  166. * List of files that should be attached to the email.
  167. *
  168. * Can be both absolute and relative paths
  169. *
  170. * @var array
  171. */
  172. public $attachments = array();
  173. /**
  174. * What mailer should EmailComponent identify itself as
  175. *
  176. * @var string
  177. */
  178. public $xMailer = 'CakePHP Email Component';
  179. /**
  180. * The list of paths to search if an attachment isn't absolute
  181. *
  182. * @var array
  183. */
  184. public $filePaths = array();
  185. /**
  186. * List of options to use for smtp mail method
  187. *
  188. * Options is:
  189. * - port
  190. * - host
  191. * - timeout
  192. * - username
  193. * - password
  194. * - client
  195. *
  196. * @var array
  197. */
  198. public $smtpOptions = array();
  199. /**
  200. * Contains the rendered plain text message if one was sent.
  201. *
  202. * @var string
  203. */
  204. public $textMessage = null;
  205. /**
  206. * Contains the rendered HTML message if one was sent.
  207. *
  208. * @var string
  209. */
  210. public $htmlMessage = null;
  211. /**
  212. * Whether to generate a Message-ID header for the
  213. * e-mail. True to generate a Message-ID, False to let
  214. * it be handled by sendmail (or similar) or a string
  215. * to completely override the Message-ID.
  216. *
  217. * If you are sending Email from a shell, be sure to set this value. As you
  218. * could encounter delivery issues if you do not.
  219. *
  220. * @var mixed
  221. */
  222. public $messageId = true;
  223. /**
  224. * Controller reference
  225. *
  226. * @var Controller
  227. */
  228. protected $_controller = null;
  229. /**
  230. * Constructor
  231. *
  232. * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
  233. * @param array $settings Array of configuration settings.
  234. */
  235. public function __construct(ComponentCollection $collection, $settings = array()) {
  236. $this->_controller = $collection->getController();
  237. parent::__construct($collection, $settings);
  238. }
  239. /**
  240. * Initialize component
  241. *
  242. * @param Controller $controller Instantiating controller
  243. * @return void
  244. */
  245. public function initialize(Controller $controller) {
  246. if (Configure::read('App.encoding') !== null) {
  247. $this->charset = Configure::read('App.encoding');
  248. }
  249. }
  250. /**
  251. * Send an email using the specified content, template and layout
  252. *
  253. * @param string|array $content Either an array of text lines, or a string with contents
  254. * If you are rendering a template this variable will be sent to the templates as `$content`
  255. * @param string $template Template to use when sending email
  256. * @param string $layout Layout to use to enclose email body
  257. * @return boolean Success
  258. */
  259. public function send($content = null, $template = null, $layout = null) {
  260. $lib = new CakeEmail();
  261. $lib->charset = $this->charset;
  262. $lib->headerCharset = $this->charset;
  263. $lib->from($this->_formatAddresses((array)$this->from));
  264. if (!empty($this->to)) {
  265. $lib->to($this->_formatAddresses((array)$this->to));
  266. }
  267. if (!empty($this->cc)) {
  268. $lib->cc($this->_formatAddresses((array)$this->cc));
  269. }
  270. if (!empty($this->bcc)) {
  271. $lib->bcc($this->_formatAddresses((array)$this->bcc));
  272. }
  273. if (!empty($this->replyTo)) {
  274. $lib->replyTo($this->_formatAddresses((array)$this->replyTo));
  275. }
  276. if (!empty($this->return)) {
  277. $lib->returnPath($this->_formatAddresses((array)$this->return));
  278. }
  279. if (!empty($this->readReceipt)) {
  280. $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
  281. }
  282. $lib->subject($this->subject)->messageID($this->messageId);
  283. $lib->helpers($this->_controller->helpers);
  284. $headers = array('X-Mailer' => $this->xMailer);
  285. foreach ($this->headers as $key => $value) {
  286. $headers['X-' . $key] = $value;
  287. }
  288. if ($this->date) {
  289. $headers['Date'] = $this->date;
  290. }
  291. $lib->setHeaders($headers);
  292. if ($template) {
  293. $this->template = $template;
  294. }
  295. if ($layout) {
  296. $this->layout = $layout;
  297. }
  298. $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
  299. if (!empty($this->attachments)) {
  300. $lib->attachments($this->_formatAttachFiles());
  301. }
  302. $lib->transport(ucfirst($this->delivery));
  303. if ($this->delivery === 'mail') {
  304. $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
  305. } elseif ($this->delivery === 'smtp') {
  306. $lib->config($this->smtpOptions);
  307. } else {
  308. $lib->config(array());
  309. }
  310. $sent = $lib->send($content);
  311. $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
  312. if (empty($this->htmlMessage)) {
  313. $this->htmlMessage = null;
  314. }
  315. $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
  316. if (empty($this->textMessage)) {
  317. $this->textMessage = null;
  318. }
  319. $this->_header = array();
  320. $this->_message = array();
  321. return $sent;
  322. }
  323. /**
  324. * Reset all EmailComponent internal variables to be able to send out a new email.
  325. *
  326. * @return void
  327. */
  328. public function reset() {
  329. $this->template = null;
  330. $this->to = array();
  331. $this->from = null;
  332. $this->replyTo = null;
  333. $this->return = null;
  334. $this->cc = array();
  335. $this->bcc = array();
  336. $this->subject = null;
  337. $this->additionalParams = null;
  338. $this->date = null;
  339. $this->attachments = array();
  340. $this->htmlMessage = null;
  341. $this->textMessage = null;
  342. $this->messageId = true;
  343. $this->delivery = 'mail';
  344. }
  345. /**
  346. * Format the attach array
  347. *
  348. * @return array
  349. */
  350. protected function _formatAttachFiles() {
  351. $files = array();
  352. foreach ($this->attachments as $filename => $attachment) {
  353. $file = $this->_findFiles($attachment);
  354. if (!empty($file)) {
  355. if (is_int($filename)) {
  356. $filename = basename($file);
  357. }
  358. $files[$filename] = $file;
  359. }
  360. }
  361. return $files;
  362. }
  363. /**
  364. * Find the specified attachment in the list of file paths
  365. *
  366. * @param string $attachment Attachment file name to find
  367. * @return string Path to located file
  368. */
  369. protected function _findFiles($attachment) {
  370. if (file_exists($attachment)) {
  371. return $attachment;
  372. }
  373. foreach ($this->filePaths as $path) {
  374. if (file_exists($path . DS . $attachment)) {
  375. $file = $path . DS . $attachment;
  376. return $file;
  377. }
  378. }
  379. return null;
  380. }
  381. /**
  382. * Format addresses to be an array with email as key and alias as value
  383. *
  384. * @param array $addresses
  385. * @return array
  386. */
  387. protected function _formatAddresses($addresses) {
  388. $formatted = array();
  389. foreach ($addresses as $address) {
  390. if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
  391. $formatted[$this->_strip($matches[3])] = $matches[2];
  392. } else {
  393. $address = $this->_strip($address);
  394. $formatted[$address] = $address;
  395. }
  396. }
  397. return $formatted;
  398. }
  399. /**
  400. * Remove certain elements (such as bcc:, to:, %0a) from given value.
  401. * Helps prevent header injection / manipulation on user content.
  402. *
  403. * @param string $value Value to strip
  404. * @param boolean $message Set to true to indicate main message content
  405. * @return string Stripped value
  406. */
  407. protected function _strip($value, $message = false) {
  408. $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
  409. $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
  410. if ($message !== true) {
  411. $search .= '|\r|\n';
  412. }
  413. $search = '#(?:' . $search . ')#i';
  414. while (preg_match($search, $value)) {
  415. $value = preg_replace($search, '', $value);
  416. }
  417. return $value;
  418. }
  419. }