EmailLib.php 16 KB

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