EmailComponent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /**
  3. * Email Component
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.libs.controller.components
  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.libs.controller.components
  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. */
  266. public function initialize($controller) {
  267. if (Configure::read('App.encoding') !== null) {
  268. $this->charset = Configure::read('App.encoding');
  269. }
  270. }
  271. /**
  272. * Send an email using the specified content, template and layout
  273. *
  274. * @param mixed $content Either an array of text lines, or a string with contents
  275. * If you are rendering a template this variable will be sent to the templates as `$content`
  276. * @param string $template Template to use when sending email
  277. * @param string $layout Layout to use to enclose email body
  278. * @return boolean Success
  279. */
  280. public function send($content = null, $template = null, $layout = null) {
  281. $lib = new CakeEmail();
  282. $lib->charset = $this->charset;
  283. $lib->from($this->_formatAddresses((array)$this->from));
  284. if (!empty($this->to)) {
  285. $lib->to($this->_formatAddresses((array)$this->to));
  286. }
  287. if (!empty($this->cc)) {
  288. $lib->cc($this->_formatAddresses((array)$this->cc));
  289. }
  290. if (!empty($this->bcc)) {
  291. $lib->bcc($this->_formatAddresses((array)$this->bcc));
  292. }
  293. if (!empty($this->replyTo)) {
  294. $lib->replyTo($this->_formatAddresses((array)$this->replyTo));
  295. }
  296. if (!empty($this->return)) {
  297. $lib->returnPath($this->_formatAddresses((array)$this->return));
  298. }
  299. if (!empty($readReceipt)) {
  300. $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
  301. }
  302. $lib->subject($this->subject)->messageID($this->messageId);
  303. $headers = array('X-Mailer' => $this->xMailer);
  304. foreach ($this->headers as $key => $value) {
  305. $headers['X-' . $key] = $value;
  306. }
  307. if ($this->date != false) {
  308. $headers['Date'] = $this->date;
  309. }
  310. $lib->setHeaders($headers);
  311. if ($template) {
  312. $this->template = $template;
  313. }
  314. if ($layout) {
  315. $this->layout = $layout;
  316. }
  317. $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
  318. if (!empty($this->attachments)) {
  319. $lib->attachments($this->_formatAttachFiles());
  320. }
  321. $lib->transport($this->delivery);
  322. if ($this->delivery === 'mail') {
  323. $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
  324. } elseif ($this->delivery === 'smtp') {
  325. $lib->config($this->smtpOptions);
  326. } else {
  327. $lib->config(array());
  328. }
  329. $sent = $lib->send($content);
  330. $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
  331. if (empty($this->htmlMessage)) {
  332. $this->htmlMessage = null;
  333. }
  334. $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
  335. if (empty($this->textMessage)) {
  336. $this->textMessage = null;
  337. }
  338. $this->_header = array();
  339. $this->_message = array();
  340. return $sent;
  341. }
  342. /**
  343. * Reset all EmailComponent internal variables to be able to send out a new email.
  344. *
  345. * @link http://book.cakephp.org/view/1285/Sending-Multiple-Emails-in-a-loop
  346. */
  347. public function reset() {
  348. $this->template = null;
  349. $this->to = array();
  350. $this->from = null;
  351. $this->replyTo = null;
  352. $this->return = null;
  353. $this->cc = array();
  354. $this->bcc = array();
  355. $this->subject = null;
  356. $this->additionalParams = null;
  357. $this->date = null;
  358. $this->attachments = array();
  359. $this->htmlMessage = null;
  360. $this->textMessage = null;
  361. $this->messageId = true;
  362. }
  363. /**
  364. * Format the attach array
  365. *
  366. * @return array
  367. */
  368. protected function _formatAttachFiles() {
  369. $files = array();
  370. foreach ($this->attachments as $filename => $attachment) {
  371. $file = $this->_findFiles($attachment);
  372. if (!empty($file)) {
  373. if (is_int($filename)) {
  374. $filename = basename($file);
  375. }
  376. $files[$filename] = $file;
  377. }
  378. }
  379. return $files;
  380. }
  381. /**
  382. * Find the specified attachment in the list of file paths
  383. *
  384. * @param string $attachment Attachment file name to find
  385. * @return string Path to located file
  386. * @access private
  387. */
  388. function _findFiles($attachment) {
  389. if (file_exists($attachment)) {
  390. return $attachment;
  391. }
  392. foreach ($this->filePaths as $path) {
  393. if (file_exists($path . DS . $attachment)) {
  394. $file = $path . DS . $attachment;
  395. return $file;
  396. }
  397. }
  398. return null;
  399. }
  400. /**
  401. * Encode the specified string using the current charset
  402. *
  403. * @param string $subject String to encode
  404. * @return string Encoded string
  405. * @access private
  406. */
  407. function _encode($subject) {
  408. $subject = $this->_strip($subject);
  409. $nl = "\r\n";
  410. if ($this->delivery == 'mail') {
  411. $nl = '';
  412. }
  413. $internalEncoding = function_exists('mb_internal_encoding');
  414. if ($internalEncoding) {
  415. $restore = mb_internal_encoding();
  416. mb_internal_encoding($this->charset);
  417. }
  418. $return = mb_encode_mimeheader($subject, $this->charset, 'B', $nl);
  419. if ($internalEncoding) {
  420. mb_internal_encoding($restore);
  421. }
  422. return $return;
  423. }
  424. /**
  425. * Format addresses to be an array with email as key and alias as value
  426. *
  427. * @param array $addresses
  428. * @return array
  429. */
  430. protected function _formatAddresses($addresses) {
  431. $formatted = array();
  432. foreach ($addresses as $address) {
  433. if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
  434. $formatted[$this->_strip($matches[3])] = $this->_encode($matches[2]);
  435. } else {
  436. $address = $this->_strip($address);
  437. $formatted[$address] = $address;
  438. }
  439. }
  440. return $formatted;
  441. }
  442. /**
  443. * Remove certain elements (such as bcc:, to:, %0a) from given value.
  444. * Helps prevent header injection / mainipulation on user content.
  445. *
  446. * @param string $value Value to strip
  447. * @param boolean $message Set to true to indicate main message content
  448. * @return string Stripped value
  449. * @access private
  450. */
  451. function _strip($value, $message = false) {
  452. $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
  453. $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
  454. if ($message !== true) {
  455. $search .= '|\r|\n';
  456. }
  457. $search = '#(?:' . $search . ')#i';
  458. while (preg_match($search, $value)) {
  459. $value = preg_replace($search, '', $value);
  460. }
  461. return $value;
  462. }
  463. }