PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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