EmailLib.php 18 KB

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