EmailLib.php 16 KB

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