From f8b1cd8952d897a2cbb2a52dfc5be004fec558f1 Mon Sep 17 00:00:00 2001
From: Caio SBA <caiosba@gmail.com>
Date: Mon, 18 Feb 2013 00:19:23 -0300
Subject: [PATCH] Job queue support

---
 smtp.module |   32 ++++++++++++++++++++++++--------
 1 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/smtp.module b/smtp.module
index c8d9c82..bec8776 100644
--- a/smtp.module
+++ b/smtp.module
@@ -788,20 +788,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

