--- listhandler.module.old	2008-01-06 18:19:16.000000000 +0000
+++ listhandler.module	2008-01-06 18:27:06.000000000 +0000
@@ -81,6 +81,11 @@
     '#default_value' => variable_get('listhandler_accountstatus', 0),
     '#options' => array(0 => t('blocked'), 1 => t('active')),
     '#description' => t('The status of accounts created by listhandler.'));
+  $form['listhandler_attachments_as_link'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Attachments as link'),
+    '#default_value' => variable_get('listhandler_attachments_as_link', 0),
+    '#description' => t('Send attachments as links instead of MIME attachments. It affects only mails generated by forum posts.'));
 
   $form['listhandler_list'] = array(
       '#tree' => TRUE,
@@ -139,6 +144,7 @@
   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']);
+  variable_set('listhandler_attachments_as_link', $form_values['listhandler_attachments_as_link']);
   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);
@@ -296,7 +302,6 @@
     // The below is required for EZMLM
     // It sets the Return Path to the Listhandler subscribed email address (rather than the webserver)
     // so that EZMLM (and everybody else) knows that we sent it
-    // BROKEN, Needs array($mid) patch first: $mid['Return-Path'] = $edit['mail'];    
 
     // 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"]) .">";
@@ -317,6 +322,26 @@
       $subject = strip_tags($edit['subject']);
       $body = strip_tags($edit['comment']);
     }
+    $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) {
+      if (variable_get('listhandler_attachments_as_link', 0)) {
+        $body .= _listhandler_mail_links($lfiles, $edit['nid']);
+      }
+      else {
+        $body = _listhandler_mail_attachments($body, $lfiles, $mid);
+      }
+    }
 
     // send email
       // was: $err = user_mail($address, $subject, $body, "From: ". $edit["name"] ." <". $edit["mail"] .">\n". $mid);
@@ -507,3 +532,52 @@
   $tests = file_scan_directory($dir, '\.test$');
   return array_keys($tests);
 }
+
+/**
+ * Convert upload files into links
+ */
+function _listhandler_mail_links($lfiles, $nid) {
+  $body = '';
+  $attachments = '';
+  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/'. $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));
+  }
+  return $body;
+}
+
+/**
+ * Convert upload files into mail attachments
+ */
+function _listhandler_mail_attachments($body, $files, &$mid) {
+  $trenner  = md5(uniqid(time()));
+  $mid['Content-Type'] = "multipart/mixed; boundary=$trenner";
+  $message  = "\n--$trenner\n";
+  $message .= "Content-Type: text/plain; charset=UTF-8; format=flowed;"."\n\n"; // sets the mime type
+  $message .= $body ."\n";
+  $message .= "\n\n";
+  foreach ($files as $key => $file) {
+    $file = (object) $file;
+    if ($file->filename) {
+      $file->filepath = str_replace("\\", "\\\\", $file->filepath);
+      $message  .= "--$trenner"."\n";
+      $message  .= "Content-Type:$file->filemime;\n\tname=$file->filename\n";
+      $message  .= "Content-Transfer-Encoding: base64\n";
+      $message  .= "Content-Disposition: attachment;\n\tfilename=$file->filename\n\n";
+      $filedata  = fread(fopen($file->filepath, "rb"), $file->filesize);
+      $message  .= chunk_split(base64_encode($filedata));
+      $message  .= "\n\n";
+    }
+  }
+  $message .= "--$trenner--";
+  return $message;
+}
