EmailLib.php 18 KB

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