From 6f4d09d8954cddbdf12522fd7dd6ab6cb831d90b Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Sun, 27 Feb 2011 15:14:57 -0500
Subject: [PATCH] Refactor mimemail().

---
 mimemail.module |  124 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 79 insertions(+), 45 deletions(-)

diff --git a/mimemail.module b/mimemail.module
index 7b3bb51..0044197 100644
--- a/mimemail.module
+++ b/mimemail.module
@@ -61,45 +61,6 @@ function mimemail_theme() {
   return mimemail_theme_theme();
 }
 
-/**
- * Sends a mime-encoded e-mail.
- *
- * This function first determines the mail engine to use, then prepares the
- * message by calling the mail engine's prepare function, or
- * mimemail_prepare_message() if another one does not exist, then sends the message.
- *
- * @param $sender
- *   The email address or user object who is sending the message.
- * @param $recipient+
- *   An email address or user object who is receiving the message.
- * @param $subject
- *   A subject line string.
- * @param $body
- *   The message body in HTML format.
- * @param $plaintext
- *   Whether to send the message as plaintext only or HTML. If set to 1, Yes
- *   or TRUE, then the message will be sent as plaintext.
- * @param $headers
- *   Optional e-mail headers in a keyed array.
- * @param $text
- *   Optional plaintext portion of a multipart e-mail.
- * @param $attachments
- *   An array of arrays which describe one or more attachments. The internal
- *   array consists of two parts: the file's path and the file's MIME type.
- *   The array of arrays looks something like this:
- *   Array
- *   (
- *     [0] => Array
- *       (
- *         [filepath] => '/path/to/file.name'
- *         [filemime] => 'mime/type'
- *       )
- *   )
- * @param $mailkey
- *   An identifier for the message.
- * @return
- *   An array containing the MIME encoded message, including headers and body.
- */
 function mimemail_prepare_message($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '') {
   module_load_include('inc', 'mimemail');
 
@@ -156,15 +117,69 @@ function mimemail_prepare_message($sender, $recipient, $subject, $body, $plainte
   return $message;
 }
 
-function mimemail($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '') {
-  $engine = variable_get('mimemail_engine', 'mimemail');
-  $mailengine = $engine .'_mailengine';
-  $engine_prepare_message = $engine .'_prepare_message';
+/**
+ * Sends a mime-encoded e-mail.
+ *
+ * This function first determines the mail engine to use, then prepares the
+ * message by calling the mail engine's prepare function, or
+ * mimemail_prepare_message() if another one does not exist, then sends the message.
+ *
+ * @param $sender
+ *   The email address or user object who is sending the message.
+ * @param $recipient+
+ *   An email address or user object who is receiving the message.
+ * @param $subject
+ *   A subject line string.
+ * @param $body
+ *   The message body in HTML format.
+ * @param $plaintext
+ *   Whether to send the message as plaintext only or HTML. If set to 1, Yes
+ *   or TRUE, then the message will be sent as plaintext.
+ * @param $headers
+ *   Optional e-mail headers in a keyed array.
+ * @param $text
+ *   Optional plaintext portion of a multipart e-mail.
+ * @param $attachments
+ *   An array of arrays which describe one or more attachments. The internal
+ *   array consists of two parts: the file's path and the file's MIME type.
+ *   The array of arrays looks something like this:
+ *   Array
+ *   (
+ *     [0] => Array
+ *       (
+ *         [filepath] => '/path/to/file.name'
+ *         [filemime] => 'mime/type'
+ *       )
+ *   )
+ * @param $mailkey
+ *   An identifier for the message.
+ * @param $send
+ *   Send the message directly.
+ * @return
+ *   An array containing the MIME encoded message, including headers and body.
+ */
+function mimemail($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '', $send = TRUE) {
 
-  if (!$engine || !function_exists($mailengine)) {
-    return FALSE;
+  // Prepare e-mail.
+  $message = mimemail_prepare($sender, $recipient, $subject, $body, $plaintext, $headers, $text, $attachments, $mailkey);
+
+  // Optionally send e-mail.
+  if ($send) {
+    $message['result'] = mimemail_send($message);
   }
 
+  return $message;
+}
+
+/**
+ * Prepare the message. Same arguments as mimemail() except $send.
+ *
+ * @return
+ *   An array containing the MIME encoded message, including headers and body.
+ */
+function mimemail_prepare($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '') {
+  $engine_prepare_message = variable_get('mimemail_engine', 'mimemail') .'_prepare_message';
+
   if (function_exists($engine_prepare_message)) {
     $message = $engine_prepare_message($sender, $recipient, $subject, $body, $plaintext, $headers, $text, $attachments, $mailkey);
   }
@@ -172,6 +187,25 @@ function mimemail($sender, $recipient, $subject, $body, $plaintext = NULL, $head
     $message = mimemail_prepare_message($sender, $recipient, $subject, $body, $plaintext, $headers, $text, $attachments, $mailkey);
   }
 
+  return $message;
+}
+
+/**
+ * Send the message.
+ *
+ * @param $message
+ *   The message array.
+ * @return
+ *   TRUE on success, FALSE on failure.
+ */
+function mimemail_send($message) {
+  $engine = variable_get('mimemail_engine', 'mimemail');
+  $mailengine = $engine .'_mailengine';
+
+  if (!$engine || !function_exists($mailengine)) {
+    return FALSE;
+  }
+
   return $mailengine('send', $message);
 }
 
-- 
1.5.6.5

