--- listhandler.module-head	2007-07-10 17:51:10.000000000 +0100
+++ listhandler.module	2007-12-13 13:04:40.000000000 +0000
@@ -75,8 +75,14 @@
     '#options' => array(0 => t('blocked'), 1 => t('active')),
     '#description' => t('The status of accounts created by listhandler.'));
 
-  $form['listhandler_list']['#tree'] = TRUE;
-  $form['listhandler_prefix']['#tree'] = TRUE;
+  $form['listhandler_list'] = array(
+    '#tree' => TRUE,
+  );
+
+  $form['listhandler_prefix'] = array(
+    '#tree' => TRUE,
+  );
+
   $result = db_query("SELECT * FROM {mailhandler} WHERE mailto != '' ORDER BY mailto");
 
   while ($mailbox = db_fetch_object($result)) {
@@ -96,8 +102,7 @@
 /**
 * Theme call to render the tabular form of mailing lists and prefixes
 */
-
-function theme_listhandler_settings($form) {
+function theme_listhandler_admin_settings($form) {
   $output = drupal_render($form['listhandler_from']);
   $output .= drupal_render($form['listhandler_strip_title']);
   $output .= drupal_render($form['listhandler_accountstatus']);
@@ -121,16 +126,14 @@
 }
 
 function listhandler_admin_settings_submit($form_id, &$form_values) {
-  $edit = $form_values['edit'];
-  variable_set('listhandler_from', $edit['listhandler_from']);
-  variable_set('listhandler_strip_title', $edit['listhandler_strip_title']);
-  variable_set('listhandler_accountstatus', $edit['listhandler_accountstatus']);
-  if (isset($edit['listhandler_prefix'])) {
-    foreach ($edit['listhandler_prefix'] as $key => $value) {
+  variable_set('listhandler_from', $form_values['listhandler_from']);
+  variable_set('listhandler_strip_title', $form_values['listhandler_strip_title']);
+  variable_set('listhandler_accountstatus', $form_values['listhandler_accountstatus']);
+  if (isset($form_values['listhandler_prefix'])) {
+    foreach ($form_values['listhandler_prefix'] as $key => $value) {
       db_query("UPDATE {mailhandler} SET prefix = '%s' WHERE mid = %d", $value, $key);
     }
   }
-  drupal_set_message('The configuration options have been saved.');
 
   return system_settings_form_submit($form_id, $form_values);
 }
@@ -209,6 +212,7 @@
 
 function listhandler_send_mail($edit) {
   global $user, $base_url;
+  $mid=array();
 
   if(!$user->uid) {
     $edit['name'] = variable_get('anonymous', 'Anonymous');
@@ -265,8 +269,8 @@
     $edit['pid']=0;
   }
   if($result) {// Parent was sent or received by listhandler
-    $mid = "References: ". $result->msgid ."\n";
-    $mid .= "In-Reply-To: ". $result->msgid ."\n";
+    $mid['References'] = $result->msgid;
+    $mid['In-Reply-To'] = $result->msgid;
   }
   //common headers
   $dir = str_replace("/", ".", substr(strchr(str_replace("http://", "", $base_url), "/"), 1));
@@ -276,7 +280,7 @@
     $edit["tid"] = $mbox[2];
     // We add tokens to the MID. Replies to comments sent by listhandler can thus be classified easier.
     $msgid = "<listhandler=". $edit["mid"] ."&site=". $dir . $_SERVER["SERVER_NAME"] ."&nid=". $edit["nid"] ."&pid=". $edit["pid"] ."&cid=".  $edit["cid"] ."&uid=". $user->uid ."&tid=".  $edit["tid"] ."&". md5($edit["nid"] . $edit["cid"]) ."@". $dir . strtolower($_SERVER["SERVER_NAME"]) .">";
-    $mid .= "Message-Id: ". $msgid ."\n";
+    $mid['Message-Id']=$msgid;
     // send mail
     $file = drupal_get_path('module', 'listhandler') .'/mail.inc';
     if (file_exists($file)) {
@@ -291,15 +295,33 @@
       $body = strip_tags($edit['comment']);
     }
 
-    // OLD  $err = user_mail($address, $subject, $body, "From: ". $edit["name"] ." <". $edit["mail"] .">\n". $mid);
-
-    $err = drupal_mail($mid, $address, $subject, $body, $edit["mail"]);
+#    // OLD  $err = user_mail($address, $subject, $body, "From: ". $edit["name"] ." <". $edit["mail"] .">\n". $mid);
+#
+#    $err = drupal_mail($mid, $address, $subject, $body, $edit["mail"]);
+    $attachments='';
+    if (is_array($edit['files'])) {
+      foreach ($edit['files'] as $key => $ufile) {
+	       $href = file_create_url((strpos($ufile['fid'], 'upload') === FALSE ? $ufile['filepath'] : file_create_filename($ufile['filename'], file_create_path())));
+        $text = $ufile['description'] ? $ufile['description'] : $ufile['filename'] ;
+	       $attachments .= "\n".$href;
+      }
+      if (!empty($attachments)) {
+	       $body .= "\n\n".t('Attachment').":\n";
+	       $body .= $attachments;
+      }
+    }
+    $ok = drupal_mail('listhandler-mail', $address, $subject, $body, $edit["mail"],$mid);
 
     // save comment ids in listhandler table.
     // Not strictly needed, the same info is stored in the msgid.
     db_query("INSERT INTO {listhandler} (msgid, nid, cid, pid, uid, mid, tid) VALUES ('%s','%s','%s','%s','%s','%s','%s')", $msgid, $edit["nid"], $edit["cid"], $edit["pid"], $user->uid, $edit["mid"], $edit["tid"]);
     // call watchdog
+    if ($ok) { 
     watchdog('listhandler', t("'!s' sent to '!a'.", array("!s" => $edit["subject"], "!a" => $address)));
+      watchdog('listhandler', t("'!s'", array("!s" => "$address, $subject, ".$edit["mail"].", $mid")));
+    } else {
+      watchdog('listhandler', t("Error sending '!s' to '!a'.", array("!s" => $edit["subject"], "!a" => $address)));
+    }
   }
 }
 
