EmailLib.php 15 KB

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