PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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