diff --git a/includes/mimemail.rules.inc b/includes/mimemail.rules.inc
index a0b04bd..93b8c1f 100644
--- a/includes/mimemail.rules.inc
+++ b/includes/mimemail.rules.inc
@@ -38,29 +38,32 @@ function mimemail_rules_action_info() {
  * Action Implementation: Send a mail to a user and to an arbitrary mail address.
  */
 function mimemail_rules_action_mail_to_user($user, $settings) {
+  global $language;
+
   // Process settings.
   $settings = mimemail_rules_form_settings_process($settings);
   $from = $settings['from'];
-  $subject = $settings['subject'];
-  $body = $settings['message_html_filter']['message_html'];
-  $plaintext = $settings['message_plaintext'];
-  $attachments = $settings['attachments'];
-  $mailkey = 'mimemail_rules_action_mail';
 
   // If recipient field is empty send it to given user object.
   $to = empty($settings['to']) ? $user->mail : implode(',', $settings['to']);
 
-  $headers = array();
-  if (!empty($settings['bcc'])) {
-    $headers['Bcc'] = implode(',', $settings['bcc']);
-  }
-  if (!empty($settings['cc'])) {
-    $headers['Cc'] = implode(',', $settings['cc']);
-  }
-
-  $status = mimemail($from, $to, $subject, $body, NULL , $headers, $plaintext, $attachments, $mailkey);
+  // Prepare the message but not send.
+  $message = drupal_mail('mimemail', 'rules_action_mail', $to, $language, $settings, $from, FALSE);
+
+  // Send the prepared message.
+  $message = mimemail(
+    $message['from'],
+    $message['to'],
+    $message['subject'],
+    $message['body'],
+    NULL ,
+    $message['headers'],
+    $message['params']['plaintext'],
+    $message['params']['attachments'],
+    $message['id']
+  );
 
-  if (!empty($status)) {
+  if ($message['result']) {
     $recipients = array_merge(explode(',', $to), $settings['bcc'], $settings['cc']);
     watchdog('rules', 'HTML mail successfully sent to %recipient', array('%recipient' => implode(', ', $recipients)));
   }
@@ -92,19 +95,23 @@ function mimemail_rules_action_mail_to_users_of_role($settings) {
 
   // Process settings.
   $settings = mimemail_rules_form_settings_process($settings);
-  $from = $settings['from'];
-  $subject = $settings['subject'];
-  $body = $settings['message_html_filter']['message_html'];
-  $plaintext = $settings['message_plaintext'];
-  $attachments = $settings['attachments'];
-  $mailkey = 'mimemail_rules_action_role_mail';
 
   // Now, actually send the mails.
-  $status = TRUE;
-  while (($account = db_fetch_object($result)) && !empty($status)) {
-    $status = mimemail($from, $account->mail, $subject, $body, NULL, array(), $plaintext, $attachments, $mailkey);
+  while (($account = db_fetch_object($result))) {
+    $message = drupal_mail('mimemail', 'rules_action_role_mail',  $account->mail, user_preferred_language($account), array(), NULL, FALSE);
+    $message = mimemail(
+      $message['from'],
+      $message['to'],
+      $message['subject'],
+      $message['body'],
+      NULL ,
+      $message['headers'],
+      $message['params']['plaintext'],
+      $message['params']['attachments'],
+      $message['id']
+    );
   }
-  if (!empty($status)) {
+  if ($message['result']) {
     $roles = array_intersect_key(user_roles(TRUE), drupal_map_assoc($recipients));
     watchdog('rules', 'HTML mail successfully sent to role(s) %roles.', array('%roles' => implode(', ', $roles)));
   }
@@ -131,21 +138,6 @@ function mimemail_rules_form_settings_process($settings) {
   }
 
   $settings['from'] = str_replace(array("\r", "\n"), '', $settings['from']);
-  $settings['subject'] = str_replace(array("\r", "\n"), '', $settings['subject']);
-
-  $attachments = array();
-  $attachments_string = trim($settings['attachments']);
-  if (!empty($attachments_string)) {
-    $attachment_lines = array_filter(explode("\n", trim($attachments_string)));
-    foreach ($attachment_lines as $key => $attachment_line) {
-      $attachment = explode(":", trim($attachment_line), 2);
-      $attachments[] = array(
-        'filepath' => $attachment[1],
-        'filemime' => $attachment[0],
-      );
-    }
-  }
-  $settings['attachments'] = empty($attachments[0]['filepath']) ? array() : $attachments;
 
   return $settings;
 }
diff --git a/mimemail.module b/mimemail.module
index 9b10615..7a6209b 100644
--- a/mimemail.module
+++ b/mimemail.module
@@ -62,6 +62,66 @@ function mimemail_theme() {
 }
 
 /**
+ * Implementation of hook_mail().
+ *
+ * Set's the message subject and body as configured in the $settings of the action.
+ */
+function mimemail_mail($key, &$message, $params) {
+  $account = $params['account'];
+  $variables = array(
+    '%site_name' => variable_get('site_name', 'Drupal'),
+    '%username' => $account->name,
+  );
+  // Replace variables.
+  if (isset($params['node']) && is_object($params['node'])) {
+    $node = $params['node'];
+    $variables += array(
+      '%author' => $account->mail,
+      '%uid' => $node->uid,
+      '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)),
+      '%node_type' => $node->type,
+      '%title' => $node->title,
+      '%teaser' => $node->teaser,
+      '%body' => $node->body,
+    );
+  }
+
+  // Prepare the array of the attachments.
+  $attachments = array();
+  $attachments_string = trim($params['attachments']);
+  if (!empty($attachments_string)) {
+    $attachment_lines = array_filter(explode('\n', trim($attachments_string)));
+    foreach ($attachment_lines as $key => $attachment_line) {
+      $attachment = explode(':', trim($attachment_line), 2);
+      $attachments[] = array(
+        'filepath' => $attachment[1],
+        'filemime' => $attachment[0],
+      );
+    }
+  }
+
+  // We also handle CC and BCC if it's set.
+  $params['cc'] = empty($params['cc']) ? array() : explode(',', $params['cc']);
+  $params['bcc'] = empty($params['bcc']) ? array() : explode(',', $params['bcc']);
+
+  if (!empty($params['cc'])) foreach ($params['cc'] as $key => $address) {
+    $params['cc'][$key] = str_replace(array("\r", "\n"), '', trim($address));
+    $message['headers']['Cc'] = implode(',', $params['cc']);
+  }
+  if (!empty($params['bcc'])) foreach ($params['bcc'] as $key => $address) {
+    $params['bcc'][$key] = str_replace(array("\r", "\n"), '', trim($address));
+    $message['headers']['Bcc'] = implode(',', $params['bcc']);
+  }
+
+  $message['to'] = strtr($message['to'], $variables);
+  $message['subject'] = strtr($params['subject'], $variables);
+  $message['body'][] = strtr(filter_xss_admin($params['message_html']), $variables);
+
+  $message['params']['plaintext'] = strtr($params['message_plaintext'], $variables);
+  $message['params']['attachments'] = $attachments;
+}
+
+/**
  * Default engine's prepare function.
  *
  * @param
diff --git a/modules/mimemail_action/mimemail_action.module b/modules/mimemail_action/mimemail_action.module
index 78aec10..fd96b65 100644
--- a/modules/mimemail_action/mimemail_action.module
+++ b/modules/mimemail_action/mimemail_action.module
@@ -61,71 +61,42 @@ function mimemail_send_mail_action(&$object, $context = array()) {
       $node = $object;
   }
 
-  $recipient = $context['to'];
-  $subject = $context['subject'];
-  $body = $context['message_html'];
-  $text = $context['message_plaintext'];
-  $mailkey = 'mimemail_action_send_mail';
-
-  $headers = array();
-  if (!empty($context['bcc'])) {
-    $headers['Bcc'] = $context['bcc'];
-  }
-  if (!empty($context['cc'])) {
-    $headers['Cc'] = $context['cc'];
-  }
-
-  // Prepare the array of the attachments.
-  $attachments = array();
-  $attachments_string = trim($context['attachments']);
-  if (!empty($attachments_string)) {
-    $attachment_lines = array_filter(explode('\n', trim($attachments_string)));
-    foreach ($attachment_lines as $key => $attachment_line) {
-      $attachment = explode(':', trim($attachment_line), 2);
-      $attachments[] = array(
-        'filepath' => $attachment[1],
-        'filemime' => $attachment[0],
-      );
-    }
-  }
+  $to = $context['to'];
 
   if (isset($node)) {
     if (!isset($account)) {
       $account = user_load(array('uid' => $node->uid));
     }
+    if ($to == '%author') {
+      $to = $account->mail;
+    }
   }
 
   if (!isset($account)) {
     $account = $user;
   }
 
-  // Set site wide variables.
-  $site_name = variable_get('site_name', 'Drupal');
-  $from = variable_get('site_mail', ini_get('sendmail_from'));
-
-  // Replace variables.
-  if (isset($node) && is_object($node)) {
-    $variables = array(
-      '%author' => $account->mail,
-      '%site_name' => $site_name,
-      '%username' => $account->name,
-      '%uid' => $node->uid,
-      '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)),
-      '%node_type' => $node->type,
-      '%title' => $node->title,
-      '%teaser' => $node->teaser,
-      '%body' => $node->body,
-    );
-    $recipient = strtr($recipient, $variables);
-    $subject = strtr($subject, $variables);
-    $body = strtr($body, $variables);
-    $text = strtr($text, $variables);
+  $params = array_merge(array('account' => $account, 'object' => $object), $context);
+  if (isset($node)) {
+    $params['node'] = $node;
   }
 
-  $status = mimemail($from, $recipient, $subject, $body, NULL, $headers, $text, $attachments, $mailkey);
+  $message = drupal_mail('mimemail', 'action_send_mail', $to, user_preferred_language($account), $params, NULL, FALSE);
+  $message = mimemail(
+    $message['from'],
+    $message['to'],
+    $message['subject'],
+    $message['body'],
+    NULL ,
+    $message['headers'],
+    $message['params']['plaintext'],
+    $message['params']['attachments'],
+    $message['id']
+  );
+
   $recipients = trim(implode(', ', array_merge(explode(',', $recipient), explode(',', $context['bcc']), explode(',', $context['cc']))), ', ');
 
-  if ($status) {
+  if ($message['result']) {
     watchdog('action', 'Sent HTML email to %recipients', array('%recipients' => $recipients));
   }
   else {
