EmailComponent.php 11 KB

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