Index: sites/all/modules/changed/smtp/smtp.mail.inc
===================================================================
--- sites/all/modules/changed/smtp/smtp.mail.inc	(revision 2374)
+++ sites/all/modules/changed/smtp/smtp.mail.inc	(working copy)
@@ -10,8 +10,18 @@
 * Include the option to choose between plain text or HTML
 */
 class SmtpMailSystem implements MailSystemInterface {
+  private static $mailer;
+  protected $AllowHtml;
 
-  protected $AllowHtml;
+  function __construct() {
+    $this->mailer = new PHPMailer();
+    $this->mailer->SMTPKeepAlive = TRUE;
+  }
+
+  function __destruct() {
+    // Close connection
+    $this->mailer->SmtpClose();
+  }
   /**
    * Concatenate and wrap the e-mail body for either
    * plain-text or HTML emails.
@@ -46,6 +56,11 @@
    *   TRUE if the mail was successfully accepted, otherwise FALSE.
    */
   public function mail(array $message) {
+    //Clear all set TO adresses and all headers...
+    $this->mailer->ClearAllRecipients();
+    $this->mailer->ClearAttachments();
+    $this->mailer->ClearCustomHeaders();
+
     $id       = $message['id'];
     $to       = $message['to'];
     $from     = $message['from'];
@@ -54,11 +69,11 @@
     $headers  = $message['headers'];
 
     // Create a new PHPMailer object - autoloaded from registry.
-    $mailer = new PHPMailer();
+    //$this->mailer = new PHPMailer();
 
     // Turn on debugging, if requested.
     if (variable_get('smtp_debugging', 0) == 1) {
-      $mailer->SMTPDebug = TRUE;
+      $this->mailer->SMTPDebug = TRUE;
     }
 
     // Set the from name and e-mail address.
@@ -111,9 +126,9 @@
     }
 
     // Defines the From value to what we expect.
-    $mailer->From     = $from;
-    $mailer->FromName = $from_name;
-    $mailer->Sender   = $from;
+    $this->mailer->From     = $from;
+    $this->mailer->FromName = $from_name;
+    $this->mailer->Sender   = $from;
 
 
     // Create the list of 'To:' recipients.
@@ -128,7 +143,7 @@
         $toname = '';
         $toaddr = $torecipient;
       }
-      $mailer->AddAddress($toaddr, $toname);
+      $this->mailer->AddAddress($toaddr, $toname);
     }
 
 
@@ -143,10 +158,10 @@
             // Should be the most common situation since drupal_mail moves the
             // from to headers.
             $from           = $value;
-            $mailer->From     = $value;
+            $this->mailer->From     = $value;
             // then from can be out of sync with from_name !
-            $mailer->FromName = '';
-            $mailer->Sender   = $value;
+            $this->mailer->FromName = '';
+            $this->mailer->Sender   = $value;
           }
           break;
         case 'content-type':
@@ -164,17 +179,17 @@
           // Set the charset based on the provided value, if there is one.
           $charset = $vars['charset'];
           if ($charset) {
-            $mailer->CharSet = $charset;
+            $this->mailer->CharSet = $charset;
           }
           switch ($vars[0]) {
             case 'text/plain':
               // The message includes only a plain text part.
-              $mailer->IsHTML(FALSE);
+              $this->mailer->IsHTML(FALSE);
               $content_type = 'text/plain';
               break;
             case 'text/html':
               // The message includes only an HTML part.
-              $mailer->IsHTML(TRUE);
+              $this->mailer->IsHTML(TRUE);
               $content_type = 'text/html';
               break;
             case 'multipart/related':
@@ -182,18 +197,18 @@
               $boundary = $this->_get_substring($value, 'boundary', '"', '"');
 
               // The message includes an HTML part w/inline attachments.
-              $mailer->ContentType = $content_type = 'multipart/related; boundary="' . $boundary . '"';
+              $this->mailer->ContentType = $content_type = 'multipart/related; boundary="' . $boundary . '"';
             break;
             case 'multipart/alternative':
               // The message includes both a plain text and an HTML part.
-              $mailer->ContentType = $content_type = 'multipart/alternative';
+              $this->mailer->ContentType = $content_type = 'multipart/alternative';
 
               // Get the boundary ID from the Content-Type header.
               $boundary = $this->_get_substring($value, 'boundary', '"', '"');
             break;
             case 'multipart/mixed':
               // The message includes one or more attachments.
-              $mailer->ContentType = $content_type = 'multipart/mixed';
+              $this->mailer->ContentType = $content_type = 'multipart/mixed';
 
               // Get the boundary ID from the Content-Type header.
               $boundary = $this->_get_substring($value, 'boundary', '"', '"');
@@ -204,7 +219,7 @@
               watchdog('smtp', 'The Content-Type of your message is not supported by PHPMailer and will be sent as text/plain instead.', array(), WATCHDOG_ERROR);
 
               // Force the Content-Type to be text/plain.
-              $mailer->IsHTML(FALSE);
+              $this->mailer->IsHTML(FALSE);
               $content_type = 'text/plain';
           }
           break;
