--- listhandler.module-5.x-1.x	2007-12-18 21:25:05.000000000 +0000
+++ listhandler.module	2007-12-20 21:35:39.000000000 +0000
@@ -212,6 +212,7 @@
 
 function listhandler_send_mail($edit) {
   global $user, $base_url;
+  $mid=array();
 
   if(!$user->uid) {
     $edit['name'] = variable_get('anonymous', 'Anonymous');
@@ -237,7 +238,7 @@
           $result2 = db_query("SELECT * FROM {mailhandler} m WHERE m.commands ~ '%s'", 'tid:[[:blank:]]*'. $taxoterm .'[[:>:]]');
       }
       while ($mbox = db_fetch_object($result2)) {
-        $mboxen[] = array($mbox->mid, $mbox->mailto, $taxoarr->tid);
+        $mboxen[] = array($mbox->mid, $mbox->mailto, $taxoarr->tid, $mbox->commands);
       }
     }
   }
@@ -254,7 +255,7 @@
         $result2 = db_query("SELECT * FROM {mailhandler} m WHERE m.commands ~ '%s'", 'tid:[[:blank:]]*'. $taxoarr->tid .'[[:>:]]');
       }
       while ($mbox = db_fetch_object($result2)) {
-        $mboxen[] = array($mbox->mid, $mbox->mailto, $taxoarr->tid);
+        $mboxen[] = array($mbox->mid, $mbox->mailto, $taxoarr->tid, $mbox->commands);
       }
     }
   }
@@ -268,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));
@@ -277,14 +278,11 @@
     $edit["mid"] = $mbox[0];
     $address = $mbox[1];
     $edit["tid"] = $mbox[2];
+    // commands
+    $commandarr = listhandler_commands($mbox[3]);
     // 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";
-    // send mail
-    $file = drupal_get_path('module', 'listhandler') .'/mail.inc';
-    if (file_exists($file)) {
-      include_once($file);
-    }
+    $mid['Message-Id']=$msgid;
     if (function_exists('mail_html_to_text')) {
       $subject = mail_html_to_text($edit['subject']);
       $body = mail_html_to_text($edit['comment']);
@@ -293,16 +291,53 @@
       $subject = strip_tags($edit['subject']);
       $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"]);
+    $attachments = '';
+    $fragment = NULL;
+    $lfiles = false;
+    if ($edit['cid']) {
+      $fragment = 'comment-'.$edit['cid'];
+      //attachment comments are already created
+      if (function_exists('_comment_upload_load_files')) {
+	$lfiles = _comment_upload_load_files($edit['cid']);
+      }
+    } else {
+      $lfiles = $edit['files'];
+    }
+    if ($lfiles) {
+      foreach ($lfiles as $key => $ufile) {
+        $ufile = (object) $ufile;
+        $href = file_create_url((strpos($ufile->fid, 'upload') === FALSE ? $ufile->filepath : file_create_filename($ufile->filename, file_create_path())));
+        $href = str_replace(' ', '%20', $href);
+        $text = $ufile->description ? $ufile->description : $ufile->filename ;
+        $attachments .= "\n\n"." [".$text."]:\n".$href;
+      }
+      if (!empty($attachments)) {
+        $url = url ('node/'.$edit['nid'],NULL,$fragment,TRUE);
+        $body .= "\n\n".t('File attachments').":";
+        $body .= $attachments;
+        $body .= "\n----------\n".t("Original attachments can be found at:\n!u",array('!u' => $url));
+      }
+    }
+    // send mail
+    $ok = 0;
+    // this makes it possible to add code specific to the listmailer in use. eg 'list_type: ezmlm' in mailhandler commands
+	$file = drupal_get_path('module', 'listhandler') .'/' . ( isset($commandarr['list_type']) ? $commandarr['list_type'] . "_" : "" ) . 'mail.inc';
+    if (file_exists($file)) {
+      include_once($file);
+    }
+    if (! $ok) {
+      $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)));
+    } else {
+    	watchdog('listhandler', t("Error sending '!s' to '!a'.", array("!s" => $edit["subject"], "!a" => $address)));
+    }
   }
 }
 
@@ -464,3 +499,26 @@
   return $node;
 }
 
+/**
+ * process the command string from mailhandler and put it into an array
+ */
+function listhandler_commands($cmdstr) {
+  $cmdarr = preg_split("/\r\n/", $cmdstr);
+  $out = array();
+  foreach($cmdarr as $line) {
+_listhandler_debugfunc("in listhandler_commands (line=$line)\n");
+
+    $line = trim($line);
+    // skip empty lines or lines commented with # or ;
+    #if( empty($line) || preg_match( "/^#.*/", $line) || preg_match( "/^;.*/", $line) ) {
+    if(empty($line)) {
+			continue;
+		}
+		if( preg_match("/(^.+):(.+$)/", $line, $matches) ) {
+      if(trim($matches[1])) {
+				$out[trim($matches[1])] = trim($matches[2]);
+			}
+		}
+	}
+	return $out;
+}
