EmailComponent.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. * Email Component
  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.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 $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 string|array $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->headerCharset = $this->charset;
  262. $lib->from($this->_formatAddresses((array)$this->from));
  263. if (!empty($this->to)) {
  264. $lib->to($this->_formatAddresses((array)$this->to));
  265. }
  266. if (!empty($this->cc)) {
  267. $lib->cc($this->_formatAddresses((array)$this->cc));
  268. }
  269. if (!empty($this->bcc)) {
  270. $lib->bcc($this->_formatAddresses((array)$this->bcc));
  271. }
  272. if (!empty($this->replyTo)) {
  273. $lib->replyTo($this->_formatAddresses((array)$this->replyTo));
  274. }
  275. if (!empty($this->return)) {
  276. $lib->returnPath($this->_formatAddresses((array)$this->return));
  277. }
  278. if (!empty($this->readReceipt)) {
  279. $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
  280. }
  281. $lib->subject($this->subject)->messageID($this->messageId);
  282. $lib->helpers($this->_controller->helpers);
  283. $headers = array('X-Mailer' => $this->xMailer);
  284. foreach ($this->headers as $key => $value) {
  285. $headers['X-' . $key] = $value;
  286. }
  287. if ($this->date) {
  288. $headers['Date'] = $this->date;
  289. }
  290. $lib->setHeaders($headers);
  291. if ($template) {
  292. $this->template = $template;
  293. }
  294. if ($layout) {
  295. $this->layout = $layout;
  296. }
  297. $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
  298. if (!empty($this->attachments)) {
  299. $lib->attachments($this->_formatAttachFiles());
  300. }
  301. $lib->transport(ucfirst($this->delivery));
  302. if ($this->delivery === 'mail') {
  303. $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
  304. } elseif ($this->delivery === 'smtp') {
  305. $lib->config($this->smtpOptions);
  306. } else {
  307. $lib->config(array());
  308. }
  309. $sent = $lib->send($content);
  310. $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
  311. if (empty($this->htmlMessage)) {
  312. $this->htmlMessage = null;
  313. }
  314. $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
  315. if (empty($this->textMessage)) {
  316. $this->textMessage = null;
  317. }
  318. $this->_header = array();
  319. $this->_message = array();
  320. return $sent;
  321. }
  322. /**
  323. * Reset all EmailComponent internal variables to be able to send out a new email.
  324. *
  325. * @return void
  326. */
  327. public function reset() {
  328. $this->template = null;
  329. $this->to = array();
  330. $this->from = null;
  331. $this->replyTo = null;
  332. $this->return = null;
  333. $this->cc = array();
  334. $this->bcc = array();
  335. $this->subject = null;
  336. $this->additionalParams = null;
  337. $this->date = null;
  338. $this->attachments = array();
  339. $this->htmlMessage = null;
  340. $this->textMessage = null;
  341. $this->messageId = true;
  342. $this->delivery = 'mail';
  343. }
  344. /**
  345. * Format the attach array
  346. *
  347. * @return array
  348. */
  349. protected function _formatAttachFiles() {
  350. $files = array();
  351. foreach ($this->attachments as $filename => $attachment) {
  352. $file = $this->_findFiles($attachment);
  353. if (!empty($file)) {
  354. if (is_int($filename)) {
  355. $filename = basename($file);
  356. }
  357. $files[$filename] = $file;
  358. }
  359. }
  360. return $files;
  361. }
  362. /**
  363. * Find the specified attachment in the list of file paths
  364. *
  365. * @param string $attachment Attachment file name to find
  366. * @return string Path to located file
  367. */
  368. protected function _findFiles($attachment) {
  369. if (file_exists($attachment)) {
  370. return $attachment;
  371. }
  372. foreach ($this->filePaths as $path) {
  373. if (file_exists($path . DS . $attachment)) {
  374. $file = $path . DS . $attachment;
  375. return $file;
  376. }
  377. }
  378. return null;
  379. }
  380. /**
  381. * Format addresses to be an array with email as key and alias as value
  382. *
  383. * @param array $addresses
  384. * @return array
  385. */
  386. protected function _formatAddresses($addresses) {
  387. $formatted = array();
  388. foreach ($addresses as $address) {
  389. if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
  390. $formatted[$this->_strip($matches[3])] = $matches[2];
  391. } else {
  392. $address = $this->_strip($address);
  393. $formatted[$address] = $address;
  394. }
  395. }
  396. return $formatted;
  397. }
  398. /**
  399. * Remove certain elements (such as bcc:, to:, %0a) from given value.
  400. * Helps prevent header injection / manipulation on user content.
  401. *
  402. * @param string $value Value to strip
  403. * @param boolean $message Set to true to indicate main message content
  404. * @return string Stripped value
  405. */
  406. protected function _strip($value, $message = false) {
  407. $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
  408. $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
  409. if ($message !== true) {
  410. $search .= '|\r|\n';
  411. }
  412. $search = '#(?:' . $search . ')#i';
  413. while (preg_match($search, $value)) {
  414. $value = preg_replace($search, '', $value);
  415. }
  416. return $value;
  417. }
  418. }