Index: simplenews.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v
retrieving revision 1.4
diff -u -F^f -r1.4 simplenews.module
--- simplenews.module	20 Jul 2005 11:16:11 -0000	1.4
+++ simplenews.module	29 Jul 2005 05:56:12 -0000
@@ -435,7 +435,7 @@ function _sn_send($timer = FALSE) {
     else {
       // first do node_prepare() to allow PHP-code to be parsed, then strip remaining HTML-tags
       $node = module_invoke('node', 'prepare', $node, FALSE);
-      $node->body = strip_tags($node->body);
+      $node->body = sn_html_to_text($node->body);
     }
     $address_default = variable_get('site_mail', ini_get('sendmail_from'));
     $name_default = variable_get('site_name', 'drupal');
@@ -484,6 +484,75 @@ function _sn_send($timer = FALSE) {
   }
 }
 
+function sn_html_to_text($txt) {
+  $pattern = '@(<a href="(.+?)">(.+?)</a>)@ei';
+  $txt = preg_replace($pattern, "'\\3 ['. _sn_mail_urls('\\2') .']'", $txt);
+  $urls = _sn_mail_urls();
+  if (count($urls)) {
+      $txt .= "\n";
+      for ($max = count($urls); $i < $max; $i++) {
+        $txt .= '['. ($i + 1) .'] '. $urls[$i] ."\n";
+      }
+  }
+  _sn_mail_urls(0, TRUE);
+  $txt = strip_tags($txt);
+  $txt = sn_entities_to_utf8($txt);
+  return wordwrap($txt, 72);
+}
+
+function sn_entities_to_utf8($text) {
+  static $table;
+  // We store named entities in a table for quick processing.  
+  if (!isset($table)) {
+    // Get all named HTML entities.
+    $table = array_flip(get_html_translation_table(HTML_ENTITIES, $special));
+    // PHP gives us Windows-1252/ISO-8859-1 data, we need UTF-8.
+    $table = array_map('utf8_encode', $table);
+  }
+  $text = strtr($text, $table);
+
+  // Any remaining entities are numerical. Use a regexp to replace them.
+  return preg_replace('/&#(x?)([A-Za-z0-9]+);/e', 'sn_entity_to_utf8("$1", "$2")', $text);
+}
+
+function sn_entity_to_utf8($hex, $codepoint) {
+  if ($hex != '') {
+    $codepoint = base_convert($codepoint, 16, 10);
+  }
+  if ($codepoint < 0x80) {
+    return chr($codepoint);
+  }
+  else if ($codepoint < 0x800) {
+    return chr(0xC0 | ($codepoint >> 6))
+         . chr(0x80 | ($codepoint & 0x3F));
+  }
+  else if ($codepoint < 0x10000) {
+    return chr(0xE0 | ( $codepoint >> 12))
+         . chr(0x80 | (($codepoint >> 6) & 0x3F))
+         . chr(0x80 | ( $codepoint       & 0x3F));
+  }
+  else if ($codepoint < 0x200000) {
+    return chr(0xF0 | ( $codepoint >> 18))
+         . chr(0x80 | (($codepoint >> 12) & 0x3F))
+         . chr(0x80 | (($codepoint >> 6)  & 0x3F))
+         . chr(0x80 | ( $codepoint        & 0x3F));
+  }
+}
+
+function _sn_mail_urls($url = 0, $refresh = FALSE) {
+  static $urls = array();
+
+  if($refresh) {
+    $urls = array();
+  }
+  
+  if ($url) {
+    $urls[] = strpos($url, '://') ? $url : url($url, NULL, NULL, 1);
+    return count($urls);
+  }
+  return $urls;
+}
+
 function sn_mail_confirm($email, $message, $snid = NULL, $op = NULL) {
   $mail->s_format = 'plain';
   $mail->encode = '';
