EmailComponent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/view/1283/Email
  30. * @deprecated Use Network/CakeEmail
  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. * conformant 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 isnt 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. * @link http://book.cakephp.org/view/1290/Sending-A-Message-Using-SMTP
  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. * @link http://book.cakephp.org/view/1285/Sending-Multiple-Emails-in-a-loop
  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. }
  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. * Encode the specified string using the current charset
  381. *
  382. * @param string $subject String to encode
  383. * @return string Encoded string
  384. */
  385. protected function _encode($subject) {
  386. $subject = $this->_strip($subject);
  387. $nl = "\r\n";
  388. if ($this->delivery == 'mail') {
  389. $nl = '';
  390. }
  391. $internalEncoding = function_exists('mb_internal_encoding');
  392. if ($internalEncoding) {
  393. $restore = mb_internal_encoding();
  394. mb_internal_encoding($this->charset);
  395. }
  396. $return = mb_encode_mimeheader($subject, $this->charset, 'B', $nl);
  397. if ($internalEncoding) {
  398. mb_internal_encoding($restore);
  399. }
  400. return $return;
  401. }
  402. /**
  403. * Format addresses to be an array with email as key and alias as value
  404. *
  405. * @param array $addresses
  406. * @return array
  407. */
  408. protected function _formatAddresses($addresses) {
  409. $formatted = array();
  410. foreach ($addresses as $address) {
  411. if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
  412. $formatted[$this->_strip($matches[3])] = $this->_encode($matches[2]);
  413. } else {
  414. $address = $this->_strip($address);
  415. $formatted[$address] = $address;
  416. }
  417. }
  418. return $formatted;
  419. }
  420. /**
  421. * Remove certain elements (such as bcc:, to:, %0a) from given value.
  422. * Helps prevent header injection / mainipulation on user content.
  423. *
  424. * @param string $value Value to strip
  425. * @param boolean $message Set to true to indicate main message content
  426. * @return string Stripped value
  427. */
  428. protected function _strip($value, $message = false) {
  429. $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
  430. $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
  431. if ($message !== true) {
  432. $search .= '|\r|\n';
  433. }
  434. $search = '#(?:' . $search . ')#i';
  435. while (preg_match($search, $value)) {
  436. $value = preg_replace($search, '', $value);
  437. }
  438. return $value;
  439. }
  440. }