--- C:\Users\ying\Downloads\smtp-6.x-1.0\smtp\smtp.module	2012-09-21 18:53:08.000000000 +0100
+++ D:\Subversion\linimatic\trunk\modules\contrib\smtp\smtp.module	2012-11-01 21:08:02.000000000 +0100
@@ -609,157 +609,172 @@
 
   // Add the message's subject.
   $mail->Subject = $subject;
 
 
   // Processes the message's body.
-  switch ($content_type) {
-    case 'multipart/related':
-      $mail->Body = $body;
+  if ((module_exists('mimemail')) && (strpos($content_type, 'multipart/') !== FALSE)) {
+
+    // Unfold the content correct when using mimemail.
+    _smtp_unfold_mimemail_multipart($mail, $body);
+
+    // if only the alternate plain text body was set,
+    // swap the bodies and set it to plain text
+    if ((strlen($mail->Body) < 1) && (strlen($mail->AltBody) > 0)) {
+      $mail->Body = $mail->AltBody;
+      $mail->AltBody = '';
+      $mail->IsHTML(FALSE);
+    }
+  }
+  else {
+    switch ($content_type) {
+      case 'multipart/related':
+        $mail->Body = $body;
 
-/**
- * TODO
- * Firgure out if there is anything more to handling this type.
- */
+        /**
+         * TODO
+         * Firgure out if there is anything more to handling this type.
+         */
 
-      break;
+        break;
 
-    case 'multipart/alternative':
-      // Split the body based on the boundary ID.
-      $body_parts = _smtp_boundary_split($body, $boundary);
-      foreach ($body_parts as $body_part) {
-        // If plain/text within the body part, add it to $mail->AltBody.
-        if (strpos($body_part, 'text/plain')) {
-          // Clean up the text.
-          $body_part = trim(_smtp_remove_headers(trim($body_part)));
-          // Include it as part of the mail object.
-          $mail->AltBody = $body_part;
-        }
-        // If plain/html within the body part, add it to $mail->Body.
-        elseif (strpos($body_part, 'text/html')) {
-          // Clean up the text.
-          $body_part = trim(_smtp_remove_headers(trim($body_part)));
-          // Include it as part of the mail object.
-          $mail->Body = $body_part;
-        }
-      }
-      break;
+      case 'multipart/alternative':
+        // Split the body based on the boundary ID.
+        $body_parts = _smtp_boundary_split($body, $boundary);
+        foreach ($body_parts as $body_part) {
+          // If plain/text within the body part, add it to $mail->AltBody.
+          if (strpos($body_part, 'text/plain')) {
+            // Clean up the text.
+            $body_part = trim(_smtp_remove_headers(trim($body_part)));
+            // Include it as part of the mail object.
+            $mail->AltBody = $body_part;
+          }
+          // If plain/html within the body part, add it to $mail->Body.
+          elseif (strpos($body_part, 'text/html')) {
+            // Clean up the text.
+            $body_part = trim(_smtp_remove_headers(trim($body_part)));
+            // Include it as part of the mail object.
+            $mail->Body = $body_part;
+          }
+        }
+        break;
 
-    case 'multipart/mixed':
-      // Split the body based on the boundary ID.
-      $body_parts = _smtp_boundary_split($body, $boundary);
+      case 'multipart/mixed':
+        // Split the body based on the boundary ID.
+        $body_parts = _smtp_boundary_split($body, $boundary);
 
-      // Determine if there is an HTML part for when adding the plain text part.
-      $text_plain = FALSE;
-      $text_html  = FALSE;
-      foreach ($body_parts as $body_part) {
-        if (strpos($body_part, 'text/plain')) {
-          $text_plain = TRUE;
-        }
-        if (strpos($body_part, 'text/html')) {
-          $text_html = TRUE;
-        }
-      }
+        // Determine if there is an HTML part for when adding the plain text part.
+        $text_plain = FALSE;
+        $text_html = FALSE;
+        foreach ($body_parts as $body_part) {
+          if (strpos($body_part, 'text/plain')) {
+            $text_plain = TRUE;
+          }
+          if (strpos($body_part, 'text/html')) {
+            $text_html = TRUE;
+          }
+        }
 
-      foreach ($body_parts as $body_part) {
-        // If test/plain within the body part, add it to either
-        // $mail->AltBody or $mail->Body, depending on whether there is
-        // also a text/html part ot not.
-        if (strpos($body_part, 'multipart/alternative')) {
-          // Clean up the text.
-          $body_part = trim(_smtp_remove_headers(trim($body_part)));
-          // Get boundary ID from the Content-Type header.
-          $boundary2 = _smtp_get_substring($body_part, 'boundary', '"', '"');
-          // Split the body based on the boundary ID.
-          $body_parts2 = _smtp_boundary_split($body_part, $boundary2);
+        foreach ($body_parts as $body_part) {
+          // If test/plain within the body part, add it to either
+          // $mail->AltBody or $mail->Body, depending on whether there is
+          // also a text/html part ot not.
+          if (strpos($body_part, 'multipart/alternative')) {
+            // Clean up the text.
+            $body_part = trim(_smtp_remove_headers(trim($body_part)));
+            // Get boundary ID from the Content-Type header.
+            $boundary2 = _smtp_get_substring($body_part, 'boundary', '"', '"');
+            // Split the body based on the boundary ID.
+            $body_parts2 = _smtp_boundary_split($body_part, $boundary2);
 
-          foreach ($body_parts2 as $body_part2) {
-            // If plain/text within the body part, add it to $mail->AltBody.
-            if (strpos($body_part2, 'text/plain')) {
-              // Clean up the text.
-              $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
-              // Include it as part of the mail object.
-              $mail->AltBody = $body_part2;
-              $mail->ContentType = 'multipart/mixed';
-            }
-            // If plain/html within the body part, add it to $mail->Body.
-            elseif (strpos($body_part2, 'text/html')) {
-              // Clean up the text.
-              $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
-              // Include it as part of the mail object.
-              $mail->Body = $body_part2;
-              $mail->ContentType = 'multipart/mixed';
-            }
-          }
-        }
-        // If text/plain within the body part, add it to $mail->Body.
-        elseif (strpos($body_part, 'text/plain')) {
-          // Clean up the text.
-          $body_part = trim(_smtp_remove_headers(trim($body_part)));
+            foreach ($body_parts2 as $body_part2) {
+              // If plain/text within the body part, add it to $mail->AltBody.
+              if (strpos($body_part2, 'text/plain')) {
+                // Clean up the text.
+                $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
+                // Include it as part of the mail object.
+                $mail->AltBody = $body_part2;
+                $mail->ContentType = 'multipart/mixed';
+              }
+              // If plain/html within the body part, add it to $mail->Body.
+              elseif (strpos($body_part2, 'text/html')) {
+                // Clean up the text.
+                $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
+                // Include it as part of the mail object.
+                $mail->Body = $body_part2;
+                $mail->ContentType = 'multipart/mixed';
+              }
+            }
+          }
+          // If text/plain within the body part, add it to $mail->Body.
+          elseif (strpos($body_part, 'text/plain')) {
+            // Clean up the text.
+            $body_part = trim(_smtp_remove_headers(trim($body_part)));
 
-          if ($text_html) {
-            $mail->AltBody = $body_part;
-            $mail->IsHTML(TRUE);
-            $mail->ContentType = 'multipart/mixed';
-          }
-          else {
-            $mail->Body = $body_part;
-            $mail->IsHTML(FALSE);
-            $mail->ContentType = 'multipart/mixed';
-          }
-        }
-        // If text/html within the body part, add it to $mail->Body.
-        elseif (strpos($body_part, 'text/html')) {
-          // Clean up the text.
-          $body_part = trim(_smtp_remove_headers(trim($body_part)));
-          // Include it as part of the mail object.
-          $mail->Body = $body_part;
-          $mail->IsHTML(TRUE);
-          $mail->ContentType = 'multipart/mixed';
-        }
-        // Add the attachment.
-        elseif (strpos($body_part, 'Content-Disposition: attachment;')) {
-          $file_path     = _smtp_get_substring($body_part, 'filename=', '"', '"');
-          $file_name     = _smtp_get_substring($body_part, ' name=', '"', '"');
-          $file_encoding = _smtp_get_substring($body_part, 'Content-Transfer-Encoding', ' ', "\n");
-          $file_type     = _smtp_get_substring($body_part, 'Content-Type', ' ', ';');
+            if ($text_html) {
+              $mail->AltBody = $body_part;
+              $mail->IsHTML(TRUE);
+              $mail->ContentType = 'multipart/mixed';
+            }
+            else {
+              $mail->Body = $body_part;
+              $mail->IsHTML(FALSE);
+              $mail->ContentType = 'multipart/mixed';
+            }
+          }
+          // If text/html within the body part, add it to $mail->Body.
+          elseif (strpos($body_part, 'text/html')) {
+            // Clean up the text.
+            $body_part = trim(_smtp_remove_headers(trim($body_part)));
+            // Include it as part of the mail object.
+            $mail->Body = $body_part;
+            $mail->IsHTML(TRUE);
+            $mail->ContentType = 'multipart/mixed';
+          }
+          // Add the attachment.
+          elseif (strpos($body_part, 'Content-Disposition: attachment;')) {
+            $file_path = _smtp_get_substring($body_part, 'filename=', '"', '"');
+            $file_name = _smtp_get_substring($body_part, ' name=', '"', '"');
+            $file_encoding = _smtp_get_substring($body_part, 'Content-Transfer-Encoding', ' ', "\n");
+            $file_type = _smtp_get_substring($body_part, 'Content-Type', ' ', ';');
 
-          if (file_exists($file_path)) {
-            if (!$mail->AddAttachment($file_path, $file_name, $file_encoding, $filetype)) {
-              drupal_set_message(t('Attahment could not be found or accessed.'));
-            }
-          }
-          else {
-            // Clean up the text.
-            $body_part = trim(_smtp_remove_headers(trim($body_part)));
+            if (file_exists($file_path)) {
+              if (!$mail->AddAttachment($file_path, $file_name, $file_encoding, $filetype)) {
+                drupal_set_message(t('Attahment could not be found or accessed.'));
+              }
+            }
+            else {
+              // Clean up the text.
+              $body_part = trim(_smtp_remove_headers(trim($body_part)));
 
-            if (drupal_strtolower($file_encoding) == 'base64') {
-              $attachment = base64_decode($body_part);
-            }
-            elseif (drupal_strtolower($file_encoding) == 'quoted-printable') {
-              $attachment = quoted_printable_decode($body_part);
-            }
-            else {
-              $attachment = $body_part;
-            }
+              if (drupal_strtolower($file_encoding) == 'base64') {
+                $attachment = base64_decode($body_part);
+              }
+              elseif (drupal_strtolower($file_encoding) == 'quoted-printable') {
+                $attachment = quoted_printable_decode($body_part);
+              }
+              else {
+                $attachment = $body_part;
+              }
 
-            $attachment_new_filename = tempnam(realpath(file_directory_temp()), 'smtp');
-            $file_path               = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_RENAME);
+              $attachment_new_filename = tempnam(realpath(file_directory_temp()), 'smtp');
+              $file_path = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_RENAME);
 
-            if (!$mail->AddAttachment($file_path, $file_name)) { // , $file_encoding, $filetype);
-              drupal_set_message(t('Attachment could not be found or accessed.'));
-            }
-          }
-        }
-      }
-      break;
+              if (!$mail->AddAttachment($file_path, $file_name)) { // , $file_encoding, $filetype);
+                drupal_set_message(t('Attachment could not be found or accessed.'));
+              }
+            }
+          }
+        }
+        break;
 
-    default:
-      $mail->Body = $body;
-      break;
-  }
+      default:
+        $mail->Body = $body;
+        break;
+    }
+  }
 
 
   // Set the authentication settings.
   $username = variable_get('smtp_username', '');
   $password = variable_get('smtp_password', '');
 
