EmailLib.php 18 KB

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