--- smtp.module	2008-01-15 18:11:30.000000000 -0500
+++ /home/greg/workspace/smtp/smtp.module	2008-01-28 01:35:22.000000000 -0500
@@ -1,5 +1,5 @@
 <?php
-// $Id: smtp.module,v 1.15.2.14 2008/01/15 23:11:30 oadaeh Exp $
+// $Id $
 
 /**
  * @file
@@ -18,18 +18,15 @@
 /**
  * Implementation of hook_menu().
  */
-function smtp_menu($may_cache) {
-  $items = array();
-  if ($may_cache) {
-    $items[] = array(
-      'path' => 'admin/settings/smtp',
-      'title' => t('SMTP support'),
-      'description' => t('Allows the sending of site email to an SMTP server of your choice.'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => 'smtp_admin_settings',
-      'type' => MENU_NORMAL_ITEM,
-    );
-  }
+function smtp_menu() {
+  $items['admin/settings/smtp'] = array(
+    'title' => 'SMTP support',
+    'description' => 'Allows the sending of site email to an SMTP server of your choice.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('smtp_admin_settings'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+
   return $items;
 }
 
@@ -97,9 +94,7 @@
   }
   else{ //If openssl is not installed use normal protocol.
     variable_set('smtp_protocol', 'standard');
-    $form['server']['smtp_protocol'] = array(
-      '#value' => t('Your PHP installation does not have SSL enabled. See the !url page on php.net for more information.', array('' => l(t('OpenSSL Functions'), 'http://php.net/openssl'))),
-    );
+    $form['server']['smtp_protocol'] = array('#value' => t('Your PHP installation does not have SSL enabled. See the !url page on php.net for more information.', array('' => l(t('OpenSSL Functions'), 'http://php.net/openssl'))));
   }
   
   $form['auth'] = array('#type' => 'fieldset', '#title' => t('SMTP Authentication'), '#description' => t('Leave blank if your SMTP server does not require authentication.'));
@@ -130,8 +125,8 @@
   $test_address = variable_get('smtp_test_address', '');
   if ($test_address != ''){
     variable_del('smtp_test_address'); //Clear the variable so only one message is sent.
-    drupal_mail('smtp-test', $test_address, t('Drupal test email'), t('If you receive this message it means your site is capable of sending email.'), variable_get('site_mail', ini_get('sendmail_from')));
-    drupal_set_message(t('A test E-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/logs/watchdog'))));
+    drupal_mail('smtp', 'test', $test_address, user_preferred_language($account));
+    drupal_set_message(t('A test E-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/reports/dblog'))));
   }
 
   $form['email_test'] = array('#type' => 'fieldset', '#title' => t('Send test E-mail'));
@@ -140,81 +135,94 @@
     '#title' => t('E-mail address to send test email too'),
     '#default_value' => '',
     '#description' => t('Type in an address to have a test email sent there.'));
-    
+  
   return system_settings_form($form);
 }
 
 /**
+ * Implementation of hook_mail
+ */
+function smtp_mail($key, &$message, $params) {
+  $language = $message['language'];
+
+  switch($key) {
+    case 'test':
+      $message['subject'] = t('Drupal test email', array(), $language->language);
+      $message['body'] = t('If you receive this message it means your site is capable of sending email.', array(), $language->language);
+    break;
+  }
+}
+
+/**
  * Sends out the email.
  *
- * @param $mailkey
- *   A key to identify the mail sent, for altering.
- * @param $to
- *   string email address to send to.
- * @param $subject
- *   string subject of email.
- * @param $body
- *   string body of message.
- * @param $header
- *   string of header lines seperated by "\n".
+ * @param $message = array()
+ *   keys:
+ *     'id' - A unique identifier of the e-mail type.
+ *     'to' - The mail address or addresses where the message will be sent to.
+ *     'subject' - Subject of the e-mail to be sent.
+ *     'body' - Message to be sent.
+ *     'headers' - Associative array containing all mail headers.
  */
