From 53445866351318395d5c8db4a0e12f706c4cf9ad Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Thu, 21 Apr 2011 11:24:27 -0400
Subject: [PATCH] Issue #1088914 by pillarsdotnet: Integrate with Mail System module.

---
 README.txt                  |   10 ++----
 includes/mimemail.admin.inc |   10 ++----
 mimemail.info               |    1 +
 mimemail.install            |   70 +++++++++++++++++++++++++++++++++++-------
 4 files changed, 67 insertions(+), 24 deletions(-)

diff --git a/README.txt b/README.txt
index 272541395cc45eb19e2327bcf3d7eb664e1b34a6..b089ef31dff84dad11cef12371d4af14f13eb214 100644
--- a/README.txt
+++ b/README.txt
@@ -19,15 +19,13 @@ USAGE
   MimeMailSystem as the responsible mail system for a particular message
   or all mail sent by one module.

-  This is done by modifying the $mail_system variable like this:
+  This can be done through the web by visiting admin/config/system/mailsystem
+  or in a program as follows:

-  array(
-    'default-system' => 'DefaultMailSystem',
+  mailsystem_set(array(
     '{$module}_{$key}' => 'MimeMailSystem', // Just messages with $key sent by $module.
     '{$module}' => 'MimeMailSystem', // All messages sent by $module.
-  );
-
-  WARNING! Make sure to not overwrite the variable just append your mail to it!
+  ));

   You can use the following optional parameters to build the e-mail:
     'plain':
diff --git a/includes/mimemail.admin.inc b/includes/mimemail.admin.inc
index 5180b28e82ca1815565c1f764197e97423204faf..faa1e3f12ce9c692b1f4a0f4a24408fc5a5353f9 100644
--- a/includes/mimemail.admin.inc
+++ b/includes/mimemail.admin.inc
@@ -146,16 +146,14 @@ function mimemail_admin_settings() {
  * Handle submission of the configuration form.
  */
 function mimemail_admin_settings_form_submit($form, &$form_state) {
-  $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
-  $default_system = $mail_system['default-system'];
+  $mail_system = mailsystem_get();
+  $default_system = $mail_system[mailsystem_default_id()];
   $mimemail_alter = $form_state['values']['mimemail_alter'];
   // Set as default mail system if Mime Mail is chosen to handle all mail.
   if ($mimemail_alter && $default_system != 'MimeMailSystem') {
-    $default_system = 'MimeMailSystem';
+    mailsystem_set(array(mailsystem_default_id() => 'MimeMailSystem'));
   }
   elseif (!$mimemail_alter && $default_system == 'MimeMailSystem') {
-    $default_system = 'DefaultMailSystem';
+    mailsystem_set(mailsystem_defaults());
   }
-  $mail_system['default-system'] = $default_system;
-  variable_set('mail_system', $mail_system);
 }
diff --git a/mimemail.info b/mimemail.info
index 6a2a8effba5aece4e39b47c403195f700334fd47..ba0924d3d0adaa39e677d847b47b56bee234490e 100644
--- a/mimemail.info
+++ b/mimemail.info
@@ -1,5 +1,6 @@
 name = Mime Mail
 description = Send MIME-encoded emails with embedded images and attachments.
+dependencies[] = mailsystem
 package = Mail
 core = 7.x

diff --git a/mimemail.install b/mimemail.install
index 4e50acac7c6addfeefed9e4d992a6bdc87507956..a2e413d2bc4088ca988ddc62a20da563e0351dae 100644
--- a/mimemail.install
+++ b/mimemail.install
@@ -6,25 +6,24 @@
  */

 /**
- * Implements hook_install().
+ * Implements hook_enable().
  */
-function mimemail_install() {
-  $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
-  $mail_system['mimemail'] = 'MimeMailSystem';
-  variable_set('mail_system', $mail_system);
+function mimemail_enable() {
+  module_load_include('module', 'mailsystem');
+  mailsystem_set(
+    array(
+      mailsystem_default_id() => 'MimeMailSystem',
+      'mimemail' => 'MimeMailSystem',
+    )
+  );
 }

 /**
  * Implements hook_disable().
  */
 function mimemail_disable() {
-  $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
-  $default_system = $mail_system['default-system'];
-  if ($default_system == 'MimeMailSystem') {
-    $mail_system['default-system'] = 'DefaultMailSystem';
-    variable_set('mail_system', $mail_system);
-    variable_set('mimemail_alter', FALSE);
-  }
+  mailsystem_clear(array('mimemail' => 'MimeMailSystem'));
+  variable_set('mimemail_alter', FALSE);
 }

 /**
@@ -48,3 +47,50 @@ function mimemail_uninstall() {
     variable_del($variable);
   }
 }
+
+/**
+ * Implements hook_requirements().
+ *
+ * Ensures that the newly-required mailsystem module is available, or else
+ * disables the mimemail module and returns an informative error message.
+ */
+function mimemail_requirements($phase) {
+  if ($phase === 'install' || module_exists('mailsystem')) {
+    return array();
+  }
+  $args = array(
+    '!mailsystem' => url('http://drupal.org/project/mailsystem'),
+    '%mailsystem' => 'Mail System',
+    '!mimemail' => url('http://drupal.org/project/mimemail'),
+    '%mimemail' => 'Mime Mail',
+  );
+  if ( module_enable(array('mailsystem'))
+    && module_load_include('module', 'mailsystem')
+  ) {
+    drupal_set_message(
+      t('The %mailsystem module has been enabled because the %mimemail module now requires it.', $args)
+    );
+    return array();
+  }
+  return array(
+    'mimemail_mailsystem' => array(
+      'title' => t('%mailsystem module', $args),
+      'value' => t('Not installed'),
+      'description' => t(
+        'The <a href="!smtp">%mimemail</a> module dependencies have changed.  Please download and install the required <a href="!mailsystem">%mailsystem</a> module, then re-enable the <a href="!mimemail">%mimemail</a> module.', $args
+      ),
+      'severity' => REQUIREMENT_ERROR,
+    ),
+  );
+}
+
+/**
+ * Implements hook_update_N().
+ *
+ * @see mimemail_requirements().
+ */
+function mimemail_update_7000() {
+  if ($requirements = smtp_requirements('runtime')) {
+    throw new DrupalUpdateException($requirements['smtp_mailsystem']['description']);
+  }
+}
--
1.7.1

