PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ php-ppcms/includes/classes/lib.email.class.php

http://php-ppcms.googlecode.com/
PHP | 557 lines | 316 code | 85 blank | 156 comment | 102 complexity | 4134815be2e1004d248fa0d01f9405ad MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. * (c) 2009, jianyuzhu@gmail.com
  5. * All rights reserved
  6. * This script is part of the PPEMI project.
  7. ***************************************************************/
  8. class LibEmail {
  9. var $html;
  10. var $text;
  11. var $output;
  12. var $html_text;
  13. var $html_images;
  14. var $image_types;
  15. var $build_params;
  16. var $attachments;
  17. var $headers;
  18. //constructor
  19. function LibEmail($headers = '') {
  20. if( $headers == '') $headers = array();
  21. $this->html_images = array();
  22. $this->headers = array();
  23. if( CONFIG_EMAIL_LINEFEED == 'CRLF') {
  24. $this->lf = "\r\n";
  25. } else {
  26. $this->lf = "\n";
  27. }
  28. /**
  29. * If you want the auto load functionality
  30. * to find other mime-image/file types, add the
  31. * extension and content type here.
  32. */
  33. $this->image_types = array('gif' => 'image/gif',
  34. 'jpg' => 'image/jpeg',
  35. 'jpeg' => 'image/jpeg',
  36. 'jpe' => 'image/jpeg',
  37. 'bmp' => 'image/bmp',
  38. 'png' => 'image/png',
  39. 'tif' => 'image/tiff',
  40. 'tiff' => 'image/tiff',
  41. 'swf' => 'application/x-shockwave-flash');
  42. $this->build_params['html_encoding'] = 'quoted-printable';
  43. $this->build_params['text_encoding'] = '7bit';
  44. //Make sure EMAIL_CHARSET is defined for big5 or gb2312.
  45. if( defined('CONFIG_EMAIL_CHARSET')) {
  46. $this->build_params['html_charset'] = constant('CONFIG_EMAIL_CHARSET');
  47. $this->build_params['text_charset'] = constant('CONFIG_EMAIL_CHARSET');
  48. } else{
  49. $this->build_params['html_charset'] = constant('CONFIG_CHARSET');
  50. $this->build_params['text_charset'] = constant('CONFIG_CHARSET');
  51. }
  52. $this->build_params['text_wrap'] = 998;
  53. /*
  54. * Make sure the MIME version header is first.
  55. */
  56. $this->headers[] = 'MIME-Version: 1.0';
  57. reset($headers);
  58. while(list(,$value) = each($headers)) {
  59. if( util_not_null($value) ) {
  60. $this->headers[] = $value;
  61. }
  62. }
  63. }
  64. /**
  65. * This function will read a file in
  66. * from a supplied filename and return
  67. * it. This can then be given as the first
  68. * argument of the the functions
  69. * add_html_image() or add_attachment().
  70. */
  71. function get_file($filename) {
  72. $return = '';
  73. if( $fp = fopen($filename, 'rb')) {
  74. while (!feof($fp)) {
  75. $return .= fread($fp, 1024);
  76. }
  77. fclose($fp);
  78. return $return;
  79. } else {
  80. return false;
  81. }
  82. }
  83. /**
  84. * Function for extracting images from
  85. * html source. This function will look
  86. * through the html code supplied by add_html()
  87. * and find any file that ends in one of the
  88. * extensions defined in $obj->image_types.
  89. * If the file exists it will read it in and
  90. * embed it, (not an attachment).
  91. *
  92. * Function contributed by Dan Allen
  93. */
  94. function find_html_images($images_dir) {
  95. // Build the list of image extensions
  96. while(list($key, ) = each($this->image_types)) {
  97. $extensions[] = $key;
  98. }
  99. preg_match_all('/"([^"]+\.(' . implode('|', $extensions).'))"/Ui', $this->html, $images);
  100. for ($i=0; $i<count($images[1]); $i++) {
  101. if( file_exists($images_dir . $images[1][$i]) ) {
  102. $html_images[] = $images[1][$i];
  103. $this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html);
  104. }
  105. }
  106. if( util_not_null($html_images)) {
  107. // If duplicate images are embedded, they may show up as attachments, so remove them.
  108. $html_images = array_unique($html_images);
  109. sort($html_images);
  110. for ($i=0; $i<count($html_images); $i++) {
  111. if( $image = $this->get_file($images_dir . $html_images[$i]) ) {
  112. $content_type = $this->image_types[substr($html_images[$i], strrpos($html_images[$i], '.') + 1)];
  113. $this->add_html_image($image, basename($html_images[$i]), $content_type);
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Adds plain text. Use this function
  120. * when NOT sending html email
  121. */
  122. function add_text($text = '') {
  123. $this->text = util_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
  124. }
  125. /**
  126. * Adds a html part to the mail.
  127. * Also replaces image names with
  128. * content-id's.
  129. */
  130. function add_html($html, $text = NULL, $images_dir = NULL) {
  131. $this->html = util_convert_linefeeds(array("\r\n", "\n", "\r"), '<br>', $html);
  132. $this->html_text = util_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
  133. if( isset($images_dir)) $this->find_html_images($images_dir);
  134. }
  135. /**
  136. * Adds an image to the list of embedded
  137. * images.
  138. */
  139. function add_html_image($file, $name = '', $c_type='application/octet-stream') {
  140. $this->html_images[] = array('body' => $file,
  141. 'name' => $name,
  142. 'c_type' => $c_type,
  143. 'cid' => md5(uniqid(time())));
  144. }
  145. /**
  146. * Adds a file to the list of attachments.
  147. */
  148. function add_attachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64') {
  149. $this->attachments[] = array('body' => $file,
  150. 'name' => $name,
  151. 'c_type' => $c_type,
  152. 'encoding' => $encoding);
  153. }
  154. /**
  155. * Adds a text subpart to a mime_part object
  156. */
  157. /* HPDL PHP3 */
  158. //function &add_text_part(&$obj, $text) {
  159. function add_text_part(&$obj, $text) {
  160. $params['content_type'] = 'text/plain';
  161. $params['encoding'] = $this->build_params['text_encoding'];
  162. $params['charset'] = $this->build_params['text_charset'];
  163. if( is_object($obj)) {
  164. return $obj->addSubpart($text, $params);
  165. } else {
  166. return new LibEmailMime($text, $params);
  167. }
  168. }
  169. /**
  170. * Adds a html subpart to a mime_part object
  171. */
  172. /* HPDL PHP3 */
  173. //function &add_html_part(&$obj) {
  174. function add_html_part(&$obj) {
  175. $params['content_type'] = 'text/html';
  176. $params['encoding'] = $this->build_params['html_encoding'];
  177. $params['charset'] = $this->build_params['html_charset'];
  178. if( is_object($obj)) {
  179. return $obj->addSubpart($this->html, $params);
  180. } else {
  181. return new LibEmailMime($this->html, $params);
  182. }
  183. }
  184. /**
  185. * Starts a message with a mixed part
  186. */
  187. /* HPDL PHP3 */
  188. //function &add_mixed_part() {
  189. function add_mixed_part() {
  190. $params['content_type'] = 'multipart/mixed';
  191. return new LibEmailMime('', $params);
  192. }
  193. /**
  194. * Adds an alternative part to a mime_part object
  195. */
  196. /* HPDL PHP3 */
  197. //function &add_alternative_part(&$obj) {
  198. function add_alternative_part(&$obj) {
  199. $params['content_type'] = 'multipart/alternative';
  200. if( is_object($obj)) {
  201. return $obj->addSubpart('', $params);
  202. } else {
  203. return new LibEmailMime('', $params);
  204. }
  205. }
  206. /**
  207. * Adds a html subpart to a mime_part object
  208. */
  209. /* HPDL PHP3 */
  210. //function &add_related_part(&$obj) {
  211. function add_related_part(&$obj) {
  212. $params['content_type'] = 'multipart/related';
  213. if( is_object($obj)) {
  214. return $obj->addSubpart('', $params);
  215. } else {
  216. return new LibEmailMime('', $params);
  217. }
  218. }
  219. /**
  220. * Adds an html image subpart to a mime_part object
  221. */
  222. /* HPDL PHP3 */
  223. //function &add_html_image_part(&$obj, $value) {
  224. function add_html_image_part(&$obj, $value) {
  225. $params['content_type'] = $value['c_type'];
  226. $params['encoding'] = 'base64';
  227. $params['disposition'] = 'inline';
  228. $params['dfilename'] = $value['name'];
  229. $params['cid'] = $value['cid'];
  230. $obj->addSubpart($value['body'], $params);
  231. }
  232. /**
  233. * Adds an attachment subpart to a mime_part object
  234. */
  235. /* HPDL PHP3 */
  236. //function &add_attachment_part(&$obj, $value) {
  237. function add_attachment_part(&$obj, $value) {
  238. $params['content_type'] = $value['c_type'];
  239. $params['encoding'] = $value['encoding'];
  240. $params['disposition'] = 'attachment';
  241. $params['dfilename'] = $value['name'];
  242. $obj->addSubpart($value['body'], $params);
  243. }
  244. /**
  245. * Builds the multipart message from the
  246. * list ($this->_parts). $params is an
  247. * array of parameters that shape the building
  248. * of the message. Currently supported are:
  249. *
  250. * $params['html_encoding'] - The type of encoding to use on html. Valid options are
  251. * "7bit", "quoted-printable" or "base64" (all without quotes).
  252. * 7bit is EXPRESSLY NOT RECOMMENDED. Default is quoted-printable
  253. * $params['text_encoding'] - The type of encoding to use on plain text Valid options are
  254. * "7bit", "quoted-printable" or "base64" (all without quotes).
  255. * Default is 7bit
  256. * $params['text_wrap'] - The character count at which to wrap 7bit encoded data.
  257. * Default this is 998.
  258. * $params['html_charset'] - The character set to use for a html section.
  259. * Default is iso-8859-1
  260. * $params['text_charset'] - The character set to use for a text section.
  261. * - Default is iso-8859-1
  262. */
  263. /* HPDL PHP3 */
  264. // function build_message($params = array()) {
  265. function build_message($params = '') {
  266. if( $params == '' ) $params = array();
  267. if( count($params) > 0 ) {
  268. reset($params);
  269. while(list($key, $value) = each($params)) {
  270. $this->build_params[$key] = $value;
  271. }
  272. }
  273. if( util_not_null($this->html_images) ) {
  274. reset($this->html_images);
  275. while(list(,$value) = each($this->html_images)) {
  276. $this->html = str_replace($value['name'], 'cid:' . $value['cid'], $this->html);
  277. }
  278. }
  279. $null = NULL;
  280. $attachments = ((util_not_null($this->attachments)) ? true : false);
  281. $html_images = ((util_not_null($this->html_images)) ? true : false);
  282. $html = ((util_not_null($this->html)) ? true : false);
  283. $text = ((util_not_null($this->text)) ? true : false);
  284. switch(true) {
  285. case (($text == true) && ($attachments == false)):
  286. /* HPDL PHP3 */
  287. // $message =& $this->add_text_part($null, $this->text);
  288. $message = $this->add_text_part($null, $this->text);
  289. break;
  290. case (($text == false) && ($attachments == true) && ($html == false)):
  291. /* HPDL PHP3 */
  292. // $message =& $this->add_mixed_part();
  293. $message = $this->add_mixed_part();
  294. for ($i=0; $i<count($this->attachments); $i++) {
  295. $this->add_attachment_part($message, $this->attachments[$i]);
  296. }
  297. break;
  298. case (($text == true) && ($attachments == true)):
  299. /* HPDL PHP3 */
  300. // $message =& $this->add_mixed_part();
  301. $message = $this->add_mixed_part();
  302. $this->add_text_part($message, $this->text);
  303. for ($i=0; $i<count($this->attachments); $i++) {
  304. $this->add_attachment_part($message, $this->attachments[$i]);
  305. }
  306. break;
  307. case (($html == true) && ($attachments == false) && ($html_images == false)):
  308. if( util_not_null($this->html_text)) {
  309. /* HPDL PHP3 */
  310. // $message =& $this->add_alternative_part($null);
  311. $message = $this->add_alternative_part($null);
  312. $this->add_text_part($message, $this->html_text);
  313. $this->add_html_part($message);
  314. } else {
  315. /* HPDL PHP3 */
  316. // $message =& $this->add_html_part($null);
  317. $message = $this->add_html_part($null);
  318. }
  319. break;
  320. case (($html == true) && ($attachments == false) && ($html_images == true)):
  321. if( util_not_null($this->html_text)) {
  322. /* HPDL PHP3 */
  323. // $message =& $this->add_alternative_part($null);
  324. $message = $this->add_alternative_part($null);
  325. $this->add_text_part($message, $this->html_text);
  326. /* HPDL PHP3 */
  327. // $related =& $this->add_related_part($message);
  328. $related = $this->add_related_part($message);
  329. } else {
  330. /* HPDL PHP3 */
  331. // $message =& $this->add_related_part($null);
  332. // $related =& $message;
  333. $message = $this->add_related_part($null);
  334. $related = $message;
  335. }
  336. $this->add_html_part($related);
  337. for ($i=0; $i<count($this->html_images); $i++) {
  338. $this->add_html_image_part($related, $this->html_images[$i]);
  339. }
  340. break;
  341. case (($html == true) && ($attachments == true) && ($html_images == false)):
  342. /* HPDL PHP3 */
  343. // $message =& $this->add_mixed_part();
  344. $message = $this->add_mixed_part();
  345. if( util_not_null($this->html_text)) {
  346. /* HPDL PHP3 */
  347. // $alt =& $this->add_alternative_part($message);
  348. $alt = $this->add_alternative_part($message);
  349. $this->add_text_part($alt, $this->html_text);
  350. $this->add_html_part($alt);
  351. } else {
  352. $this->add_html_part($message);
  353. }
  354. for ($i=0; $i<count($this->attachments); $i++) {
  355. $this->add_attachment_part($message, $this->attachments[$i]);
  356. }
  357. break;
  358. case (($html == true) && ($attachments == true) && ($html_images == true)):
  359. /* HPDL PHP3 */
  360. // $message =& $this->add_mixed_part();
  361. $message = $this->add_mixed_part();
  362. if( util_not_null($this->html_text)) {
  363. /* HPDL PHP3 */
  364. // $alt =& $this->add_alternative_part($message);
  365. $alt = $this->add_alternative_part($message);
  366. $this->add_text_part($alt, $this->html_text);
  367. /* HPDL PHP3 */
  368. // $rel =& $this->add_related_part($alt);
  369. $rel = $this->add_related_part($alt);
  370. } else {
  371. /* HPDL PHP3 */
  372. // $rel =& $this->add_related_part($message);
  373. $rel = $this->add_related_part($message);
  374. }
  375. $this->add_html_part($rel);
  376. for ($i=0; $i<count($this->html_images); $i++) {
  377. $this->add_html_image_part($rel, $this->html_images[$i]);
  378. }
  379. for ($i=0; $i<count($this->attachments); $i++) {
  380. $this->add_attachment_part($message, $this->attachments[$i]);
  381. }
  382. break;
  383. }
  384. if( (isset($message)) && (is_object($message)) ) {
  385. $output = $message->encode();
  386. $this->output = $output['body'];
  387. reset($output['headers']);
  388. while(list($key, $value) = each($output['headers'])) {
  389. $headers[] = $key . ': ' . $value;
  390. }
  391. $this->headers = array_merge($this->headers, $headers);
  392. return true;
  393. } else {
  394. return false;
  395. }
  396. }
  397. /**
  398. * Sends the mail.
  399. */
  400. function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
  401. $to = (($to_name != '') ? '"' . $to_name . '" <' . $to_addr . '>' : $to_addr);
  402. $from = (($from_name != '') ? '"' . $from_name . '" <' . $from_addr . '>' : $from_addr);
  403. if( is_string($headers)) {
  404. $headers = explode($this->lf, trim($headers));
  405. }
  406. for ($i=0; $i<count($headers); $i++) {
  407. if( is_array($headers[$i]) ) {
  408. for ($j=0; $j<count($headers[$i]); $j++) {
  409. if( $headers[$i][$j] != '') {
  410. $xtra_headers[] = $headers[$i][$j];
  411. }
  412. }
  413. }
  414. if( $headers[$i] != '') {
  415. $xtra_headers[] = $headers[$i];
  416. }
  417. }
  418. if( !isset($xtra_headers)) {
  419. $xtra_headers = array();
  420. }
  421. if( CONFIG_EMAIL_TRANSPORT == 'smtp' ) {
  422. return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
  423. } else {
  424. return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
  425. }
  426. }
  427. /**
  428. * Use this method to return the email
  429. * in message/rfc822 format. Useful for
  430. * adding an email to another email as
  431. * an attachment. there's a commented
  432. * out example in example.php.
  433. *
  434. * string get_rfc822(string To name,
  435. * string To email,
  436. * string From name,
  437. * string From email,
  438. * [string Subject,
  439. * string Extra headers])
  440. */
  441. function get_rfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
  442. // Make up the date header as according to RFC822
  443. $date = 'Date: ' . date('D, d M y H:i:s');
  444. $to = (($to_name != '') ? 'To: "' . $to_name . '" <' . $to_addr . '>' : 'To: ' . $to_addr);
  445. $from = (($from_name != '') ? 'From: "' . $from_name . '" <' . $from_addr . '>' : 'From: ' . $from_addr);
  446. if( is_string($subject)) {
  447. $subject = 'Subject: ' . $subject;
  448. }
  449. if( is_string($headers)) {
  450. $headers = explode($this->lf, trim($headers));
  451. }
  452. for ($i=0; $i<count($headers); $i++) {
  453. if( is_array($headers[$i]) ) {
  454. for ($j=0; $j<count($headers[$i]); $j++) {
  455. if( $headers[$i][$j] != '') {
  456. $xtra_headers[] = $headers[$i][$j];
  457. }
  458. }
  459. }
  460. if( $headers[$i] != '') {
  461. $xtra_headers[] = $headers[$i];
  462. }
  463. }
  464. if( !isset($xtra_headers)) {
  465. $xtra_headers = array();
  466. }
  467. $headers = array_merge($this->headers, $xtra_headers);
  468. return $date . $this->lf . $from . $this->lf . $to . $this->lf . $subject . $this->lf . implode($this->lf, $headers) . $this->lf . $this->lf . $this->output;
  469. }
  470. }
  471. //
  472. ?>