-function drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $header) {
-	$mail = new phpmailer(); //Create a new phpmailer object.
-	$username = variable_get('smtp_username', '');
-	$password = variable_get('smtp_password', '');
-	
-	//Set the default name emails should be from. If value is not defined in settings use site_name.
-	if (variable_get('smtp_fromname', '') != ''){
-    $from_name = variable_get('smtp_fromname', '');
-	}
-	else{
-	  $from_name = variable_get('site_name', '');
-	}
-
+function drupal_mail_wrapper($message) {
+  $mail = new phpmailer(); //Create a new phpmailer object.
+  $username = variable_get('smtp_username', '');
+  $password = variable_get('smtp_password', '');
+  
+  //Set the default name emails should be from. If value is not defined in settings use site_name.
+  if (variable_get('smtp_fromname', '') != '') {
+      $from_name = variable_get('smtp_fromname', '');
+  }
+  else{
+    $from_name = variable_get('site_name', '');
+  }
+  
   //If from email address is blank use smtp_from config option
-  if ($from == '' && variable_get('smtp_from', '') != ''){
+  if(array_key_exists('from', $message['headers'])) {
+    if($message['headers']['from'] != '') {
+      $from = $message['headers']['from'];
+    }
+    else {
+      $from = variable_get('smtp_from', '');
+    }
+  }
+  else {
     $from = variable_get('smtp_from', '');
   }
-
-	//Decide whether to use SMTP Authorization. Do so if username and password are given.
-	if ($username != '' and $password != '') {
-	  $auth = TRUE;
-	} else {
-	  $auth = FALSE;
-	}
-	
-	
-	//Take care of the email headers.
-  foreach ($header as $key => $value) {
-    //watchdog('error', 'Key: ' . $key . ' Value: ' . $value);
-    if (strtolower($key) == 'from') {
-      if ($from == NULL or $from == '') {
-        $from = $value; //If a from value was already given then set based on header.
-      }
-    }
-    else if (strtolower($key) == 'content-type' && strpos(strtolower($value),'text/html') !== FALSE) {
+  
+  //Decide whether to use SMTP Authorization. Do so if username and password are given.
+  if ($username != '' and $password != '') {
+    $auth = TRUE;
+  } else {
+    $auth = FALSE;
+  }
+  
+  //Take care of the email headers.
+  foreach ($message['headers'] as $name => $value) {
+    //watchdog('error', 'Key: ' . $name . ' Value: ' . $value);
+    if (strtolower($name) == 'content-type' && strpos(strtolower($value),'text/html') !== FALSE) {
       $mail->IsHTML(TRUE);
     }
-    else if (strtolower($key) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) {
+    else if (strtolower($name) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) {
       //$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone
-      $mail->AddCustomHeader($key . ": " . $value);
+      $mail->AddCustomHeader($name . ": " . $value);
       $mail->message_type = "pre";
     }
-    else if (strtolower($key) == 'reply-to') {
+    else if (strtolower($name) == 'reply-to') {
       $mail->AddReplyTo = $value;
     }
-    else if (strtolower($key) == 'return-path') {
+    else if (strtolower($name) == 'return-path') {
       if (trim($value) !=  '') {
         $mail->Sender = $value;
       }
     }
-    else if (strtolower($key) == 'content-transfer-encoding') {
+    else if (strtolower($name) == 'content-transfer-encoding') {
       $mail->Encoding = $value;
     }
-    else if (strtolower($key) == 'mime-version') {
+    else if (strtolower($name) == 'mime-version') {
       // just ommit MIME-Version it since it will be set by PHP-Mailer
     }
-    else if (strtolower($key) == 'bcc') {
+    else if (strtolower($name) == 'bcc') {
       $bccrecipients = split(",", $value);
       foreach ($bccrecipients as $bccrecipient) {
         if ( strpos($bccrecipient, '<') !== false ) {
@@ -230,39 +238,39 @@
       }
     }
     else { //Else the header key is not special, just add it.
-      $mail->AddCustomHeader($key . ": " . $value); //Add header line.
+      $mail->AddCustomHeader($name . ": " . $value); //Add header line.
     }
   }
 
-	//If no from address has been set than use a default.
-	if ($from == '' or $from == NULL){
+  //If no from address has been set than use a default.
+  if ($from == '' or $from == NULL){
     $from = variable_get('site_mail', 'test@example.com'); //If no from can be found use site_mail variable.
-	}
-
-	//Set the correct protocol prefix to append to the smtp host.
-	switch(variable_get('smtp_protocol', 'standard')){
-	  case "ssl":
-	    $mail->Protocol = 'ssl://';
-	    break;
-	  case "tls":
-	    $mail->Protocol = 'tls://';
-	    break;
-	  case "standard":
-	    $mail->Protocol = '';
-	}
-	
-	$mail->Host = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
-	$mail->Port = variable_get('smtp_port', '25');
-	$mail->Mailer = "smtp";
-	$mail->SMTPAuth = $auth;
-	$mail->Username = $username;
-	$mail->Password = $password;
-	$mail->CharSet = 'utf-8';
-	
-	$mail->From = $from;
-	$mail->FromName = $from_name;
-
-  $torecipients = split(",", $to);
+  }
+  
+  //Set the correct protocol prefix to append to the smtp host.
+  switch(variable_get('smtp_protocol', 'standard')) {
+    case "ssl":
+      $mail->Protocol = 'ssl://';
+      break;
+    case "tls":
+      $mail->Protocol = 'tls://';
+      break;
+    case "standard":
+      $mail->Protocol = '';
+  }
+  
+  $mail->Host = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
+  $mail->Port = variable_get('smtp_port', '25');
+  $mail->Mailer = "smtp";
+  $mail->SMTPAuth = $auth;
+  $mail->Username = $username;
+  $mail->Password = $password;
+  $mail->CharSet = 'utf-8';
+  
+  $mail->From = $from;
+  $mail->FromName = $from_name;
+  
+  $torecipients = split(",", $message['to']);
   foreach ($torecipients as $torecipient) {
     if (strpos($torecipient, '<') !== false) {
       $toparts = explode(" <", $torecipient);
@@ -276,21 +284,20 @@
     $mail->AddAddress($toaddr, $toname);
   }
 
-	$mail->Subject = $subject;
-	$mail->Body = $body;
-	
-  watchdog('smtp', t('Sending mail to: !to', array('!to' => $to)));
-
-	//Try to send email, if it fails set watchdog entry.
-	if(!$mail->Send()) {
-    watchdog("smtp", t('Error sending e-mail from !from to !to: ', array('!from' => $from, '!to' => $to)) . $mail->ErrorInfo, WATCHDOG_ERROR);
-		return false;
-	}
-	
-	$mail->SmtpClose();
-	return true;
-}
+  $mail->Subject = $message['subject'];
+  $mail->Body = $message['body'];
+  
+  watchdog('smtp', t('Sending mail to: !to', array('!to' => $message['to'])));
 
+  //Try to send email, if it fails set watchdog entry.
+  if(!$mail->Send()) {
+    watchdog("smtp", t('Error sending e-mail from !from to !to: ', array('!from' => $from, '!to' => $message['to'])) . $mail->ErrorInfo, NULL, WATCHDOG_ERROR);
+    return false;
+  }
+  
+  $mail->SmtpClose();
+  return true;
+}
 
 /**
  * PHPMailer language settings.  
