EmailLib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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
  10. *
  11. * @author Mark Scherer
  12. * @license MIT
  13. * 2011-10-31 ms
  14. */
  15. class EmailLib extends CakeEmail {
  16. public $error = '';
  17. protected $_log = null;
  18. protected $_debug = null;
  19. # for multiple emails, just adjust these "default" values "on the fly"
  20. public $deliveryMethod = 'mail';
  21. public $layout = 'external'; # usually 'external' (internal for admins only)
  22. # with presets, only TO/FROM (depends), subject and message has to be set
  23. protected $presets = array(
  24. 'user' => '',
  25. 'admin' => '',
  26. );
  27. protected $options = array();
  28. protected $complex = null; # if Controller is available, and layout/elements can be switched...
  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->to(Configure::read('Config.admin_email'));
  48. $instance->from(Configure::read('Config.admin_email'));
  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. if (true || $send === true) {
  59. return $instance->send($message);
  60. }
  61. return $instance;
  62. }
  63. public function layout($layout = false) {
  64. if ($layout !== false) {
  65. $this->_layout = $layout;
  66. }
  67. return $this;
  68. }
  69. /**
  70. * @param string $file: absolute path
  71. * @param string $filename (optional)
  72. * 2011-11-02 ms
  73. */
  74. public function addAttachment($file, $name = null) {
  75. if (!empty($name)) {
  76. return $this->addAttachments(array($name=>$file));
  77. }
  78. return $this->addAttachments($file);
  79. }
  80. /**
  81. * @param string $file: absolute path
  82. * @param string $filename (optional)
  83. * @return mixed ressource $EmailLib or string $contentId
  84. * 2011-11-02 ms
  85. */
  86. public function addEmbeddedAttachment($file, $name = null, $contentId = null) {
  87. $fileInfo = array();
  88. $fileInfo['file'] = realpath($file);
  89. $fileInfo['mimetype'] = $this->_getMime($file);
  90. $fileInfo['contentId'] = $contentId ? $contentId : str_replace('-', '', String::uuid()) . '@' . env('HTTP_HOST');
  91. if (empty($name)) {
  92. $name = basename($file);
  93. }
  94. $file = array($name=>$fileInfo);
  95. $res = $this->addAttachments($file);
  96. if ($contentId === null) {
  97. return $fileInfo['contentId'];
  98. }
  99. return $res;
  100. }
  101. protected function _getMime($filename) {
  102. if (function_exists('finfo_open')) {
  103. $finfo = finfo_open(FILEINFO_MIME);
  104. $mimetype = finfo_file($finfo, $filename);
  105. finfo_close($finfo);
  106. } else {
  107. //TODO: improve
  108. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  109. switch ($ext) {
  110. case "zip": $mime="application/zip"; break;
  111. case "ez": $mime="application/andrew-inset"; break;
  112. case "hqx": $mime="application/mac-binhex40"; break;
  113. case "cpt": $mime="application/mac-compactpro"; break;
  114. case "doc": $mime="application/msword"; break;
  115. case "bin": $mime="application/octet-stream"; break;
  116. case "dms": $mime="application/octet-stream"; break;
  117. case "lha": $mime="application/octet-stream"; break;
  118. case "lzh": $mime="application/octet-stream"; break;
  119. case "exe": $mime="application/octet-stream"; break;
  120. case "class": $mime="application/octet-stream"; break;
  121. case "so": $mime="application/octet-stream"; break;
  122. case "dll": $mime="application/octet-stream"; break;
  123. case "oda": $mime="application/oda"; break;
  124. case "pdf": $mime="application/pdf"; break;
  125. case "ai": $mime="application/postscript"; break;
  126. case "eps": $mime="application/postscript"; break;
  127. case "ps": $mime="application/postscript"; break;
  128. case "smi": $mime="application/smil"; break;
  129. case "smil": $mime="application/smil"; break;
  130. case "xls": $mime="application/vnd.ms-excel"; break;
  131. case "ppt": $mime="application/vnd.ms-powerpoint"; break;
  132. case "wbxml": $mime="application/vnd.wap.wbxml"; break;
  133. case "wmlc": $mime="application/vnd.wap.wmlc"; break;
  134. case "wmlsc": $mime="application/vnd.wap.wmlscriptc"; break;
  135. case "bcpio": $mime="application/x-bcpio"; break;
  136. case "vcd": $mime="application/x-cdlink"; break;
  137. case "pgn": $mime="application/x-chess-pgn"; break;
  138. case "cpio": $mime="application/x-cpio"; break;
  139. case "csh": $mime="application/x-csh"; break;
  140. case "dcr": $mime="application/x-director"; break;
  141. case "dir": $mime="application/x-director"; break;
  142. case "dxr": $mime="application/x-director"; break;
  143. case "dvi": $mime="application/x-dvi"; break;
  144. case "spl": $mime="application/x-futuresplash"; break;
  145. case "gtar": $mime="application/x-gtar"; break;
  146. case "hdf": $mime="application/x-hdf"; break;
  147. case "js": $mime="application/x-javascript"; break;
  148. case "skp": $mime="application/x-koan"; break;
  149. case "skd": $mime="application/x-koan"; break;
  150. case "skt": $mime="application/x-koan"; break;
  151. case "skm": $mime="application/x-koan"; break;
  152. case "latex": $mime="application/x-latex"; break;
  153. case "nc": $mime="application/x-netcdf"; break;
  154. case "cdf": $mime="application/x-netcdf"; break;
  155. case "sh": $mime="application/x-sh"; break;
  156. case "shar": $mime="application/x-shar"; break;
  157. case "swf": $mime="application/x-shockwave-flash"; break;
  158. case "sit": $mime="application/x-stuffit"; break;
  159. case "sv4cpio": $mime="application/x-sv4cpio"; break;
  160. case "sv4crc": $mime="application/x-sv4crc"; break;
  161. case "tar": $mime="application/x-tar"; break;
  162. case "tcl": $mime="application/x-tcl"; break;
  163. case "tex": $mime="application/x-tex"; break;
  164. case "texinfo": $mime="application/x-texinfo"; break;
  165. case "texi": $mime="application/x-texinfo"; break;
  166. case "t": $mime="application/x-troff"; break;
  167. case "tr": $mime="application/x-troff"; break;
  168. case "roff": $mime="application/x-troff"; break;
  169. case "man": $mime="application/x-troff-man"; break;
  170. case "me": $mime="application/x-troff-me"; break;
  171. case "ms": $mime="application/x-troff-ms"; break;
  172. case "ustar": $mime="application/x-ustar"; break;
  173. case "src": $mime="application/x-wais-source"; break;
  174. case "xhtml": $mime="application/xhtml+xml"; break;
  175. case "xht": $mime="application/xhtml+xml"; break;
  176. case "zip": $mime="application/zip"; break;
  177. case "au": $mime="audio/basic"; break;
  178. case "snd": $mime="audio/basic"; break;
  179. case "mid": $mime="audio/midi"; break;
  180. case "midi": $mime="audio/midi"; break;
  181. case "kar": $mime="audio/midi"; break;
  182. case "mpga": $mime="audio/mpeg"; break;
  183. case "mp2": $mime="audio/mpeg"; break;
  184. case "mp3": $mime="audio/mpeg"; break;
  185. case "aif": $mime="audio/x-aiff"; break;
  186. case "aiff": $mime="audio/x-aiff"; break;
  187. case "aifc": $mime="audio/x-aiff"; break;
  188. case "m3u": $mime="audio/x-mpegurl"; break;
  189. case "ram": $mime="audio/x-pn-realaudio"; break;
  190. case "rm": $mime="audio/x-pn-realaudio"; break;
  191. case "rpm": $mime="audio/x-pn-realaudio-plugin"; break;
  192. case "ra": $mime="audio/x-realaudio"; break;
  193. case "wav": $mime="audio/x-wav"; break;
  194. case "pdb": $mime="chemical/x-pdb"; break;
  195. case "xyz": $mime="chemical/x-xyz"; break;
  196. case "bmp": $mime="image/bmp"; break;
  197. case "gif": $mime="image/gif"; break;
  198. case "ief": $mime="image/ief"; break;
  199. case "jpeg": $mime="image/jpeg"; break;
  200. case "jpg": $mime="image/jpeg"; break;
  201. case "jpe": $mime="image/jpeg"; break;
  202. case "png": $mime="image/png"; break;
  203. case "tiff": $mime="image/tiff"; break;
  204. case "tif": $mime="image/tiff"; break;
  205. case "djvu": $mime="image/vnd.djvu"; break;
  206. case "djv": $mime="image/vnd.djvu"; break;
  207. case "wbmp": $mime="image/vnd.wap.wbmp"; break;
  208. case "ras": $mime="image/x-cmu-raster"; break;
  209. case "pnm": $mime="image/x-portable-anymap"; break;
  210. case "pbm": $mime="image/x-portable-bitmap"; break;
  211. case "pgm": $mime="image/x-portable-graymap"; break;
  212. case "ppm": $mime="image/x-portable-pixmap"; break;
  213. case "rgb": $mime="image/x-rgb"; break;
  214. case "xbm": $mime="image/x-xbitmap"; break;
  215. case "xpm": $mime="image/x-xpixmap"; break;
  216. case "xwd": $mime="image/x-xwindowdump"; break;
  217. case "igs": $mime="model/iges"; break;
  218. case "iges": $mime="model/iges"; break;
  219. case "msh": $mime="model/mesh"; break;
  220. case "mesh": $mime="model/mesh"; break;
  221. case "silo": $mime="model/mesh"; break;
  222. case "wrl": $mime="model/vrml"; break;
  223. case "vrml": $mime="model/vrml"; break;
  224. case "css": $mime="text/css"; break;
  225. case "html": $mime="text/html"; break;
  226. case "htm": $mime="text/html"; break;
  227. case "asc": $mime="text/plain"; break;
  228. case "txt": $mime="text/plain"; break;
  229. case "rtx": $mime="text/richtext"; break;
  230. case "rtf": $mime="text/rtf"; break;
  231. case "sgml": $mime="text/sgml"; break;
  232. case "sgm": $mime="text/sgml"; break;
  233. case "tsv": $mime="text/tab-separated-values"; break;
  234. case "wml": $mime="text/vnd.wap.wml"; break;
  235. case "wmls": $mime="text/vnd.wap.wmlscript"; break;
  236. case "etx": $mime="text/x-setext"; break;
  237. case "xml": $mime="text/xml"; break;
  238. case "xsl": $mime="text/xml"; break;
  239. case "mpeg": $mime="video/mpeg"; break;
  240. case "mpg": $mime="video/mpeg"; break;
  241. case "mpe": $mime="video/mpeg"; break;
  242. case "qt": $mime="video/quicktime"; break;
  243. case "mov": $mime="video/quicktime"; break;
  244. case "mxu": $mime="video/vnd.mpegurl"; break;
  245. case "avi": $mime="video/x-msvideo"; break;
  246. case "movie": $mime="video/x-sgi-movie"; break;
  247. case "asf": $mime="video/x-ms-asf"; break;
  248. case "asx": $mime="video/x-ms-asf"; break;
  249. case "wm": $mime="video/x-ms-wm"; break;
  250. case "wmv": $mime="video/x-ms-wmv"; break;
  251. case "wvx": $mime="video/x-ms-wvx"; break;
  252. case "ice": $mime="x-conference/x-cooltalk"; break;
  253. }
  254. if (empty($mime)) {
  255. $mime = 'application/octet-stream';
  256. }
  257. $mimetype = $mime;
  258. }
  259. return $mimetype;
  260. }
  261. public function preset($type = null) {
  262. # testing only:
  263. //pr ($this->Email);
  264. //pr ($this);
  265. }
  266. /**
  267. * test for a specific error
  268. * @param code: 4xx, 5xx, 5xx 5.1.1, 5xx 5.2.2, ...
  269. * @return boolean $status (TRUE only if this specific error occured)
  270. * 2010-06-08 ms
  271. */
  272. public function hasError($code) {
  273. if (!empty($this->errors)) {
  274. foreach ($this->errors as $error) {
  275. if (substr($error, 0, strlen($code)) == (string)$code) {
  276. return true;
  277. }
  278. }
  279. }
  280. return false;
  281. }
  282. public function validates() {
  283. if (!empty($this->Email->subject)) {
  284. return true;
  285. }
  286. return false;
  287. }
  288. /**
  289. * Set the body of the mail as we send it.
  290. * Note: the text can be an array, each element will appear as a seperate line in the message body.
  291. * @param string/array: message
  292. * LEAVE empty if you use $this->set() in combination with templates
  293. */
  294. public function send($message = null) {
  295. $this->_log = array(
  296. 'to' => $this->_to,
  297. 'from' => $this->_from,
  298. 'sender' => $this->_sender,
  299. 'replyTo' => $this->_replyTo,
  300. 'cc' => $this->_cc,
  301. 'subject' => $this->_subject,
  302. 'cc' => $this->_cc,
  303. 'transport' => $this->_transportName
  304. );
  305. # prep images for inline
  306. /*
  307. if ($this->_emailFormat !== 'text') {
  308. if ($message !== null) {
  309. $message = $this->_prepMessage($message);
  310. } else {
  311. $this->_htmlMessage = $this->_prepMessage($this->_htmlMessage);
  312. }
  313. }
  314. */
  315. try {
  316. $this->_debug = parent::send($message);
  317. } catch (Exception $e) {
  318. $this->error = $e->getMessage();
  319. if (Configure::read('debug') > 0 || env('REMOTE_ADDR') == '127.0.0.1') {
  320. $this->error .= ' (line '.$e->getLine().' in '.$e->getFile().')'.PHP_EOL.$e->getTraceAsString();
  321. }
  322. return false;
  323. }
  324. if (!empty($this->_config['report'])) {
  325. $this->_logEmail();
  326. }
  327. return true;
  328. }
  329. protected function _prepMessage($text) {
  330. return $text;
  331. }
  332. /**
  333. * @return string
  334. */
  335. public function getError() {
  336. return $this->error;
  337. }
  338. protected function _logEmail($append = null) {
  339. $res = $this->_log['transport'].
  340. ' - '.'TO:'.implode(',', array_keys($this->_log['to'])).
  341. '||FROM:'.implode(',', array_keys($this->_log['from'])).
  342. '||REPLY:'.implode(',', array_keys($this->_log['replyTo'])).
  343. '||S:'.$this->_log['subject'];
  344. $type = 'email';
  345. if (!empty($this->error)) {
  346. $type = 'email_error';
  347. $res .= '||ERROR:' . $this->error;
  348. }
  349. if ($append) {
  350. $res .= '||'.$append;
  351. }
  352. CakeLog::write($type, $res);
  353. }
  354. /**
  355. * toggle debug mode
  356. * 2011-05-27 ms
  357. */
  358. public function debug($mode = null) {
  359. if ($mode) {
  360. $this->debug = true;
  361. //$this->delivery('debug');
  362. } else {
  363. $this->debug = false;
  364. //$this->delivery($this->deliveryMethod);
  365. }
  366. }
  367. public function resetAndSet() {
  368. $this->_to = array();
  369. $this->_cc = array();
  370. $this->_bcc = array();
  371. $this->_messageId = true;
  372. $this->_subject = '';
  373. $this->_headers = array();
  374. $this->_viewVars = array();
  375. $this->_textMessage = '';
  376. $this->_htmlMessage = '';
  377. $this->_message = '';
  378. $this->_attachments = array();
  379. $this->from(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  380. if ($xMailer = Configure::read('Config.x-mailer')) {
  381. $this->addHeaders(array('X-Mailer'=>$xMailer));
  382. }
  383. //$this->errors = array();
  384. //$this->charset($this->charset);
  385. //$this->sendAs($this->sendAs);
  386. //$this->layout($this->layout);
  387. //$this->delivery($this->deliveryMethod);
  388. }
  389. public function reset() {
  390. parent::reset();
  391. $this->error = '';
  392. $this->_debug = null;
  393. }
  394. public function flashDebug() {
  395. $info = $this->Email->Controller->Session->read('Message.email.message');
  396. if (empty($info)) {
  397. $info = $this->Email->Controller->Session->read('Message.email.message');
  398. }
  399. $info .= BR;
  400. $info .= h($this->_logMessage());
  401. $this->Email->Controller->Session->delete('Message.email');
  402. $this->Email->Controller->flashMessage($info, 'info');
  403. if (!empty($this->errors)) {
  404. $this->Email->Controller->flashMessage(implode(BR, $this->errors), 'error');
  405. }
  406. }
  407. }