@@ -217,16 +232,16 @@
               $replyToName = trim($replyToParts[0]);
               $replyToName = trim($replyToName, '"');
               $replyToAddr = rtrim($replyToParts[1], '>');
-              $mailer->AddReplyTo($replyToAddr, $replyToName);
+              $this->mailer->AddReplyTo($replyToAddr, $replyToName);
             }
             else {
-              $mailer->AddReplyTo($value);
+              $this->mailer->AddReplyTo($value);
             }
           }
           break;
 
         case 'content-transfer-encoding':
-          $mailer->Encoding = $value;
+          $this->mailer->Encoding = $value;
           break;
 
         case 'return-path':
@@ -236,7 +251,7 @@
           break;
 
         case 'errors-to':
-          $mailer->AddCustomHeader('Errors-To: ' . $value);
+          $this->mailer->AddCustomHeader('Errors-To: ' . $value);
           break;
 
         case 'cc':
@@ -251,7 +266,7 @@
               $ccname = '';
               $ccaddr = $ccrecipient;
             }
-            $mailer->AddBCC($ccaddr, $ccname);
+            $this->mailer->AddBCC($ccaddr, $ccname);
           }
           break;
 
@@ -267,13 +282,13 @@
               $bccname = '';
               $bccaddr = $bccrecipient;
             }
-            $mailer->AddBCC($bccaddr, $bccname);
+            $this->mailer->AddBCC($bccaddr, $bccname);
           }
           break;
 
         default:
           // The header key is not special - add it as is.
-          $mailer->AddCustomHeader($key . ': ' . $value);
+          $this->mailer->AddCustomHeader($key . ': ' . $value);
       }
     }
 
@@ -284,25 +299,25 @@
 
     // Add one last header item, but not if it has already been added.
     $errors_to = FALSE;
