EmailLib.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <?php
  2. App::uses('CakeEmail', 'Network/Email');
  3. App::uses('CakeLog', 'Log');
  4. App::uses('Utility', 'Tools.Utility');
  5. App::uses('MimeLib', 'Tools.Lib');
  6. if (!defined('BR')) {
  7. define('BR', '<br />');
  8. }
  9. // Support BC (snake case config)
  10. if (!Configure::read('Config.systemEmail')) {
  11. Configure::write('Config.systemEmail', Configure::read('Config.system_email'));
  12. }
  13. if (!Configure::read('Config.systemName')) {
  14. Configure::write('Config.systemName', Configure::read('Config.system_name'));
  15. }
  16. if (!Configure::read('Config.adminEmail')) {
  17. Configure::write('Config.adminEmail', Configure::read('Config.admin_email'));
  18. }
  19. if (!Configure::read('Config.adminName')) {
  20. Configure::write('Config.adminName', Configure::read('Config.admin_name'));
  21. }
  22. /**
  23. * Convenience class for internal mailer.
  24. *
  25. * Adds some useful features and fixes some bugs:
  26. * - enable easier attachment adding (and also from blob)
  27. * - enable embedded images in html mails
  28. * - extensive logging and error tracing
  29. * - create mails with blob attachments (embedded or attached)
  30. * - allow wrapLength to be adjusted
  31. * - Configure::read('Config.xMailer') can modify the x-mailer
  32. * - basic validation supported
  33. * - allow priority to be set (1 to 5)
  34. *
  35. * Configs for auto-from can be set via Configure::read('Config.adminEmail').
  36. * For systemEmail() one also needs Configure value Config.systemEmail to be set.
  37. *
  38. * @author Mark Scherer
  39. * @license MIT
  40. */
  41. class EmailLib extends CakeEmail {
  42. protected $_log = null;
  43. protected $_debug = null;
  44. protected $_error = null;
  45. protected $_wrapLength = null;
  46. protected $_priority = null;
  47. public function __construct($config = null) {
  48. if ($config === null) {
  49. $config = 'default';
  50. }
  51. parent::__construct($config);
  52. $this->resetAndSet();
  53. }
  54. /**
  55. * Quick way to send emails to admin.
  56. * App::uses() + EmailLib::systemEmail()
  57. *
  58. * Note: always go out with default settings (e.g.: SMTP even if debug > 0)
  59. *
  60. * @param string $subject
  61. * @param string $message
  62. * @param string $transportConfig
  63. * @return bool Success
  64. */
  65. public static function systemEmail($subject, $message = 'System Email', $transportConfig = null) {
  66. $class = __CLASS__;
  67. $instance = new $class($transportConfig);
  68. $instance->from(Configure::read('Config.systemEmail'), Configure::read('Config.systemName'));
  69. $instance->to(Configure::read('Config.adminEmail'), Configure::read('Config.adminName'));
  70. if ($subject !== null) {
  71. $instance->subject($subject);
  72. }
  73. if (is_array($message)) {
  74. $instance->viewVars($message);
  75. $message = null;
  76. } elseif ($message === null && array_key_exists('message', $config = $instance->config())) {
  77. $message = $config['message'];
  78. }
  79. return $instance->send($message);
  80. }
  81. /**
  82. * Change the layout
  83. *
  84. * @param string $layout Layout to use (or false to use none)
  85. * @return resource EmailLib
  86. */
  87. public function layout($layout = false) {
  88. if ($layout !== false) {
  89. $this->_layout = $layout;
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Add an attachment from file
  95. *
  96. * @param string $file: absolute path
  97. * @param string $filename
  98. * @param array $fileInfo
  99. * @return resource EmailLib
  100. */
  101. public function addAttachment($file, $name = null, $fileInfo = array()) {
  102. $fileInfo['file'] = $file;
  103. if (!empty($name)) {
  104. $fileInfo = array($name => $fileInfo);
  105. } else {
  106. $fileInfo = array($fileInfo);
  107. }
  108. return $this->addAttachments($fileInfo);
  109. }
  110. /**
  111. * Add an attachment as blob
  112. *
  113. * @param binary $content: blob data
  114. * @param string $filename to attach it
  115. * @param string $mimeType (leave it empty to get mimetype from $filename)
  116. * @param array $fileInfo
  117. * @return resource EmailLib
  118. */
  119. public function addBlobAttachment($content, $filename, $mimeType = null, $fileInfo = array()) {
  120. if ($mimeType === null) {
  121. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  122. $mimeType = $this->_getMimeByExtension($ext);
  123. }
  124. $fileInfo['content'] = $content;
  125. $fileInfo['mimetype'] = $mimeType;
  126. $file = array($filename => $fileInfo);
  127. return $this->addAttachments($file);
  128. }
  129. /**
  130. * Add an inline attachment from file
  131. *
  132. * Options:
  133. * - mimetype
  134. * - contentDisposition
  135. *
  136. * @param string $file: absolute path
  137. * @param string $filename (optional)
  138. * @param string $contentId (optional)
  139. * @param array $options Options
  140. * @return mixed resource $EmailLib or string $contentId
  141. */
  142. public function addEmbeddedAttachment($file, $name = null, $contentId = null, $options = array()) {
  143. if (empty($name)) {
  144. $name = basename($file);
  145. }
  146. if ($contentId === null && ($cid = $this->_isEmbeddedAttachment($file, $name))) {
  147. return $cid;
  148. }
  149. $options['file'] = $file;
  150. if (empty($options['mimetype'])) {
  151. $options['mimetype'] = $this->_getMime($file);
  152. }
  153. $options['contentId'] = $contentId ? $contentId : str_replace('-', '', String::uuid()) . '@' . $this->_domain;
  154. $file = array($name => $options);
  155. $res = $this->addAttachments($file);
  156. if ($contentId === null) {
  157. return $options['contentId'];
  158. }
  159. return $res;
  160. }
  161. /**
  162. * Add an inline attachment as blob
  163. *
  164. * Options:
  165. * - contentDisposition
  166. *
  167. * @param binary $content: blob data
  168. * @param string $filename to attach it
  169. * @param string $mimeType (leave it empty to get mimetype from $filename)
  170. * @param string $contentId (optional)
  171. * @param array $options Options
  172. * @return mixed resource $EmailLib or string $contentId
  173. */
  174. public function addEmbeddedBlobAttachment($content, $filename, $mimeType = null, $contentId = null, $options = array()) {
  175. if ($mimeType === null) {
  176. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  177. $mimeType = $this->_getMimeByExtension($ext);
  178. }
  179. $options['content'] = $content;
  180. $options['mimetype'] = $mimeType;
  181. $options['contentId'] = $contentId ? $contentId : str_replace('-', '', String::uuid()) . '@' . $this->_domain;
  182. $file = array($filename => $options);
  183. $res = $this->addAttachments($file);
  184. if ($contentId === null) {
  185. return $options['contentId'];
  186. }
  187. return $res;
  188. }
  189. /**
  190. * Returns if this particular file has already been attached as embedded file with this exact name
  191. * to prevent the same image to overwrite each other and also to only send this image once.
  192. * Allows multiple usage of the same embedded image (using the same cid)
  193. *
  194. * @return string cid of the found file or false if no such attachment can be found
  195. */
  196. protected function _isEmbeddedAttachment($file, $name) {
  197. foreach ($this->_attachments as $filename => $fileInfo) {
  198. if ($filename !== $name) {
  199. continue;
  200. }
  201. if ($fileInfo['file'] === $file) {
  202. return $fileInfo['contentId'];
  203. }
  204. }
  205. return false;
  206. }
  207. /**
  208. * Try to determine the mimetype by filename.
  209. * Uses finfo_open() if availble, otherwise guesses it via file extension.
  210. *
  211. * @param string $filename
  212. * @return string Mimetype
  213. */
  214. protected function _getMime($filename) {
  215. $mimeType = Utility::getMimeType($filename);
  216. if (!$mimeType) {
  217. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  218. $mimeType = $this->_getMimeByExtension($ext);
  219. }
  220. return $mimeType;
  221. }
  222. /**
  223. * Try to find mimetype by file extension
  224. *
  225. * @param string $ext lowercase (jpg, png, pdf, ...)
  226. * @param string $defaultMimeType
  227. * @return string Mimetype (falls back to `application/octet-stream`)
  228. */
  229. protected function _getMimeByExtension($ext, $default = 'application/octet-stream') {
  230. if (!$ext) {
  231. return $default;
  232. }
  233. if (!isset($this->_Mime)) {
  234. $this->_Mime = new MimeLib();
  235. }
  236. $mime = $this->_Mime->getMimeType($ext);
  237. if (!$mime) {
  238. $mime = $default;
  239. }
  240. return $mime;
  241. }
  242. /**
  243. * Read the file contents and return a base64 version of the file contents.
  244. * Overwrite parent to avoid File class and file_exists to false negative existent
  245. * remove images.
  246. * Also fixes file_get_contents (used via File class) to close the connection again
  247. * after getting remote files. So far it would have kept the connection open in HTTP/1.1.
  248. *
  249. * @param string $path The absolute path to the file to read.
  250. * @return string File contents in base64 encoding
  251. */
  252. protected function _readFile($path) {
  253. $context = stream_context_create(
  254. array('http' => array('header' => 'Connection: close')));
  255. $content = file_get_contents($path, 0, $context);
  256. if (!$content) {
  257. trigger_error('No content found for ' . $path);
  258. }
  259. return chunk_split(base64_encode($content));
  260. }
  261. /**
  262. * Validate if the email has the required fields necessary to make send() work.
  263. * Assumes layouting (does not check on content to be present or if view/layout files are missing).
  264. *
  265. * @return bool Success
  266. */
  267. public function validates() {
  268. if (!empty($this->_subject) && !empty($this->_to)) {
  269. return true;
  270. }
  271. return false;
  272. }
  273. /**
  274. * Attach inline/embedded files to the message.
  275. *
  276. * CUSTOM FIX: blob data support
  277. *
  278. * @override
  279. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  280. * @return array An array of lines to add to the message
  281. */
  282. protected function _attachInlineFiles($boundary = null) {
  283. if ($boundary === null) {
  284. $boundary = $this->_boundary;
  285. }
  286. $msg = array();
  287. foreach ($this->_attachments as $filename => $fileInfo) {
  288. if (empty($fileInfo['contentId'])) {
  289. continue;
  290. }
  291. if (!empty($fileInfo['content'])) {
  292. $data = $fileInfo['content'];
  293. $data = chunk_split(base64_encode($data));
  294. } elseif (!empty($fileInfo['file'])) {
  295. $data = $this->_readFile($fileInfo['file']);
  296. } else {
  297. continue;
  298. }
  299. $msg[] = '--' . $boundary;
  300. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  301. $msg[] = 'Content-Transfer-Encoding: base64';
  302. $msg[] = 'Content-ID: <' . $fileInfo['contentId'] . '>';
  303. $msg[] = 'Content-Disposition: inline; filename="' . $filename . '"';
  304. $msg[] = '';
  305. $msg[] = $data;
  306. $msg[] = '';
  307. }
  308. return $msg;
  309. }
  310. /**
  311. * Attach non-embedded files by adding file contents inside boundaries.
  312. *
  313. * CUSTOM FIX: blob data support
  314. *
  315. * @override
  316. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  317. * @return array An array of lines to add to the message
  318. */
  319. protected function _attachFiles($boundary = null) {
  320. if ($boundary === null) {
  321. $boundary = $this->_boundary;
  322. }
  323. $msg = array();
  324. foreach ($this->_attachments as $filename => $fileInfo) {
  325. if (!empty($fileInfo['contentId'])) {
  326. continue;
  327. }
  328. if (!empty($fileInfo['content'])) {
  329. $data = $fileInfo['content'];
  330. $data = chunk_split(base64_encode($data));
  331. } elseif (!empty($fileInfo['file'])) {
  332. $data = $this->_readFile($fileInfo['file']);
  333. } else {
  334. continue;
  335. }
  336. $msg[] = '--' . $boundary;
  337. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  338. $msg[] = 'Content-Transfer-Encoding: base64';
  339. if (
  340. !isset($fileInfo['contentDisposition']) ||
  341. $fileInfo['contentDisposition']
  342. ) {
  343. $msg[] = 'Content-Disposition: attachment; filename="' . $filename . '"';
  344. }
  345. $msg[] = '';
  346. $msg[] = $data;
  347. $msg[] = '';
  348. }
  349. return $msg;
  350. }
  351. /**
  352. * Add attachments to the email message
  353. *
  354. * CUSTOM FIX: Allow URLs
  355. * CUSTOM FIX: Blob data support
  356. *
  357. * Attachments can be defined in a few forms depending on how much control you need:
  358. *
  359. * Attach a single file:
  360. *
  361. * {{{
  362. * $email->attachments('path/to/file');
  363. * }}}
  364. *
  365. * Attach a file with a different filename:
  366. *
  367. * {{{
  368. * $email->attachments(array('custom_name.txt' => 'path/to/file.txt'));
  369. * }}}
  370. *
  371. * Attach a file and specify additional properties:
  372. *
  373. * {{{
  374. * $email->attachments(array('custom_name.png' => array(
  375. * 'file' => 'path/to/file',
  376. * 'mimetype' => 'image/png',
  377. * 'contentId' => 'abc123'
  378. * ));
  379. * }}}
  380. *
  381. * The `contentId` key allows you to specify an inline attachment. In your email text, you
  382. * can use `<img src="cid:abc123" />` to display the image inline.
  383. *
  384. * @override
  385. * @param mixed $attachments String with the filename or array with filenames
  386. * @return mixed Either the array of attachments when getting or $this when setting.
  387. * @throws SocketException
  388. */
  389. public function attachments($attachments = null) {
  390. if ($attachments === null) {
  391. return $this->_attachments;
  392. }
  393. $attach = array();
  394. foreach ((array)$attachments as $name => $fileInfo) {
  395. if (!is_array($fileInfo)) {
  396. $fileInfo = array('file' => $fileInfo);
  397. }
  398. if (empty($fileInfo['content'])) {
  399. if (!isset($fileInfo['file'])) {
  400. throw new SocketException(__d('cake_dev', 'File not specified.'));
  401. }
  402. $fileName = $fileInfo['file'];
  403. if (!preg_match('~^https?://~i', $fileInfo['file'])) {
  404. $fileInfo['file'] = realpath($fileInfo['file']);
  405. }
  406. if ($fileInfo['file'] === false || !Utility::fileExists($fileInfo['file'])) {
  407. throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileName));
  408. }
  409. if (is_int($name)) {
  410. $name = basename($fileInfo['file']);
  411. }
  412. }
  413. if (empty($fileInfo['mimetype'])) {
  414. $ext = pathinfo($name, PATHINFO_EXTENSION);
  415. $fileInfo['mimetype'] = $this->_getMimeByExtension($ext);
  416. }
  417. $attach[$name] = $fileInfo;
  418. }
  419. $this->_attachments = $attach;
  420. return $this;
  421. }
  422. /**
  423. * Set the body of the mail as we send it.
  424. * Note: the text can be an array, each element will appear as a seperate line in the message body.
  425. *
  426. * Do NOT pass a message if you use $this->set() in combination with templates
  427. *
  428. * @overwrite
  429. * @param string/array: message
  430. * @return bool Success
  431. */
  432. public function send($message = null) {
  433. $this->_log = array(
  434. 'to' => $this->_to,
  435. 'from' => $this->_from,
  436. 'sender' => $this->_sender,
  437. 'replyTo' => $this->_replyTo,
  438. 'cc' => $this->_cc,
  439. 'subject' => $this->_subject,
  440. 'bcc' => $this->_bcc,
  441. 'transport' => $this->_transportName
  442. );
  443. if ($this->_priority) {
  444. $this->_headers['X-Priority'] = $this->_priority;
  445. //$this->_headers['X-MSMail-Priority'] = 'High';
  446. //$this->_headers['Importance'] = 'High';
  447. }
  448. // Security measure to not sent to the actual addressee in debug mode while email sending is live
  449. if (Configure::read('debug') && Configure::read('Email.live')) {
  450. $adminEmail = Configure::read('Config.adminEmail');
  451. if (!$adminEmail) {
  452. $adminEmail = Configure::read('Config.systemEmail');
  453. }
  454. foreach ($this->_to as $k => $v) {
  455. if ($k === $adminEmail) {
  456. continue;
  457. }
  458. unset($this->_to[$k]);
  459. $this->_to[$adminEmail] = $v;
  460. }
  461. foreach ($this->_cc as $k => $v) {
  462. if ($k === $adminEmail) {
  463. continue;
  464. }
  465. unset($this->_cc[$k]);
  466. $this->_cc[$adminEmail] = $v;
  467. }
  468. foreach ($this->_bcc as $k => $v) {
  469. if ($k === $adminEmail) {
  470. continue;
  471. }
  472. unset($this->_bcc[$k]);
  473. $this->_bcc[] = $v;
  474. }
  475. }
  476. try {
  477. $this->_debug = parent::send($message);
  478. } catch (Exception $e) {
  479. $this->_error = $e->getMessage();
  480. $this->_error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL .
  481. $e->getTraceAsString();
  482. if (!empty($this->_config['logReport'])) {
  483. $this->_logEmail();
  484. } else {
  485. CakeLog::write('error', $this->_error);
  486. }
  487. return false;
  488. }
  489. if (!empty($this->_config['logReport'])) {
  490. $this->_logEmail();
  491. }
  492. return true;
  493. }
  494. /**
  495. * Allow modifications of the message
  496. *
  497. * @param string $text
  498. * @return string Text
  499. */
  500. protected function _prepMessage($text) {
  501. return $text;
  502. }
  503. /**
  504. * Returns the error if existent
  505. *
  506. * @return string
  507. */
  508. public function getError() {
  509. return $this->_error;
  510. }
  511. /**
  512. * Returns the log if existent
  513. *
  514. * @return string
  515. */
  516. public function getLog() {
  517. return $this->_log;
  518. }
  519. /**
  520. * Returns the debug content returned by send()
  521. *
  522. * @return string
  523. */
  524. public function getDebug() {
  525. return $this->_debug;
  526. }
  527. /**
  528. * Set/Get wrapLength
  529. *
  530. * @param int $length Must not be more than CakeEmail::LINE_LENGTH_MUST
  531. * @return int|CakeEmail
  532. */
  533. public function wrapLength($length = null) {
  534. if ($length === null) {
  535. return $this->_wrapLength;
  536. }
  537. $this->_wrapLength = $length;
  538. return $this;
  539. }
  540. /**
  541. * Set/Get priority
  542. *
  543. * @param int $priority 1 (highest) to 5 (lowest)
  544. * @return int|CakeEmail
  545. */
  546. public function priority($priority = null) {
  547. if ($priority === null) {
  548. return $this->_priority;
  549. }
  550. $this->_priority = $priority;
  551. return $this;
  552. }
  553. /**
  554. * Fix line length
  555. *
  556. * @overwrite
  557. * @param string $message Message to wrap
  558. * @return array Wrapped message
  559. */
  560. protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
  561. if ($this->_wrapLength !== null) {
  562. $wrapLength = $this->_wrapLength;
  563. }
  564. return parent::_wrap($message, $wrapLength);
  565. }
  566. /**
  567. * Logs Email to type email
  568. *
  569. * @return void
  570. */
  571. protected function _logEmail($append = null) {
  572. $res = $this->_log['transport'] .
  573. ' - ' . 'TO:' . implode(',', array_keys($this->_log['to'])) .
  574. '||FROM:' . implode(',', array_keys($this->_log['from'])) .
  575. '||REPLY:' . implode(',', array_keys($this->_log['replyTo'])) .
  576. '||S:' . $this->_log['subject'];
  577. $type = 'email';
  578. if (!empty($this->_error)) {
  579. $type = 'email_error';
  580. $res .= '||ERROR:' . $this->_error;
  581. }
  582. if ($append) {
  583. $res .= '||' . $append;
  584. }
  585. CakeLog::write($type, $res);
  586. }
  587. /**
  588. * EmailLib::resetAndSet()
  589. *
  590. * @return void
  591. */
  592. public function resetAndSet() {
  593. $this->_to = array();
  594. $this->_cc = array();
  595. $this->_bcc = array();
  596. $this->_messageId = true;
  597. $this->_subject = '';
  598. $this->_headers = array();
  599. $this->_viewVars = array();
  600. $this->_textMessage = '';
  601. $this->_htmlMessage = '';
  602. $this->_message = '';
  603. $this->_attachments = array();
  604. $this->_error = null;
  605. $this->_debug = null;
  606. if ($fromEmail = Configure::read('Config.systemEmail')) {
  607. $fromName = Configure::read('Config.systemName');
  608. } else {
  609. $fromEmail = Configure::read('Config.adminEmail');
  610. $fromName = Configure::read('Config.adminName');
  611. }
  612. if (!$fromEmail) {
  613. throw new RuntimeException('You need to either define systemEmail or adminEmail in Config.');
  614. }
  615. $this->from($fromEmail, $fromName);
  616. if ($xMailer = Configure::read('Config.xMailer')) {
  617. $this->addHeaders(array('X-Mailer' => $xMailer));
  618. }
  619. }
  620. }