Email.php 14 KB

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