EmailLib.php 15 KB

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