PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/phplist/admin/class.html.mime.mail.inc

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