EmailLib.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. App::uses('CakeEmail', 'Network/Email');
  3. App::uses('CakeLog', 'Log');
  4. if (!defined('BR')) {
  5. define('BR', '<br />');
  6. }
  7. /**
  8. * Convenience class for internal mailer.
  9. * Adds some nice features and fixes some bugs:
  10. * - enbale embedded images in html mails
  11. * - allow setting domain for CLI environment (now in core)
  12. * - enable easier attachment adding
  13. * - extensive logging and error tracing
  14. * - create mails with blob attachments (embedded or attached)
  15. * TODO: cleanup and more tests
  16. *
  17. * @author Mark Scherer
  18. * @license MIT
  19. * @cakephp 2.2
  20. * 2012-03-30 ms
  21. */
  22. class EmailLib extends CakeEmail {
  23. protected $_log = null;
  24. protected $_debug = null;
  25. protected $_error = null;
  26. public function __construct($config = null) {
  27. if ($config === null) {
  28. $config = 'default';
  29. }
  30. parent::__construct($config);
  31. $this->resetAndSet();
  32. }
  33. /**
  34. * quick way to send emails to admin
  35. * App::uses() + EmailLib::systemEmail()
  36. *
  37. * Note: always go out with default settings (e.g.: SMTP even if debug > 0)
  38. * @return bool $success
  39. * 2011-10-31 ms
  40. */
  41. public static function systemEmail($subject, $message = 'System Email', $transportConfig = null) {
  42. $class = __CLASS__;
  43. $instance = new $class($transportConfig);
  44. $instance->to(Configure::read('Config.admin_email'));
  45. $instance->from(Configure::read('Config.admin_email'));
  46. if ($subject !== null) {
  47. $instance->subject($subject);
  48. }
  49. if (is_array($message)) {
  50. $instance->viewVars($message);
  51. $message = null;
  52. } elseif ($message === null && array_key_exists('message', $config = $instance->config())) {
  53. $message = $config['message'];
  54. }
  55. if (true || $send === true) {
  56. return $instance->send($message);
  57. }
  58. return $instance;
  59. }
  60. /**
  61. * @param string $layout Layout to use (or false to use none)
  62. * @return resource EmailLib
  63. * 2011-11-02 ms
  64. */
  65. public function layout($layout = false) {
  66. if ($layout !== false) {
  67. $this->_layout = $layout;
  68. }
  69. return $this;
  70. }
  71. /**
  72. * @param string $file: absolute path
  73. * @param string $filename (optional)
  74. * @return resource EmailLib
  75. * 2011-11-02 ms
  76. */
  77. public function addAttachment($file, $name = null, $fileInfo = array()) {
  78. $fileInfo['file'] = $file;
  79. if (!empty($name)) {
  80. $fileInfo = array($name => $fileInfo);
  81. } else {
  82. $fileInfo = array($fileInfo);
  83. }
  84. return $this->addAttachments($fileInfo);
  85. }
  86. /**
  87. * @param binary $content: blob data
  88. * @param string $filename to attach it
  89. * @param string $mimeType (leave it empty to get mimetype from $filename)
  90. * @param string $contentId (optional)
  91. * @return mixed ressource EmailLib or string $contentId
  92. * 2011-11-02 ms
  93. */
  94. public function addBlobAttachment($content, $name, $mimeType = null, $fileInfo = array()) {
  95. $fileInfo['content'] = $content;
  96. $fileInfo['mimetype'] = $mimeType;
  97. $file = array($name=>$fileInfo);
  98. $res = $this->addAttachments($file);
  99. if ($contentId === null) {
  100. return $fileInfo['contentId'];
  101. }
  102. return $res;
  103. }
  104. /**
  105. * @param binary $content: blob data
  106. * @param string $filename to attach it
  107. * @param string $mimeType (leave it empty to get mimetype from $filename)
  108. * @param string $contentId (optional)
  109. * @param array $options
  110. * - contentDisposition
  111. * @return mixed ressource $EmailLib or string $contentId
  112. * 2011-11-02 ms
  113. */
  114. public function addEmbeddedBlobAttachment($content, $name, $mimeType = null, $contentId = null, $options = array()) {
  115. $options['content'] = $content;
  116. $options['mimetype'] = $mimeType;
  117. $options['contentId'] = $contentId ? $contentId : str_replace('-', '', String::uuid()) . '@' . $this->_domain;
  118. $file = array($name => $options);
  119. $res = $this->addAttachments($file);
  120. if ($contentId === null) {
  121. return $options['contentId'];
  122. }
  123. return $res;
  124. }
  125. /**
  126. * @param string $file: absolute path
  127. * @param string $filename (optional)
  128. * @param string $contentId (optional)
  129. * @param array $options
  130. * - mimetype
  131. * - contentDisposition
  132. * @return mixed ressource $EmailLib or string $contentId
  133. * 2011-11-02 ms
  134. */
  135. public function addEmbeddedAttachment($file, $name = null, $contentId = null, $options = array()) {
  136. $path = realpath($file);
  137. if (empty($name)) {
  138. $name = basename($file);
  139. }
  140. if ($contentId === null && ($cid = $this->_isEmbeddedAttachment($path, $name))) {
  141. return $cid;
  142. }
  143. $options['file'] = $path;
  144. if (empty($options['mimetype'])) {
  145. $options['mimetype'] = $this->_getMime($file);
  146. }
  147. $options['contentId'] = $contentId ? $contentId : str_replace('-', '', String::uuid()) . '@' . $this->_domain;
  148. $file = array($name => $options);
  149. $res = $this->addAttachments($file);
  150. if ($contentId === null) {
  151. return $options['contentId'];
  152. }
  153. return $res;
  154. }
  155. /**
  156. * Returns if this particular file has already been attached as embedded file with this exact name
  157. * to prevent the same image to overwrite each other and also to only send this image once.
  158. * Allows multiple usage of the same embedded image (using the same cid)
  159. *
  160. * @return string cid of the found file or false if no such attachment can be found
  161. */
  162. protected function _isEmbeddedAttachment($file, $name) {
  163. foreach ($this->_attachments as $filename => $fileInfo) {
  164. if ($filename != $name) {
  165. continue;
  166. }
  167. if ($fileInfo['file'] == $file) {
  168. return $fileInfo['contentId'];
  169. }
  170. }
  171. return false;
  172. }
  173. protected function _getMime($filename) {
  174. if (function_exists('finfo_open')) {
  175. $finfo = finfo_open(FILEINFO_MIME);
  176. $mimetype = finfo_file($finfo, $filename);
  177. finfo_close($finfo);
  178. } else {
  179. //TODO: improve
  180. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  181. $mimetype = $this->_getMimeByExtension($ext);
  182. }
  183. return $mimetype;
  184. }
  185. /**
  186. * try to find mimetype by file extension
  187. * @param string $ext lowercase (jpg, png, pdf, ...)
  188. * @param string $defaultMimeType
  189. * @return string $mimeType (falls back to )
  190. * 2012-04-17 ms
  191. */
  192. protected function _getMimeByExtension($ext, $default = 'application/octet-stream') {
  193. switch ($ext) {
  194. case "zip": $mime="application/zip"; break;
  195. case "ez": $mime="application/andrew-inset"; break;
  196. case "hqx": $mime="application/mac-binhex40"; break;
  197. case "cpt": $mime="application/mac-compactpro"; break;
  198. case "doc": $mime="application/msword"; break;
  199. case "bin": $mime="application/octet-stream"; break;
  200. case "dms": $mime="application/octet-stream"; break;
  201. case "lha": $mime="application/octet-stream"; break;
  202. case "lzh": $mime="application/octet-stream"; break;
  203. case "exe": $mime="application/octet-stream"; break;
  204. case "class": $mime="application/octet-stream"; break;
  205. case "so": $mime="application/octet-stream"; break;
  206. case "dll": $mime="application/octet-stream"; break;
  207. case "oda": $mime="application/oda"; break;
  208. case "pdf": $mime="application/pdf"; break;
  209. case "ai": $mime="application/postscript"; break;
  210. case "eps": $mime="application/postscript"; break;
  211. case "ps": $mime="application/postscript"; break;
  212. case "smi": $mime="application/smil"; break;
  213. case "smil": $mime="application/smil"; break;
  214. case "xls": $mime="application/vnd.ms-excel"; break;
  215. case "ppt": $mime="application/vnd.ms-powerpoint"; break;
  216. case "wbxml": $mime="application/vnd.wap.wbxml"; break;
  217. case "wmlc": $mime="application/vnd.wap.wmlc"; break;
  218. case "wmlsc": $mime="application/vnd.wap.wmlscriptc"; break;
  219. case "bcpio": $mime="application/x-bcpio"; break;
  220. case "vcd": $mime="application/x-cdlink"; break;
  221. case "pgn": $mime="application/x-chess-pgn"; break;
  222. case "cpio": $mime="application/x-cpio"; break;
  223. case "csh": $mime="application/x-csh"; break;
  224. case "dcr": $mime="application/x-director"; break;
  225. case "dir": $mime="application/x-director"; break;
  226. case "dxr": $mime="application/x-director"; break;
  227. case "dvi": $mime="application/x-dvi"; break;
  228. case "spl": $mime="application/x-futuresplash"; break;
  229. case "gtar": $mime="application/x-gtar"; break;
  230. case "hdf": $mime="application/x-hdf"; break;
  231. case "js": $mime="application/x-javascript"; break;
  232. case "skp": $mime="application/x-koan"; break;
  233. case "skd": $mime="application/x-koan"; break;
  234. case "skt": $mime="application/x-koan"; break;
  235. case "skm": $mime="application/x-koan"; break;
  236. case "latex": $mime="application/x-latex"; break;
  237. case "nc": $mime="application/x-netcdf"; break;
  238. case "cdf": $mime="application/x-netcdf"; break;
  239. case "sh": $mime="application/x-sh"; break;
  240. case "shar": $mime="application/x-shar"; break;
  241. case "swf": $mime="application/x-shockwave-flash"; break;
  242. case "sit": $mime="application/x-stuffit"; break;
  243. case "sv4cpio": $mime="application/x-sv4cpio"; break;
  244. case "sv4crc": $mime="application/x-sv4crc"; break;
  245. case "tar": $mime="application/x-tar"; break;
  246. case "tcl": $mime="application/x-tcl"; break;
  247. case "tex": $mime="application/x-tex"; break;
  248. case "texinfo": $mime="application/x-texinfo"; break;
  249. case "texi": $mime="application/x-texinfo"; break;
  250. case "t": $mime="application/x-troff"; break;
  251. case "tr": $mime="application/x-troff"; break;
  252. case "roff": $mime="application/x-troff"; break;
  253. case "man": $mime="application/x-troff-man"; break;
  254. case "me": $mime="application/x-troff-me"; break;
  255. case "ms": $mime="application/x-troff-ms"; break;
  256. case "ustar": $mime="application/x-ustar"; break;
  257. case "src": $mime="application/x-wais-source"; break;
  258. case "xhtml": $mime="application/xhtml+xml"; break;
  259. case "xht": $mime="application/xhtml+xml"; break;
  260. case "zip": $mime="application/zip"; break;
  261. case "au": $mime="audio/basic"; break;
  262. case "snd": $mime="audio/basic"; break;
  263. case "mid": $mime="audio/midi"; break;
  264. case "midi": $mime="audio/midi"; break;
  265. case "kar": $mime="audio/midi"; break;
  266. case "mpga": $mime="audio/mpeg"; break;
  267. case "mp2": $mime="audio/mpeg"; break;
  268. case "mp3": $mime="audio/mpeg"; break;
  269. case "aif": $mime="audio/x-aiff"; break;
  270. case "aiff": $mime="audio/x-aiff"; break;
  271. case "aifc": $mime="audio/x-aiff"; break;
  272. case "m3u": $mime="audio/x-mpegurl"; break;
  273. case "ram": $mime="audio/x-pn-realaudio"; break;
  274. case "rm": $mime="audio/x-pn-realaudio"; break;
  275. case "rpm": $mime="audio/x-pn-realaudio-plugin"; break;
  276. case "ra": $mime="audio/x-realaudio"; break;
  277. case "wav": $mime="audio/x-wav"; break;
  278. case "pdb": $mime="chemical/x-pdb"; break;
  279. case "xyz": $mime="chemical/x-xyz"; break;
  280. case "bmp": $mime="image/bmp"; break;
  281. case "gif": $mime="image/gif"; break;
  282. case "ief": $mime="image/ief"; break;
  283. case "jpeg": $mime="image/jpeg"; break;
  284. case "jpg": $mime="image/jpeg"; break;
  285. case "jpe": $mime="image/jpeg"; break;
  286. case "png": $mime="image/png"; break;
  287. case "tiff": $mime="image/tiff"; break;
  288. case "tif": $mime="image/tiff"; break;
  289. case "djvu": $mime="image/vnd.djvu"; break;
  290. case "djv": $mime="image/vnd.djvu"; break;
  291. case "wbmp": $mime="image/vnd.wap.wbmp"; break;
  292. case "ras": $mime="image/x-cmu-raster"; break;
  293. case "pnm": $mime="image/x-portable-anymap"; break;
  294. case "pbm": $mime="image/x-portable-bitmap"; break;
  295. case "pgm": $mime="image/x-portable-graymap"; break;
  296. case "ppm": $mime="image/x-portable-pixmap"; break;
  297. case "rgb": $mime="image/x-rgb"; break;
  298. case "xbm": $mime="image/x-xbitmap"; break;
  299. case "xpm": $mime="image/x-xpixmap"; break;
  300. case "xwd": $mime="image/x-xwindowdump"; break;
  301. case "igs": $mime="model/iges"; break;
  302. case "iges": $mime="model/iges"; break;
  303. case "msh": $mime="model/mesh"; break;
  304. case "mesh": $mime="model/mesh"; break;
  305. case "silo": $mime="model/mesh"; break;
  306. case "wrl": $mime="model/vrml"; break;
  307. case "vrml": $mime="model/vrml"; break;
  308. case "css": $mime="text/css"; break;
  309. case "html": $mime="text/html"; break;
  310. case "htm": $mime="text/html"; break;
  311. case "asc": $mime="text/plain"; break;
  312. case "txt": $mime="text/plain"; break;
  313. case "rtx": $mime="text/richtext"; break;
  314. case "rtf": $mime="text/rtf"; break;
  315. case "sgml": $mime="text/sgml"; break;
  316. case "sgm": $mime="text/sgml"; break;
  317. case "tsv": $mime="text/tab-separated-values"; break;
  318. case "wml": $mime="text/vnd.wap.wml"; break;
  319. case "wmls": $mime="text/vnd.wap.wmlscript"; break;
  320. case "etx": $mime="text/x-setext"; break;
  321. case "xml": $mime="text/xml"; break;
  322. case "xsl": $mime="text/xml"; break;
  323. case "mpeg": $mime="video/mpeg"; break;
  324. case "mpg": $mime="video/mpeg"; break;
  325. case "mpe": $mime="video/mpeg"; break;
  326. case "qt": $mime="video/quicktime"; break;
  327. case "mov": $mime="video/quicktime"; break;
  328. case "mxu": $mime="video/vnd.mpegurl"; break;
  329. case "avi": $mime="video/x-msvideo"; break;
  330. case "movie": $mime="video/x-sgi-movie"; break;
  331. case "asf": $mime="video/x-ms-asf"; break;
  332. case "asx": $mime="video/x-ms-asf"; break;
  333. case "wm": $mime="video/x-ms-wm"; break;
  334. case "wmv": $mime="video/x-ms-wmv"; break;
  335. case "wvx": $mime="video/x-ms-wvx"; break;
  336. case "ice": $mime="x-conference/x-cooltalk"; break;
  337. }
  338. if (empty($mime)) {
  339. $mime = $default;
  340. }
  341. return $mime;
  342. }
  343. public function validates() {
  344. if (!empty($this->Email->subject)) {
  345. return true;
  346. }
  347. return false;
  348. }
  349. /**
  350. * Attach inline/embedded files to the message.
  351. * @override
  352. * CUSTOM FIX: blob data support
  353. *
  354. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  355. * @return array An array of lines to add to the message
  356. */
  357. protected function _attachInlineFiles($boundary = null) {
  358. if ($boundary === null) {
  359. $boundary = $this->_boundary;
  360. }
  361. $msg = array();
  362. foreach ($this->_attachments as $filename => $fileInfo) {
  363. if (empty($fileInfo['contentId'])) {
  364. continue;
  365. }
  366. if (!empty($fileInfo['content'])) {
  367. $data = $fileInfo['content'];
  368. $data = chunk_split(base64_encode($data));
  369. } elseif (!empty($fileInfo['file'])) {
  370. $data = $this->_readFile($fileInfo['file']);
  371. } else {
  372. continue;
  373. }
  374. $msg[] = '--' . $boundary;
  375. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  376. $msg[] = 'Content-Transfer-Encoding: base64';
  377. $msg[] = 'Content-ID: <' . $fileInfo['contentId'] . '>';
  378. $msg[] = 'Content-Disposition: inline; filename="' . $filename . '"';
  379. $msg[] = '';
  380. $msg[] = $data;
  381. $msg[] = '';
  382. }
  383. return $msg;
  384. }
  385. /**
  386. * Attach non-embedded files by adding file contents inside boundaries.
  387. * @override
  388. * CUSTOM FIX: blob data support
  389. *
  390. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  391. * @return array An array of lines to add to the message
  392. */
  393. protected function _attachFiles($boundary = null) {
  394. if ($boundary === null) {
  395. $boundary = $this->_boundary;
  396. }
  397. $msg = array();
  398. foreach ($this->_attachments as $filename => $fileInfo) {
  399. if (!empty($fileInfo['contentId'])) {
  400. continue;
  401. }
  402. if (!empty($fileInfo['content'])) {
  403. $data = $fileInfo['content'];
  404. $data = chunk_split(base64_encode($data));
  405. } elseif (!empty($fileInfo['file'])) {
  406. $data = $this->_readFile($fileInfo['file']);
  407. } else {
  408. continue;
  409. }
  410. $msg[] = '--' . $boundary;
  411. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  412. $msg[] = 'Content-Transfer-Encoding: base64';
  413. if (
  414. !isset($fileInfo['contentDisposition']) ||
  415. $fileInfo['contentDisposition']
  416. ) {
  417. $msg[] = 'Content-Disposition: attachment; filename="' . $filename . '"';
  418. }
  419. $msg[] = '';
  420. $msg[] = $data;
  421. $msg[] = '';
  422. }
  423. return $msg;
  424. }
  425. /**
  426. * Add attachments to the email message
  427. * @override
  428. * CUSTOM FIX: blob data support
  429. *
  430. * Attachments can be defined in a few forms depending on how much control you need:
  431. *
  432. * Attach a single file:
  433. *
  434. * {{{
  435. * $email->attachments('path/to/file');
  436. * }}}
  437. *
  438. * Attach a file with a different filename:
  439. *
  440. * {{{
  441. * $email->attachments(array('custom_name.txt' => 'path/to/file.txt'));
  442. * }}}
  443. *
  444. * Attach a file and specify additional properties:
  445. *
  446. * {{{
  447. * $email->attachments(array('custom_name.png' => array(
  448. * 'file' => 'path/to/file',
  449. * 'mimetype' => 'image/png',
  450. * 'contentId' => 'abc123'
  451. * ));
  452. * }}}
  453. *
  454. * The `contentId` key allows you to specify an inline attachment. In your email text, you
  455. * can use `<img src="cid:abc123" />` to display the image inline.
  456. *
  457. * @param mixed $attachments String with the filename or array with filenames
  458. * @return mixed Either the array of attachments when getting or $this when setting.
  459. * @throws SocketException
  460. */
  461. public function attachments($attachments = null) {
  462. if ($attachments === null) {
  463. return $this->_attachments;
  464. }
  465. $attach = array();
  466. foreach ((array)$attachments as $name => $fileInfo) {
  467. if (!is_array($fileInfo)) {
  468. $fileInfo = array('file' => $fileInfo);
  469. }
  470. if (empty($fileInfo['content'])) {
  471. if (!isset($fileInfo['file'])) {
  472. throw new SocketException(__d('cake_dev', 'File not specified.'));
  473. }
  474. $fileInfo['file'] = realpath($fileInfo['file']);
  475. if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
  476. throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileInfo['file']));
  477. }
  478. if (is_int($name)) {
  479. $name = basename($fileInfo['file']);
  480. }
  481. }
  482. if (empty($fileInfo['mimetype'])) {
  483. $ext = pathinfo($name, PATHINFO_EXTENSION);
  484. $fileInfo['mimetype'] = $this->_getMimeByExtension($ext);
  485. }
  486. $attach[$name] = $fileInfo;
  487. }
  488. $this->_attachments = $attach;
  489. return $this;
  490. }
  491. /**
  492. * Get list of headers
  493. * @override
  494. * CUSTOM FIX: message id correctly set in CLI and can be passed in via domain()
  495. *
  496. * ### Includes:
  497. *
  498. * - `from`
  499. * - `replyTo`
  500. * - `readReceipt`
  501. * - `returnPath`
  502. * - `to`
  503. * - `cc`
  504. * - `bcc`
  505. * - `subject`
  506. *
  507. * @param array $include
  508. * @return array
  509. */
  510. public function getHeaders($include = array()) {
  511. if ($include == array_values($include)) {
  512. $include = array_fill_keys($include, true);
  513. }
  514. $defaults = array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), false);
  515. $include += $defaults;
  516. $headers = array();
  517. $relation = array(
  518. 'from' => 'From',
  519. 'replyTo' => 'Reply-To',
  520. 'readReceipt' => 'Disposition-Notification-To',
  521. 'returnPath' => 'Return-Path'
  522. );
  523. foreach ($relation as $var => $header) {
  524. if ($include[$var]) {
  525. $var = '_' . $var;
  526. $headers[$header] = current($this->_formatAddress($this->{$var}));
  527. }
  528. }
  529. if ($include['sender']) {
  530. if (key($this->_sender) === key($this->_from)) {
  531. $headers['Sender'] = '';
  532. } else {
  533. $headers['Sender'] = current($this->_formatAddress($this->_sender));
  534. }
  535. }
  536. foreach (array('to', 'cc', 'bcc') as $var) {
  537. if ($include[$var]) {
  538. $classVar = '_' . $var;
  539. $headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar}));
  540. }
  541. }
  542. $headers += $this->_headers;
  543. if (!isset($headers['X-Mailer'])) {
  544. $headers['X-Mailer'] = self::EMAIL_CLIENT;
  545. }
  546. if (!isset($headers['Date'])) {
  547. $headers['Date'] = date(DATE_RFC2822);
  548. }
  549. if ($this->_messageId !== false) {
  550. if ($this->_messageId === true) {
  551. $headers['Message-ID'] = '<' . str_replace('-', '', String::UUID()) . '@' . $this->_domain . '>';
  552. } else {
  553. $headers['Message-ID'] = $this->_messageId;
  554. }
  555. }
  556. if ($include['subject']) {
  557. $headers['Subject'] = $this->_subject;
  558. }
  559. $headers['MIME-Version'] = '1.0';
  560. if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
  561. $headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
  562. } elseif ($this->_emailFormat === 'text') {
  563. $headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
  564. } elseif ($this->_emailFormat === 'html') {
  565. $headers['Content-Type'] = 'text/html; charset=' . $this->charset;
  566. }
  567. $headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
  568. return $headers;
  569. }
  570. /**
  571. * Apply the config to an instance
  572. *
  573. * @param CakeEmail $obj CakeEmail
  574. * @param array $config
  575. * @return void
  576. * @throws ConfigureException When configuration file cannot be found, or is missing
  577. * the named config.
  578. */
  579. protected function _applyConfig($config) {
  580. if (is_string($config)) {
  581. if (!class_exists('EmailConfig') && !config('email')) {
  582. throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
  583. }
  584. $configs = new EmailConfig();
  585. if (!isset($configs->{$config})) {
  586. throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config));
  587. }
  588. $config = $configs->{$config};
  589. }
  590. $this->_config += $config;
  591. if (!empty($config['charset'])) {
  592. $this->charset = $config['charset'];
  593. }
  594. if (!empty($config['headerCharset'])) {
  595. $this->headerCharset = $config['headerCharset'];
  596. }
  597. if (empty($this->headerCharset)) {
  598. $this->headerCharset = $this->charset;
  599. }
  600. $simpleMethods = array(
  601. 'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
  602. 'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
  603. 'transport', 'emailFormat'
  604. );
  605. foreach ($simpleMethods as $method) {
  606. if (isset($config[$method])) {
  607. $this->$method($config[$method]);
  608. unset($config[$method]);
  609. }
  610. }
  611. if (isset($config['headers'])) {
  612. $this->setHeaders($config['headers']);
  613. unset($config['headers']);
  614. }
  615. if (array_key_exists('template', $config)) {
  616. $layout = false;
  617. if (array_key_exists('layout', $config)) {
  618. $layout = $config['layout'];
  619. unset($config['layout']);
  620. }
  621. $this->template($config['template'], $layout);
  622. unset($config['template']);
  623. }
  624. $this->transportClass()->config($config);
  625. }
  626. /**
  627. * Set the body of the mail as we send it.
  628. * Note: the text can be an array, each element will appear as a seperate line in the message body.
  629. *
  630. * LEAVE empty if you use $this->set() in combination with templates
  631. *
  632. * @param string/array: message
  633. * @return bool $success
  634. */
  635. public function send($message = null) {
  636. $this->_log = array(
  637. 'to' => $this->_to,
  638. 'from' => $this->_from,
  639. 'sender' => $this->_sender,
  640. 'replyTo' => $this->_replyTo,
  641. 'cc' => $this->_cc,
  642. 'subject' => $this->_subject,
  643. 'cc' => $this->_cc,
  644. 'transport' => $this->_transportName
  645. );
  646. # prep images for inline
  647. /*
  648. if ($this->_emailFormat !== 'text') {
  649. if ($message !== null) {
  650. $message = $this->_prepMessage($message);
  651. } else {
  652. $this->_htmlMessage = $this->_prepMessage($this->_htmlMessage);
  653. }
  654. }
  655. */
  656. try {
  657. $this->_debug = parent::send($message);
  658. } catch (Exception $e) {
  659. $this->_error = $e->getMessage();
  660. $this->_error .= ' (line '.$e->getLine().' in '.$e->getFile().')'.PHP_EOL.$e->getTraceAsString();
  661. if (!empty($this->_config['report'])) {
  662. $this->_logEmail();
  663. }
  664. return false;
  665. }
  666. if (!empty($this->_config['report'])) {
  667. $this->_logEmail();
  668. }
  669. return true;
  670. }
  671. protected function _prepMessage($text) {
  672. return $text;
  673. }
  674. /**
  675. * Returns the error if existent
  676. *
  677. * @return string
  678. */
  679. public function getError() {
  680. return $this->_error;
  681. }
  682. /**
  683. * Returns the debug content returned by send()
  684. *
  685. * @return string
  686. */
  687. public function getDebug() {
  688. return $this->_debug;
  689. }
  690. /**
  691. * Logs Email to type email
  692. * @return void
  693. */
  694. protected function _logEmail($append = null) {
  695. $res = $this->_log['transport'].
  696. ' - '.'TO:'.implode(',', array_keys($this->_log['to'])).
  697. '||FROM:'.implode(',', array_keys($this->_log['from'])).
  698. '||REPLY:'.implode(',', array_keys($this->_log['replyTo'])).
  699. '||S:'.$this->_log['subject'];
  700. $type = 'email';
  701. if (!empty($this->_error)) {
  702. $type = 'email_error';
  703. $res .= '||ERROR:' . $this->_error;
  704. }
  705. if ($append) {
  706. $res .= '||'.$append;
  707. }
  708. CakeLog::write($type, $res);
  709. }
  710. public function resetAndSet() {
  711. //$this->reset();
  712. $this->_to = array();
  713. $this->_cc = array();
  714. $this->_bcc = array();
  715. $this->_messageId = true;
  716. $this->_subject = '';
  717. $this->_headers = array();
  718. $this->_viewVars = array();
  719. $this->_textMessage = '';
  720. $this->_htmlMessage = '';
  721. $this->_message = '';
  722. $this->_attachments = array();
  723. $this->_error = null;
  724. $this->_debug = null;
  725. $this->from(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  726. if ($xMailer = Configure::read('Config.x-mailer')) {
  727. $this->addHeaders(array('X-Mailer'=>$xMailer));
  728. }
  729. //$this->_errors = array();
  730. //$this->charset($this->charset);
  731. //$this->sendAs($this->sendAs);
  732. //$this->layout($this->_layout);
  733. //$this->delivery($this->deliveryMethod);
  734. }
  735. }