-    foreach ($mailer->CustomHeader as $custom_header) {
+    foreach ($this->mailer->CustomHeader as $custom_header) {
       if ($custom_header[0] = '') {
         $errors_to = TRUE;
       }
     }
     if ($errors_to) {
-      $mailer->AddCustomHeader('Errors-To: '. $from);
+      $this->mailer->AddCustomHeader('Errors-To: '. $from);
     }
    */
 
 
     // Add the message's subject.
-    $mailer->Subject = $subject;
+    $this->mailer->Subject = $subject;
 
 
     // Processes the message's body.
     switch ($content_type) {
       case 'multipart/related':
-        $mailer->Body = $body;
+        $this->mailer->Body = $body;
 
   /**
    * TODO
@@ -315,19 +330,19 @@
         // Split the body based on the boundary ID.
         $body_parts = $this->_boundary_split($body, $boundary);
         foreach ($body_parts as $body_part) {
-          // If plain/text within the body part, add it to $mailer->AltBody.
+          // If plain/text within the body part, add it to $this->mailer->AltBody.
           if (strpos($body_part, 'text/plain')) {
             // Clean up the text.
             $body_part = trim($this->_remove_headers(trim($body_part)));
             // Include it as part of the mail object.
-            $mailer->AltBody = $body_part;
+            $this->mailer->AltBody = $body_part;
           }
-          // If plain/html within the body part, add it to $mailer->Body.
+          // If plain/html within the body part, add it to $this->mailer->Body.
           elseif (strpos($body_part, 'text/html')) {
             // Clean up the text.
             $body_part = trim($this->_remove_headers(trim($body_part)));
             // Include it as part of the mail object.
-            $mailer->Body = $body_part;
+            $this->mailer->Body = $body_part;
           }
         }
         break;
@@ -350,7 +365,7 @@
 
         foreach ($body_parts as $body_part) {
           // If test/plain within the body part, add it to either
-          // $mailer->AltBody or $mailer->Body, depending on whether there is
+          // $this->mailer->AltBody or $this->mailer->Body, depending on whether there is
           // also a text/html part ot not.
           if (strpos($body_part, 'multipart/alternative')) {
             // Clean up the text.
@@ -361,48 +376,48 @@
             $body_parts2 = $this->_boundary_split($body_part, $boundary2);
 
             foreach ($body_parts2 as $body_part2) {
-              // If plain/text within the body part, add it to $mailer->AltBody.
+              // If plain/text within the body part, add it to $this->mailer->AltBody.
               if (strpos($body_part2, 'text/plain')) {
                 // Clean up the text.
                 $body_part2 = trim($this->_remove_headers(trim($body_part2)));
                 // Include it as part of the mail object.
-                $mailer->AltBody = $body_part2;
-                $mailer->ContentType = 'multipart/mixed';
+                $this->mailer->AltBody = $body_part2;
+                $this->mailer->ContentType = 'multipart/mixed';
               }
-              // If plain/html within the body part, add it to $mailer->Body.
+              // If plain/html within the body part, add it to $this->mailer->Body.
               elseif (strpos($body_part2, 'text/html')) {
                 // Clean up the text.
                 $body_part2 = trim($this->_remove_headers(trim($body_part2)));
                 // Include it as part of the mail object.
-                $mailer->Body = $body_part2;
-                $mailer->ContentType = 'multipart/mixed';
+                $this->mailer->Body = $body_part2;
+                $this->mailer->ContentType = 'multipart/mixed';
               }
             }
           }
-          // If text/plain within the body part, add it to $mailer->Body.
+          // If text/plain within the body part, add it to $this->mailer->Body.
           elseif (strpos($body_part, 'text/plain')) {
             // Clean up the text.
             $body_part = trim($this->_remove_headers(trim($body_part)));
 
             if ($text_html) {
-              $mailer->AltBody = $body_part;
-              $mailer->IsHTML(TRUE);
-              $mailer->ContentType = 'multipart/mixed';
+              $this->mailer->AltBody = $body_part;
+              $this->mailer->IsHTML(TRUE);
+              $this->mailer->ContentType = 'multipart/mixed';
             }
             else {
-              $mailer->Body = $body_part;
-              $mailer->IsHTML(FALSE);
-              $mailer->ContentType = 'multipart/mixed';
+              $this->mailer->Body = $body_part;
+              $this->mailer->IsHTML(FALSE);
+              $this->mailer->ContentType = 'multipart/mixed';
             }
           }
-          // If text/html within the body part, add it to $mailer->Body.
+          // If text/html within the body part, add it to $this->mailer->Body.
           elseif (strpos($body_part, 'text/html')) {
             // Clean up the text.
             $body_part = trim($this->_remove_headers(trim($body_part)));
             // Include it as part of the mail object.
-            $mailer->Body = $body_part;
-            $mailer->IsHTML(TRUE);
-            $mailer->ContentType = 'multipart/mixed';
+            $this->mailer->Body = $body_part;
+            $this->mailer->IsHTML(TRUE);
+            $this->mailer->ContentType = 'multipart/mixed';
           }
           // Add the attachment.
           elseif (strpos($body_part, 'Content-Disposition: attachment;')) {
@@ -412,7 +427,7 @@
             $file_type     = $this->_get_substring($body_part, 'Content-Type', ' ', ';');
 
             if (file_exists($file_path)) {
-              if (!$mailer->AddAttachment($file_path, $file_name, $file_encoding, $filetype)) {
+              if (!$this->mailer->AddAttachment($file_path, $file_name, $file_encoding, $filetype)) {
                 drupal_set_message(t('Attahment could not be found or accessed.'));
               }
             }
@@ -433,7 +448,7 @@
               $attachment_new_filename = tempnam(realpath(file_directory_temp()), 'smtp');
               $file_path               = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_RENAME);
 
-              if (!$mailer->AddAttachment($file_path, $file_name)) { // , $file_encoding, $filetype);
+              if (!$this->mailer->AddAttachment($file_path, $file_name)) { // , $file_encoding, $filetype);
                 drupal_set_message(t('Attachment could not be found or accessed.'));
               }
             }
@@ -442,7 +457,7 @@
         break;
 
       default:
-        $mailer->Body = $body;
+        $this->mailer->Body = $body;
         break;
     }
 
@@ -453,43 +468,41 @@
 
     // If username and password are given, use SMTP authentication.
     if ($username != '' && $password != '') {
-      $mailer->SMTPAuth = TRUE;
-      $mailer->Username = $username;
-      $mailer->Password = $password;
+      $this->mailer->SMTPAuth = TRUE;
+      $this->mailer->Username = $username;
+      $this->mailer->Password = $password;
     }
 
 
     // Set the protocol prefix for the smtp host.
     switch (variable_get('smtp_protocol', 'standard')) {
       case 'ssl':
-        $mailer->SMTPSecure = 'ssl';
+        $this->mailer->SMTPSecure = 'ssl';
         break;
 
       case 'tls':
-        $mailer->SMTPSecure = 'tls';
+        $this->mailer->SMTPSecure = 'tls';
         break;
 
       default:
-        $mailer->SMTPSecure = '';
+        $this->mailer->SMTPSecure = '';
     }
 
 
     // Set other connection settings.
-    $mailer->Host   = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
-    $mailer->Port   = variable_get('smtp_port', '25');
-    $mailer->Mailer = 'smtp';
+    $this->mailer->Host   = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
+    $this->mailer->Port   = variable_get('smtp_port', '25');
+    $this->mailer->Mailer = 'smtp';
 
 
     // Let the people know what is going on.
     //watchdog('smtp', 'Sending mail to: @to', array('@to' => $to));
 
     // Try to send e-mail. If it fails, set watchdog entry.
-    if (!$mailer->Send()) {
-      watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mailer->ErrorInfo), WATCHDOG_ERROR);
+    if (!$this->mailer->Send()) {
+      watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $this->mailer->ErrorInfo), WATCHDOG_ERROR);
       return FALSE;
     }
-
-    $mailer->SmtpClose();
     return TRUE;
   }
   /**
