### Eclipse Workspace Patch 1.0
#P CVS simplenews HEAD
Index: simplenews.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v
retrieving revision 1.56
diff -u -r1.56 simplenews.module
--- simplenews.module	20 Feb 2007 18:47:01 -0000	1.56
+++ simplenews.module	2 Apr 2007 19:59:24 -0000
@@ -253,7 +253,7 @@
   if (!$sel3 = $node->receipt) {
     $sel3 = variable_get('simplenews_receipt', 0);
   }
-
+  
   $form['sending_options'] = array(
     '#type' => 'fieldset',
     '#title' => t('Newsletter sending options'),
@@ -395,7 +395,7 @@
   //since simplenews_cron() also calls _simplenews_send().
   $s_status = ($node->send == 1 && user_access('send newsletter')) ? 1 : 0;
   db_query("INSERT INTO {simplenews_newsletters} (nid, tid, s_status, s_format, priority, receipt)
-		  VALUES (%d, %d, %d, '%s', %d, %d)", $node->nid, $node->simplenews_tid, $s_status, $node->s_format, $node->priority, $node->receipt);
+      VALUES (%d, %d, %d, '%s', %d, %d)", $node->nid, $node->simplenews_tid, $s_status, $node->s_format, $node->priority, $node->receipt);
 }
 
 /**
@@ -407,7 +407,7 @@
   $node->simplenews_tid = $tid ? $tid[0] : 0;
   $s_status = ($node->send == 1 && user_access('send newsletter')) ? 1 : 0;
   db_query("UPDATE {simplenews_newsletters} SET tid = %d, s_status = %d, s_format = '%s', priority = %d, receipt = %d
-		  WHERE nid = %d", $node->simplenews_tid, $s_status, $node->s_format, $node->priority, $node->receipt, $node->nid);
+      WHERE nid = %d", $node->simplenews_tid, $s_status, $node->s_format, $node->priority, $node->receipt, $node->nid);
 }
 
 /**
@@ -1135,7 +1135,23 @@
 function simplenews_mail_send($mail) {
   $from_email = isset($mail->from_address) ? $mail->from_address : variable_get('site_mail', ini_get('sendmail_from'));
   $from = isset($mail->from_name) ? '"'. addslashes($mail->from_name).'" <'. $from_email .'>' : $from_email;
+  $encoding = variable_get('simplenews_encoding', 'utf-8');
+  
+  // If subject is not set, default to title.
+  if (!isset($mail->subject)) {
+    $mail->subject = $mail->title;
+  }
 
+  //Convert mail content and header info if required.
+  if ($encoding != 'utf-8') {
+    $mail->subject = simplenews_convert_from_utf8($mail->subject, $encoding);
+    $mail->title = simplenews_convert_from_utf8($mail->title, $encoding);
+    $mail->body = simplenews_convert_from_utf8($mail->body, $encoding);
+    $from_email = simplenews_convert_from_utf8($from_email, $encoding);
+    $from = simplenews_convert_from_utf8($from, $encoding);
+    $mail->to = simplenews_convert_from_utf8($mail->to, $encoding);
+  }
+  
   $headers = array(
     //'From' => $from,
     'Reply-To' => $from_email,
@@ -1179,11 +1195,6 @@
       break;
   }
 
-  // If subject is not set, default to title.
-  if (!isset($mail->subject)) {
-    $mail->subject = $mail->title;
-  }
-
   if (module_exists('mimemail')) {
     if ($mail->s_format == 'plain') {
       $plain_text_only = TRUE;
@@ -1193,10 +1204,10 @@
       $plain_text_only = FALSE;
       $plain_text_body = NULL;
     }
-
     return mimemail($from, $mail->to, $mail->subject, $mail->body, $plain_text_only, $headers, $plain_text_body);
   }
   else {
+    $headers['Content-Type'] = 'text/plain; charset='. $encoding;
     return drupal_mail('simplenews-send-mail', $mail->to, $mail->subject, $mail->body, $from_email, $headers);
   }
 }
@@ -1889,6 +1900,20 @@
       '#required' => TRUE,
       '#default_value' => variable_get('simplenews_from_address', $address_default),
     );
+    $form['simplenews_encoding_setting'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Encoding setting'),
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+      '#description' => t('Character encoding used for all mail send with simplenews.'),
+    );
+    $form['simplenews_encoding_setting']['simplenews_encoding'] = array(
+    '#type' => 'select',
+      '#title' => t('Encoding'),
+      '#options' => _simplenews_encoding_options(),
+      '#description' => t('Select the default mail encoding.'),
+      '#default_value' => variable_get('simplenews_encoding', 'utf-8'),
+    );
     $form['simplenews_init_send'] = array('#type' => 'fieldset',
       '#title' => t('Initial send time'),
       '#collapsible' => TRUE,
@@ -2295,6 +2320,40 @@
 }
 
 /**
+ * Helper function to determine if character conversion is possible
+ */
+function _simplenews_encoding_options() {
+  $conversions = array('utf-8' => t('utf-8'));
+  if (function_exists('iconv') || function_exists('mb_convert_encoding') || function_exists('recode_string')) {
+    $conversions['iso-8859-1'] = t('iso-8859-1');
+  }
+  return $conversions;
+}
+
+/**
+ * Based drupal_convert_to_utf8
+ * 
+ * @param $data The data to be converted.
+ * @param $encoding The encoding that the data will be in
+ */
+function simplenews_convert_from_utf8($data, $encoding) {
+  if (function_exists('iconv')) {
+    $out = @iconv('utf-8', $encoding, $data);
+  }
+  else if (function_exists('mb_convert_encoding')) {
+    $out = @mb_convert_encoding($data, $encoding, 'utf-8');
+  }
+  else if (function_exists('recode_string')) {
+    $out = @recode_string('utf-8..'.$encoding, $data);
+  }
+  else {
+    watchdog('php', t("Unsupported encoding %s. Please install iconv, GNU recode or mbstring for PHP.", array('%s' => $encoding)), WATCHDOG_ERROR);
+    return FALSE;
+  }
+
+  return $out;
+} 
+/**
  * Theme the newsletter message subject and body.
  */
 function theme_simplenews_newsletter($node, $tid) {
