Email.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. namespace Tools\Network\Email;
  3. use Cake\Core\Configure;
  4. use Cake\Network\Email\Email as CakeEmail;
  5. use Tools\Utility\Text;
  6. use InvalidArgumentException;
  7. use Tools\Utility\Mime;
  8. class Email extends CakeEmail {
  9. protected $_wrapLength = null;
  10. protected $_priority = null;
  11. protected $_error = null;
  12. public function __construct($config = null) {
  13. if ($config === null) {
  14. $config = 'default';
  15. }
  16. parent::__construct($config);
  17. }
  18. /**
  19. * Change the layout
  20. *
  21. * @param string $layout Layout to use (or false to use none)
  22. * @return resource EmailLib
  23. */
  24. public function layout($layout = false) {
  25. if ($layout !== false) {
  26. $this->_layout = $layout;
  27. }
  28. return $this;
  29. }
  30. /**
  31. * Set/Get wrapLength
  32. *
  33. * @param int $length Must not be more than CakeEmail::LINE_LENGTH_MUST
  34. * @return int|CakeEmail
  35. */
  36. public function wrapLength($length = null) {
  37. if ($length === null) {
  38. return $this->_wrapLength;
  39. }
  40. $this->_wrapLength = $length;
  41. return $this;
  42. }
  43. /**
  44. * Set/Get priority
  45. *
  46. * @param int $priority 1 (highest) to 5 (lowest)
  47. * @return int|CakeEmail
  48. */
  49. public function priority($priority = null) {
  50. if ($priority === null) {
  51. return $this->_priority;
  52. }
  53. $this->_priority = $priority;
  54. return $this;
  55. }
  56. /**
  57. * Fix line length
  58. *
  59. * @overwrite
  60. * @param string $message Message to wrap
  61. * @return array Wrapped message
  62. */
  63. protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
  64. if ($this->_wrapLength !== null) {
  65. $wrapLength = $this->_wrapLength;
  66. }
  67. return parent::_wrap($message, $wrapLength);
  68. }
  69. /**
  70. * EmailLib::resetAndSet()
  71. *
  72. * @return void
  73. */
  74. public function reset() {
  75. parent::reset();
  76. $this->_priority = null;
  77. $this->_wrapLength = null;
  78. $this->_error = null;
  79. $this->_debug = null;
  80. }
  81. /**
  82. * Ovewrite to allow custom enhancements
  83. *
  84. * @param mixed $config
  85. * @return null|$this
  86. */
  87. public function profile($config = null) {
  88. if ($config === null) {
  89. return $this->_profile;
  90. }
  91. if (!is_array($config)) {
  92. $config = (string)$config;
  93. }
  94. $this->_applyConfig($config);
  95. if ($fromEmail = Configure::read('Config.systemEmail')) {
  96. $fromName = Configure::read('Config.systemName');
  97. } else {
  98. $fromEmail = Configure::read('Config.adminEmail');
  99. $fromName = Configure::read('Config.adminName');
  100. }
  101. if ($fromEmail) {
  102. $this->from($fromEmail, $fromName);
  103. }
  104. if ($xMailer = Configure::read('Config.xMailer')) {
  105. $this->addHeaders(['X-Mailer' => $xMailer]);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Overwrite to allow mimetype detection
  111. *
  112. * @param mixed $attachments
  113. * @return
  114. */
  115. public function attachments($attachments = null) {
  116. if ($attachments === null) {
  117. return $this->_attachments;
  118. }
  119. $attach = [];
  120. foreach ((array)$attachments as $name => $fileInfo) {
  121. if (!is_array($fileInfo)) {
  122. $fileInfo = ['file' => $fileInfo];
  123. }
  124. if (!isset($fileInfo['file'])) {
  125. if (!isset($fileInfo['data'])) {
  126. throw new InvalidArgumentException('No file or data specified.');
  127. }
  128. if (is_int($name)) {
  129. throw new InvalidArgumentException('No filename specified.');
  130. }
  131. $fileInfo['data'] = chunk_split(base64_encode($fileInfo['data']), 76, "\r\n");
  132. } else {
  133. $fileName = $fileInfo['file'];
  134. $fileInfo['file'] = realpath($fileInfo['file']);
  135. if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
  136. throw new InvalidArgumentException(sprintf('File not found: "%s"', $fileName));
  137. }
  138. if (is_int($name)) {
  139. $name = basename($fileInfo['file']);
  140. }
  141. }
  142. if (!isset($fileInfo['mimetype'])) {
  143. $ext = pathinfo($name, PATHINFO_EXTENSION);
  144. $fileInfo['mimetype'] = $this->_getMimeByExtension($ext);
  145. }
  146. $attach[$name] = $fileInfo;
  147. }
  148. $this->_attachments = $attach;
  149. return $this;
  150. }
  151. /**
  152. * Add an attachment from file
  153. *
  154. * @param string $file: absolute path
  155. * @param string $filename
  156. * @param array $fileInfo
  157. * @return resource EmailLib
  158. */
  159. public function addAttachment($file, $name = null, $fileInfo = []) {
  160. $fileInfo['file'] = $file;
  161. if (!empty($name)) {
  162. $fileInfo = [$name => $fileInfo];
  163. } else {
  164. $fileInfo = [$fileInfo];
  165. }
  166. return $this->addAttachments($fileInfo);
  167. }
  168. /**
  169. * Add an attachment as blob
  170. *
  171. * @param binary $content: blob data
  172. * @param string $filename to attach it
  173. * @param string $mimeType (leave it empty to get mimetype from $filename)
  174. * @param array $fileInfo
  175. * @return resource EmailLib
  176. */
  177. public function addBlobAttachment($content, $filename, $mimeType = null, $fileInfo = []) {
  178. if ($mimeType === null) {
  179. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  180. $mimeType = $this->_getMimeByExtension($ext);
  181. }
  182. $fileInfo['data'] = $content;
  183. $fileInfo['mimetype'] = $mimeType;
  184. $file = [$filename => $fileInfo];
  185. return $this->addAttachments($file);
  186. }
  187. /**
  188. * Add an inline attachment from file
  189. *
  190. * Options:
  191. * - mimetype
  192. * - contentDisposition
  193. *
  194. * @param string $file: absolute path
  195. * @param string $filename (optional)
  196. * @param string $contentId (optional)
  197. * @param array $options Options
  198. * @return mixed resource $EmailLib or string $contentId
  199. */
  200. public function addEmbeddedAttachment($file, $name = null, $contentId = null, $options = []) {
  201. if (empty($name)) {
  202. $name = basename($file);
  203. }
  204. if ($contentId === null && ($cid = $this->_isEmbeddedAttachment($file, $name))) {
  205. return $cid;
  206. }
  207. $options['file'] = $file;
  208. if (empty($options['mimetype'])) {
  209. $options['mimetype'] = $this->_getMime($file);
  210. }
  211. $options['contentId'] = $contentId ? $contentId : str_replace('-', '', Text::uuid()) . '@' . $this->_domain;
  212. $file = [$name => $options];
  213. $res = $this->addAttachments($file);
  214. if ($contentId === null) {
  215. return $options['contentId'];
  216. }
  217. return $res;
  218. }
  219. /**
  220. * Add an inline attachment as blob
  221. *
  222. * Options:
  223. * - contentDisposition
  224. *
  225. * @param binary $content: blob data
  226. * @param string $filename to attach it
  227. * @param string $mimeType (leave it empty to get mimetype from $filename)
  228. * @param string $contentId (optional)
  229. * @param array $options Options
  230. * @return mixed resource $EmailLib or string $contentId
  231. */
  232. public function addEmbeddedBlobAttachment($content, $filename, $mimeType = null, $contentId = null, $options = []) {
  233. if ($mimeType === null) {
  234. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  235. $mimeType = $this->_getMimeByExtension($ext);
  236. }
  237. $options['data'] = $content;
  238. $options['mimetype'] = $mimeType;
  239. $options['contentId'] = $contentId ? $contentId : str_replace('-', '', Text::uuid()) . '@' . $this->_domain;
  240. $file = [$filename => $options];
  241. $res = $this->addAttachments($file);
  242. if ($contentId === null) {
  243. return $options['contentId'];
  244. }
  245. return $res;
  246. }
  247. /**
  248. * Returns if this particular file has already been attached as embedded file with this exact name
  249. * to prevent the same image to overwrite each other and also to only send this image once.
  250. * Allows multiple usage of the same embedded image (using the same cid)
  251. *
  252. * @return string cid of the found file or false if no such attachment can be found
  253. */
  254. protected function _isEmbeddedAttachment($file, $name) {
  255. foreach ($this->_attachments as $filename => $fileInfo) {
  256. if ($filename !== $name) {
  257. continue;
  258. }
  259. if ($fileInfo['file'] === $file) {
  260. return $fileInfo['contentId'];
  261. }
  262. }
  263. return false;
  264. }
  265. protected function _getMimeByExtension($ext, $default = 'application/octet-stream') {
  266. if (!isset($this->_Mime)) {
  267. $this->_Mime = new Mime();
  268. }
  269. $mime = $this->_Mime->getMimeTypeByAlias($ext);
  270. if (!$mime) {
  271. $mime = $default;
  272. }
  273. return $mime;
  274. }
  275. /**
  276. * Try to find mimetype by file extension
  277. *
  278. * @param string $ext lowercase (jpg, png, pdf, ...)
  279. * @param string $defaultMimeType
  280. * @return string Mimetype (falls back to `application/octet-stream`)
  281. */
  282. protected function _getMime($filename, $default = 'application/octet-stream') {
  283. if (!isset($this->_Mime)) {
  284. $this->_Mime = new Mime();
  285. }
  286. $mime = $this->_Mime->detectMimeType($filename);
  287. // Some environments falsely return the default too fast, better fallback to extension here
  288. if (!$mime || $mime === $default) {
  289. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  290. $mime = $this->_Mime->getMimeTypeByAlias($ext);
  291. }
  292. return $mime;
  293. }
  294. /**
  295. * Read the file contents and return a base64 version of the file contents.
  296. * Overwrite parent to avoid File class and file_exists to false negative existent
  297. * remove images.
  298. * Also fixes file_get_contents (used via File class) to close the connection again
  299. * after getting remote files. So far it would have kept the connection open in HTTP/1.1.
  300. *
  301. * @param string $path The absolute path to the file to read.
  302. * @return string File contents in base64 encoding
  303. */
  304. protected function _readFile($path) {
  305. $context = stream_context_create(
  306. ['http' => ['header' => 'Connection: close']]);
  307. $content = file_get_contents($path, 0, $context);
  308. if (!$content) {
  309. trigger_error('No content found for ' . $path);
  310. }
  311. return chunk_split(base64_encode($content));
  312. }
  313. /**
  314. * Validate if the email has the required fields necessary to make send() work.
  315. * Assumes layouting (does not check on content to be present or if view/layout files are missing).
  316. *
  317. * @return bool Success
  318. */
  319. public function validates() {
  320. if (!empty($this->_subject) && !empty($this->_to)) {
  321. return true;
  322. }
  323. return false;
  324. }
  325. /**
  326. * Set the body of the mail as we send it.
  327. * Note: the text can be an array, each element will appear as a seperate line in the message body.
  328. *
  329. * Do NOT pass a message if you use $this->set() in combination with templates
  330. *
  331. * @overwrite
  332. * @param string/array: message
  333. * @return bool Success
  334. */
  335. public function send($message = null) {
  336. $this->profile('default');
  337. $this->_log = [
  338. 'to' => $this->_to,
  339. 'from' => $this->_from,
  340. 'sender' => $this->_sender,
  341. 'replyTo' => $this->_replyTo,
  342. 'cc' => $this->_cc,
  343. 'subject' => $this->_subject,
  344. 'bcc' => $this->_bcc,
  345. 'transport' => $this->_transport
  346. ];
  347. if ($this->_priority) {
  348. $this->_headers['X-Priority'] = $this->_priority;
  349. //$this->_headers['X-MSMail-Priority'] = 'High';
  350. //$this->_headers['Importance'] = 'High';
  351. }
  352. // Security measure to not sent to the actual addressee in debug mode while email sending is live
  353. if (Configure::read('debug') && Configure::read('Email.live')) {
  354. $adminEmail = Configure::read('Config.adminEmail');
  355. if (!$adminEmail) {
  356. $adminEmail = Configure::read('Config.systemEmail');
  357. }
  358. foreach ($this->_to as $k => $v) {
  359. if ($k === $adminEmail) {
  360. continue;
  361. }
  362. unset($this->_to[$k]);
  363. $this->_to[$adminEmail] = $v;
  364. }
  365. foreach ($this->_cc as $k => $v) {
  366. if ($k === $adminEmail) {
  367. continue;
  368. }
  369. unset($this->_cc[$k]);
  370. $this->_cc[$adminEmail] = $v;
  371. }
  372. foreach ($this->_bcc as $k => $v) {
  373. if ($k === $adminEmail) {
  374. continue;
  375. }
  376. unset($this->_bcc[$k]);
  377. $this->_bcc[] = $v;
  378. }
  379. }
  380. try {
  381. $this->_debug = parent::send($message);
  382. } catch (\Exception $e) {
  383. $this->_error = $e->getMessage();
  384. $this->_error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL .
  385. $e->getTraceAsString();
  386. if (!empty($this->_config['logReport'])) {
  387. $this->_logEmail();
  388. } else {
  389. //Log::write('error', $this->_error);
  390. }
  391. return false;
  392. }
  393. if (!empty($this->_config['logReport'])) {
  394. $this->_logEmail();
  395. }
  396. return true;
  397. }
  398. /**
  399. * Attach inline/embedded files to the message.
  400. *
  401. * CUSTOM FIX: blob data support
  402. *
  403. * @override
  404. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  405. * @return array An array of lines to add to the message
  406. */
  407. protected function _attachInlineFiles($boundary = null) {
  408. if ($boundary === null) {
  409. $boundary = $this->_boundary;
  410. }
  411. $msg = [];
  412. foreach ($this->_attachments as $filename => $fileInfo) {
  413. if (empty($fileInfo['contentId'])) {
  414. continue;
  415. }
  416. if (!empty($fileInfo['data'])) {
  417. $data = $fileInfo['data'];
  418. $data = chunk_split(base64_encode($data));
  419. } elseif (!empty($fileInfo['file'])) {
  420. $data = $this->_readFile($fileInfo['file']);
  421. } else {
  422. continue;
  423. }
  424. $msg[] = '--' . $boundary;
  425. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  426. $msg[] = 'Content-Transfer-Encoding: base64';
  427. $msg[] = 'Content-ID: <' . $fileInfo['contentId'] . '>';
  428. $msg[] = 'Content-Disposition: inline; filename="' . $filename . '"';
  429. $msg[] = '';
  430. $msg[] = $data;
  431. $msg[] = '';
  432. }
  433. return $msg;
  434. }
  435. /**
  436. * Attach non-embedded files by adding file contents inside boundaries.
  437. *
  438. * CUSTOM FIX: blob data support
  439. *
  440. * @override
  441. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  442. * @return array An array of lines to add to the message
  443. */
  444. protected function _attachFiles($boundary = null) {
  445. if ($boundary === null) {
  446. $boundary = $this->_boundary;
  447. }
  448. $msg = [];
  449. foreach ($this->_attachments as $filename => $fileInfo) {
  450. if (!empty($fileInfo['contentId'])) {
  451. continue;
  452. }
  453. if (!empty($fileInfo['data'])) {
  454. $data = $fileInfo['data'];
  455. $data = chunk_split(base64_encode($data));
  456. } elseif (!empty($fileInfo['file'])) {
  457. $data = $this->_readFile($fileInfo['file']);
  458. } else {
  459. continue;
  460. }
  461. $msg[] = '--' . $boundary;
  462. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  463. $msg[] = 'Content-Transfer-Encoding: base64';
  464. if (
  465. !isset($fileInfo['contentDisposition']) ||
  466. $fileInfo['contentDisposition']
  467. ) {
  468. $msg[] = 'Content-Disposition: attachment; filename="' . $filename . '"';
  469. }
  470. $msg[] = '';
  471. $msg[] = $data;
  472. $msg[] = '';
  473. }
  474. return $msg;
  475. }
  476. /**
  477. * Returns the error if existent
  478. *
  479. * @return string
  480. */
  481. public function getError() {
  482. return $this->_error;
  483. }
  484. }