Message.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace Tools\Mailer;
  3. use Cake\Core\Configure;
  4. use Cake\Mailer\Message as CakeMessage;
  5. use InvalidArgumentException;
  6. use Tools\Utility\Mime;
  7. use Tools\Utility\Text;
  8. use Tools\Utility\Utility;
  9. /**
  10. * Allows locale overwrite to send emails in a specific language
  11. */
  12. class Message extends CakeMessage {
  13. /**
  14. * @var \Tools\Utility\Mime|null
  15. */
  16. protected $_Mime;
  17. /**
  18. * @param array|null $config Array of configs, or string to load configs from app.php
  19. */
  20. public function __construct(?array $config = null) {
  21. parent::__construct($config);
  22. $xMailer = Configure::read('Config.xMailer');
  23. if ($xMailer) {
  24. $this->addHeaders(['X-Mailer' => $xMailer]);
  25. }
  26. $this->setDefaults();
  27. }
  28. /**
  29. * Overwrite to allow custom enhancements
  30. *
  31. * @return void
  32. */
  33. protected function setDefaults(): void {
  34. $fromEmail = Configure::read('Config.systemEmail');
  35. if ($fromEmail) {
  36. $fromName = Configure::read('Config.systemName');
  37. } else {
  38. $fromEmail = Configure::read('Config.adminEmail');
  39. $fromName = Configure::read('Config.adminName');
  40. }
  41. if ($fromEmail) {
  42. $this->setFrom($fromEmail, $fromName);
  43. }
  44. }
  45. /**
  46. * Overwrite to allow mimetype detection
  47. *
  48. * @param array|string $attachments String with the filename or array with filenames
  49. * @throws \InvalidArgumentException
  50. * @return $this
  51. */
  52. public function setAttachments($attachments) {
  53. $attach = [];
  54. foreach ((array)$attachments as $name => $fileInfo) {
  55. if (!is_array($fileInfo)) {
  56. $fileInfo = ['file' => $fileInfo];
  57. }
  58. if (!isset($fileInfo['file'])) {
  59. if (!isset($fileInfo['data'])) {
  60. throw new InvalidArgumentException('No file or data specified.');
  61. }
  62. if (is_int($name)) {
  63. throw new InvalidArgumentException('No filename specified.');
  64. }
  65. } else {
  66. $fileName = $fileInfo['file'];
  67. if (!preg_match('~^https?://~i', $fileInfo['file'])) {
  68. $fileInfo['file'] = realpath($fileInfo['file']);
  69. }
  70. if ($fileInfo['file'] === false || !Utility::fileExists($fileInfo['file'])) {
  71. throw new InvalidArgumentException(sprintf('File not found: "%s"', $fileName));
  72. }
  73. if (is_int($name)) {
  74. $name = basename($fileInfo['file']);
  75. }
  76. }
  77. if (!isset($fileInfo['mimetype'])) {
  78. $ext = pathinfo($name, PATHINFO_EXTENSION);
  79. $fileInfo['mimetype'] = $this->_getMimeByExtension($ext);
  80. }
  81. $attach[$name] = $fileInfo;
  82. }
  83. parent::setAttachments($attach);
  84. return $this;
  85. }
  86. /**
  87. * Add an attachment from file
  88. *
  89. * @param string $file Absolute path
  90. * @param string|null $name
  91. * @param array $fileInfo
  92. * @return $this
  93. */
  94. public function addAttachment($file, $name = null, $fileInfo = []) {
  95. $fileInfo['file'] = $file;
  96. if (!empty($name)) {
  97. $fileInfo = [$name => $fileInfo];
  98. } else {
  99. $fileInfo = [$fileInfo];
  100. }
  101. $this->addAttachments($fileInfo);
  102. return $this;
  103. }
  104. /**
  105. * Add an attachment as blob
  106. *
  107. * @param string $content Blob data
  108. * @param string $filename to attach it
  109. * @param string|null $mimeType (leave it empty to get mimetype from $filename)
  110. * @param array $fileInfo
  111. * @return $this
  112. */
  113. public function addBlobAttachment($content, $filename, $mimeType = null, $fileInfo = []) {
  114. if ($mimeType === null) {
  115. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  116. $mimeType = $this->_getMimeByExtension($ext);
  117. }
  118. $fileInfo['data'] = $content;
  119. $fileInfo['mimetype'] = $mimeType;
  120. $file = [$filename => $fileInfo];
  121. $this->addAttachments($file);
  122. return $this;
  123. }
  124. /**
  125. * Adds an inline attachment from file.
  126. *
  127. * Options:
  128. * - mimetype
  129. * - contentDisposition
  130. *
  131. * @param string $contentId
  132. * @param string $file
  133. * @param string|null $name
  134. * @param array<string, mixed> $options
  135. * @return $this
  136. */
  137. public function addEmbeddedAttachmentByContentId($contentId, $file, $name = null, array $options = []) {
  138. if (empty($name)) {
  139. $name = basename($file);
  140. }
  141. $name = pathinfo($name, PATHINFO_FILENAME) . '_' . md5($file) . '.' . pathinfo($name, PATHINFO_EXTENSION);
  142. $options['file'] = $file;
  143. if (empty($options['mimetype'])) {
  144. $options['mimetype'] = $this->_getMime($file);
  145. }
  146. $options['contentId'] = $contentId;
  147. $file = [$name => $options];
  148. $this->addAttachments($file);
  149. return $this;
  150. }
  151. /**
  152. * Adds an inline attachment from file.
  153. *
  154. * Options:
  155. * - mimetype
  156. * - contentDisposition
  157. *
  158. * @param string $file Absolute path
  159. * @param string|null $name (optional)
  160. * @param array<string, mixed> $options Options
  161. * @return string
  162. */
  163. public function addEmbeddedAttachment(string $file, ?string $name = null, array $options = []): string {
  164. if (empty($name)) {
  165. $name = basename($file);
  166. }
  167. $name = pathinfo($name, PATHINFO_FILENAME) . '_' . md5($file) . '.' . pathinfo($name, PATHINFO_EXTENSION);
  168. $cid = $this->_isEmbeddedAttachment($file, $name);
  169. if ($cid) {
  170. return $cid;
  171. }
  172. $options['file'] = $file;
  173. if (empty($options['mimetype'])) {
  174. $options['mimetype'] = $this->_getMime($file);
  175. }
  176. $options['contentId'] = str_replace('-', '', Text::uuid()) . '@' . $this->getDomain();
  177. $file = [$name => $options];
  178. $this->addAttachments($file);
  179. return $options['contentId'];
  180. }
  181. /**
  182. * Adds an inline attachment from file.
  183. *
  184. * Options:
  185. * - mimetype
  186. * - contentDisposition
  187. *
  188. * @param string $contentId
  189. * @param string $content Blob data
  190. * @param string $file File File path to file
  191. * @param string|null $mimeType (leave it empty to get mimetype from $filename)
  192. * @param array<string, mixed> $options
  193. * @return $this
  194. */
  195. public function addEmbeddedBlobAttachmentByContentId($contentId, $content, $file, $mimeType = null, array $options = []) {
  196. if ($mimeType === null) {
  197. $ext = pathinfo($file, PATHINFO_EXTENSION);
  198. $mimeType = $this->_getMimeByExtension($ext);
  199. }
  200. $filename = pathinfo($file, PATHINFO_FILENAME) . '_' . md5($content) . '.' . pathinfo($file, PATHINFO_EXTENSION);
  201. $options['data'] = $content;
  202. $options['mimetype'] = $mimeType;
  203. $options['contentId'] = $contentId;
  204. $file = [$filename => $options];
  205. $this->addAttachments($file);
  206. return $this;
  207. }
  208. /**
  209. * Add an inline attachment as blob
  210. *
  211. * Options:
  212. * - contentDisposition
  213. *
  214. * @param string $content Blob data
  215. * @param string $filename to attach it
  216. * @param string|null $mimeType (leave it empty to get mimetype from $filename)
  217. * @param array|string|null $options Options - string CID is deprecated
  218. * @param array $notUsed
  219. * @return string|null CID CcontentId (null is deprecated)
  220. */
  221. public function addEmbeddedBlobAttachment($content, $filename, $mimeType = null, $options = null, array $notUsed = []) {
  222. if ($mimeType === null) {
  223. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  224. $mimeType = $this->_getMimeByExtension($ext);
  225. }
  226. $contentId = null;
  227. // Deprecated $contentId here
  228. if (!is_array($options)) {
  229. $contentId = $options;
  230. $options = $notUsed;
  231. }
  232. $filename = pathinfo($filename, PATHINFO_FILENAME) . '_' . md5($content) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
  233. if ($contentId === null) {
  234. $cid = $this->_isEmbeddedBlobAttachment($content, $filename);
  235. if ($cid) {
  236. return $cid;
  237. }
  238. }
  239. $options['data'] = $content;
  240. $options['mimetype'] = $mimeType;
  241. $options['contentId'] = $contentId ?: str_replace('-', '', Text::uuid()) . '@' . $this->getDomain();
  242. $file = [$filename => $options];
  243. $this->addAttachments($file);
  244. if ($contentId === null) {
  245. return $options['contentId'];
  246. }
  247. // Deprecated
  248. return $contentId;
  249. }
  250. /**
  251. * Returns if this particular file has already been attached as embedded file with this exact name
  252. * to prevent the same image to overwrite each other and also to only send this image once.
  253. * Allows multiple usage of the same embedded image (using the same cid)
  254. *
  255. * @param string $file
  256. * @param string $name
  257. * @return string|false CID of the found file or false if no such attachment can be found
  258. */
  259. protected function _isEmbeddedAttachment($file, $name) {
  260. foreach ($this->getAttachments() as $filename => $fileInfo) {
  261. if ($filename !== $name) {
  262. continue;
  263. }
  264. return $fileInfo['contentId'];
  265. }
  266. return false;
  267. }
  268. /**
  269. * Returns if this particular file has already been attached as embedded file with this exact name
  270. * to prevent the same image to overwrite each other and also to only send this image once.
  271. * Allows multiple usage of the same embedded image (using the same cid)
  272. *
  273. * @param string $content
  274. * @param string $name
  275. * @return string|false CID of the found file or false if no such attachment can be found
  276. */
  277. protected function _isEmbeddedBlobAttachment($content, $name) {
  278. foreach ($this->getAttachments() as $filename => $fileInfo) {
  279. if ($filename !== $name) {
  280. continue;
  281. }
  282. return $fileInfo['contentId'];
  283. }
  284. return false;
  285. }
  286. /**
  287. * @param string $ext
  288. * @param string $default
  289. * @return mixed
  290. */
  291. protected function _getMimeByExtension($ext, $default = 'application/octet-stream') {
  292. if (!isset($this->_Mime)) {
  293. $this->_Mime = new Mime();
  294. }
  295. $mime = $this->_Mime->getMimeTypeByAlias($ext);
  296. if (!$mime) {
  297. $mime = $default;
  298. }
  299. return $mime;
  300. }
  301. /**
  302. * Try to find mimetype by file extension
  303. *
  304. * @param string $filename File name
  305. * @param string $default default MimeType
  306. * @return string Mimetype (falls back to `application/octet-stream`)
  307. */
  308. protected function _getMime($filename, $default = 'application/octet-stream') {
  309. if (!isset($this->_Mime)) {
  310. $this->_Mime = new Mime();
  311. }
  312. $mime = $this->_Mime->detectMimeType($filename);
  313. // Some environments falsely return the default too fast, better fallback to extension here
  314. if (!$mime || $mime === $default) {
  315. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  316. $mime = $this->_Mime->getMimeTypeByAlias($ext);
  317. }
  318. return $mime;
  319. }
  320. /**
  321. * Read the file contents and return a base64 version of the file contents.
  322. * Overwrite parent to avoid File class and file_exists to false negative existent
  323. * remove images.
  324. * Also fixes file_get_contents (used via File class) to close the connection again
  325. * after getting remote files. So far it would have kept the connection open in HTTP/1.1.
  326. *
  327. * @param string $path The absolute path to the file to read.
  328. * @return string File contents in base64 encoding
  329. */
  330. protected function _readFile($path) {
  331. $context = stream_context_create(
  332. ['http' => ['header' => 'Connection: close']],
  333. );
  334. $content = file_get_contents($path, false, $context);
  335. if (!$content) {
  336. throw new \RuntimeException('No content found for ' . $path);
  337. }
  338. return chunk_split(base64_encode($content));
  339. }
  340. /**
  341. * Attach inline/embedded files to the message.
  342. *
  343. * CUSTOM FIX: blob data support
  344. *
  345. * @override
  346. * @param string|null $boundary Boundary to use. If null, will default to $this->_boundary
  347. * @return array An array of lines to add to the message
  348. */
  349. protected function attachInlineFiles(?string $boundary = null): array {
  350. if ($boundary === null) {
  351. /** @var string $boundary */
  352. $boundary = $this->boundary;
  353. }
  354. $msg = [];
  355. foreach ($this->getAttachments() as $filename => $fileInfo) {
  356. if (empty($fileInfo['contentId'])) {
  357. continue;
  358. }
  359. if (!empty($fileInfo['data'])) {
  360. $data = $fileInfo['data'];
  361. $data = chunk_split(base64_encode($data));
  362. } elseif (!empty($fileInfo['file'])) {
  363. $data = $this->_readFile($fileInfo['file']);
  364. } else {
  365. continue;
  366. }
  367. $msg[] = '--' . $boundary;
  368. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  369. $msg[] = 'Content-Transfer-Encoding: base64';
  370. $msg[] = 'Content-ID: <' . $fileInfo['contentId'] . '>';
  371. $msg[] = 'Content-Disposition: inline; filename="' . $filename . '"';
  372. $msg[] = '';
  373. $msg[] = $data;
  374. $msg[] = '';
  375. }
  376. return $msg;
  377. }
  378. }