EmailLib.php 18 KB

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