diff --git a/mimemail.inc b/mimemail.inc
index 8f40c01..abd617b 100644
--- a/mimemail.inc
+++ b/mimemail.inc
@@ -223,6 +223,19 @@ function _mimemail_imagecache($url) {
 }
 
 /**
+ * Helper function to get or store the current mailkey.
+ */
+function _mimemail_mailkey($value = NULL) {
+  static $mailkey = '';
+
+  if ($value !== NULL) {
+    $mailkey = $value;
+  }
+
+  return $mailkey;
+}
+
+/**
  * Helper function to build multipart messages.
  *
  * @param $parts
@@ -630,6 +643,10 @@ function _mimemail_url($url, $embed_file = FALSE) {
     'prefix' => $prefix,
   );
 
+  $data = array('path' => $path);
+  $data['__drupal_alter_by_ref'] = array(&$options);
+  drupal_alter('mimemail_url', $data);
+  $path = $data['path'];
+
   $url = url($path, $options);
 
   // If url() added a ?q= where there should not be one, remove it.
diff --git a/mimemail.module b/mimemail.module
index a3f156d..a441e4d 100644
--- a/mimemail.module
+++ b/mimemail.module
@@ -148,6 +148,9 @@ function mimemail_mail($key, &$message, $params) {
 function mimemail_prepare_message($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '') {
   module_load_include('inc', 'mimemail');
 
+  // Store current mailkey for later use.
+  _mimemail_mailkey($mailkey);
+
   $site_name = variable_get('site_name', 'Drupal');
   $site_mail = variable_get('site_mail', ini_get('sendmail_from'));
 