@@ -865,15 +880,23 @@
 function _smtp_remove_headers($input) {
   $part_array = explode("\n", $input);
 
   if (strpos($part_array[0], 'Content') !== FALSE) {
     if (strpos($part_array[1], 'Content') !== FALSE) {
       if (strpos($part_array[2], 'Content') !== FALSE) {
+        if (strpos($part_array[2], 'Content') !== FALSE) {
           array_shift($part_array);
           array_shift($part_array);
           array_shift($part_array);
+          array_shift($part_array);
+        }
+        else {
+          array_shift($part_array);
+          array_shift($part_array);
+          array_shift($part_array);
+        }
       }
       else {
         array_shift($part_array);
         array_shift($part_array);
       }
     }
@@ -926,13 +949,148 @@
     return smtp_drupal_mail_wrapper($message);
   }
   
 }
 
 if (module_exists('mimemail')) {
-  
+  /**
+   * unfold an mimemail nested multipart body recursive.
+   *
+   *  mimemail enfolds the content parts by multible nested multipart chunks:
+   *
+   *  multipart/mixed
+   *       +  multipart/related
+   *           +  multipart/alternative
+   *              +  text/plain
+   *              +  text/html
+   *          +  inline image 1
+   *          +  inline image 2
+   *          ...
+   *          +  inline image n
+   *      +  attachment 1
+   *      + attachment 1
+   *      ...
+   *      +  attachment n
+   *
+   *   if you use the mimemail inline image feature,
+   *   as an result the orgininal content rebuilding of the smtp authentiction module doesn't work.
+   *
+   * @param $mail
+   *   points to the current instance of the phpmailer.
+   * @param $body_part
+   *   mime encoded body part delivered by the mimemail module .
+   * @param int $recursion_fuse
+   *   (Optional) fuse against recursion overflow .
+   */
+  function _smtp_unfold_mimemail_multipart($mail, $body_part, $recursion_fuse = 0) {
+    if (++$recursion_fuse > 10) {
+      drupal_set_message(t('Error decoding mimemail delivered mail body. (to much enfolded body parts)'));
+      return;
+    }
+
+    $body_part = trim(_smtp_remove_headers(trim($body_part)));
+    // Get boundary ID from the Content-Type header.
+    $boundary2 = _smtp_get_substring($body_part, 'boundary', '"', '"');
+    // Split the body based on the boundary ID.
+    $body_parts2 = _smtp_boundary_split($body_part, $boundary2);
+
+    foreach ($body_parts2 as $body_part2) {
+      $disposition = _smtp_get_substring($body_part2, 'Content-Disposition', ' ', ';');
+      $charset = _smtp_get_substring($body_part2, ' charset', '=', "\n");
+      $file_cid = _smtp_get_substring($body_part2, 'Content-ID', '<', '>');
+      $file_path = _smtp_get_substring($body_part2, 'filename=', '"', '"');
+      $file_name = _smtp_get_substring($body_part2, ' name=', '"', '"');
+      $file_encoding = _smtp_get_substring($body_part2, 'Content-Transfer-Encoding', ' ', "\n");
+      $file_type = _smtp_get_substring($body_part2, 'Content-Type', ' ', ';');
+
+      if (strpos($disposition, 'related') !== FALSE) {
+
+        if (file_exists($file_path)) {
+          if (!$mail->AddEmbeddedImage($file_path, $file_cid, $file_name)) {
+            drupal_set_message(t('Image could not be found or accessed.'));
+          }
+        }
+        else {
+          // Clean up the text.
+          $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
+
+          if (drupal_strtolower($file_encoding) == 'base64') {
+            $attachment = base64_decode($body_part2);
+          }
+          elseif (drupal_strtolower($file_encoding) == 'quoted-printable') {
+            $attachment = quoted_printable_decode($body_part2);
+          }
+          else {
+            $attachment = $body_part;
+          }
+
+          $attachment_new_filename = tempnam(realpath(file_directory_temp()), 'smtp');
+          $file_path = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_RENAME);
+
+          if (!$mail->AddEmbeddedImage($file_path, $file_cid, $file_name)) {
+            drupal_set_message(t('Image could not be found or accessed.'));
+          }
+        }
+      }
+      elseif (strpos($disposition, 'attachment') !== FALSE) {
+
+        if (file_exists($file_path)) {
+          if (!$mail->AddAttachment($file_path, $file_name, $file_encoding, $filetype)) {
+            drupal_set_message(t('Attachment could not be found or accessed.'));
+          }
+        }
+        else {
+          // Clean up the text.
+          $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
+
+          if (drupal_strtolower($file_encoding) == 'base64') {
+            $attachment = base64_decode($body_part2);
+          }
+          elseif (drupal_strtolower($file_encoding) == 'quoted-printable') {
+            $attachment = quoted_printable_decode($body_part2);
+          }
+          else {
+            $attachment = $body_part;
+          }
+
+          $attachment_new_filename = tempnam(realpath(file_directory_temp()), 'smtp');
+          $file_path = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_RENAME);
+
+          if (!$mail->AddAttachment($file_path, $file_name)) {
+            drupal_set_message(t('Attachment could not be found or accessed.'));
+          }
+        }
+      }
+      elseif (strpos($file_type, 'multipart/') !== FALSE) {
+
+        // recursive call
+        _smtp_unfold_mimemail_multipart($mail, $body_part2, $recursion_fuse);
+
+      }
+      elseif (strpos($file_type, 'text/plain') !== FALSE) {
+        // Clean up the text.
+        $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
+        // Include it as part of the mail object.
+        $mail->AltBody = $body_part2;
+        if ($charset) {
+          $mail->CharSet = $charset;
+        }
+      }
+      // If plain/html within the body part, add it to $mail->Body.
+      elseif (strpos($file_type, 'text/html') !== FALSE) {
+        // Clean up the text.
+        $body_part2 = trim(_smtp_remove_headers(trim($body_part2)));
+        // Include it as part of the mail object.
+        $mail->Body = $body_part2;
+        if ($charset) {
+          $mail->CharSet = $charset;
+        }
+      }
+    }
+  }
+
   /**
    * hook_mailengine for SMTP/MIMEMAIL integration
    *
    * @param $op
    *   The operation to perform on the message.
    * @param $message
