From 9cb0780e44e70c186968d67622ef86bd3c3261bb Mon Sep 17 00:00:00 2001
From: Caio SBA <caiosba@gmail.com>
Date: Wed, 1 Aug 2012 00:52:07 -0300
Subject: [PATCH] Fix from and support queue

---
 smtp.module |   36 ++++++++++++++++++++++++++----------
 1 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/smtp.module b/smtp.module
index 694516f..42b6cb8 100644
--- a/smtp.module
+++ b/smtp.module
@@ -315,9 +315,9 @@ function smtp_drupal_mail_wrapper($message) {
 
   if ($from == NULL || $from == '') {
     // If from e-mail address is blank, use smtp_from config option.
-    if ($from = variable_get('smtp_from', '') == '') {
+    if (($from = variable_get('smtp_from', '')) == '') {
       // If smtp_from config option is blank, use site_email.
-      if ($from = variable_get('site_email', '') == '') {
+      if (($from = variable_get('site_mail', '')) == '') {
         drupal_set_message(t('There is no submitted from address.'), 'error');
         watchdog('smtp', 'There is no submitted from address.', array(), WATCHDOG_ERROR);
         return FALSE;
@@ -719,20 +719,36 @@ function smtp_drupal_mail_wrapper($message) {
   $mail->Mailer = 'smtp';
 
 
-  // Let the people know what is going on.
-  watchdog('smtp', 'Sending mail to: @to', array('@to' => $to));
-
+  // Try to queue e-mail.
+  if (function_exists('job_queue_add')) {
+    watchdog('smtp', 'Queueing mail to: @to', array('@to' => $to));
+    job_queue_add('smtp_mail_send', t('Queued email'), array($mail), drupal_get_path('module', 'smtp') .'/phpmailer/class.phpmailer.php');
+  }
   // Try to send e-mail. If it fails, set watchdog entry.
-  if (!$mail->Send()) {
-    watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mail->ErrorInfo), WATCHDOG_ERROR);
-    return FALSE;
+  else {
+    watchdog('smtp', 'Sending mail to: @to', array('@to' => $to));
+    if (!$mail->Send()) {
+      watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mail->ErrorInfo), WATCHDOG_ERROR);
+      return FALSE;
+    }
+    $mail->SmtpClose();
   }
 
-  $mail->SmtpClose();
   return TRUE;
 }  //  End of smtp_drupal_mail_wrapper().
 
-
+/**
+ * Send an e-mail. 
+ */
+function smtp_mail_send($mail) {
+  if (!$mail->Send()) {
+    watchdog('smtp', 'Error sending e-mail: !error_message', array('!error_message' => $mail->ErrorInfo), WATCHDOG_ERROR);
+  }
+  else {
+    watchdog('smtp', 'Sent queued mail');
+    $mail->SmtpClose();
+  }
+}
 
 /**
  * Implementation of hook_mail().
-- 
1.7.2.5

