diff --git a/mimemail.inc b/mimemail.inc
index 83c95aa..a4f42f0 100644
--- a/mimemail.inc
+++ b/mimemail.inc
@@ -430,35 +430,33 @@ function mimemail_html_body($body, $subject, $plain = FALSE, $plaintext = NULL,
  *
  * @param string $url
  *   The file path.
+ * @param boolean $to_embed
+ *   (optional) Wheter the URL is used to embed the file. Defaults to NULL.
  *
  * @return string
  *   A processed URL.
  */
-function _mimemail_url($url, $embed_file = NULL) {
+function _mimemail_url($url, $to_embed = NULL) {
   global $base_url;
   $url = urldecode($url);
 
-  // If the URL is absolute or a mailto, return it as-is.
-  if (strpos($url, '://') !== FALSE || preg_match('!(mailto|callto|tel)\:!', $url)) {
-    $url = str_replace(' ', '%20', $url);
-    return $url;
-  }
-  // If the image embedding is disabled, return the absolute URL for the image.
-  elseif (variable_get('mimemail_linkonly', 0) && preg_match('!\.(png|gif|jpg|jpeg)!i', $url)) {
-    $url = $base_url . $url;
-    $url = str_replace(' ', '%20', $url);
-    return $url;
-  }
-
-  $url = preg_replace('!^' . base_path() . '!', '', $url, 1);
+  $to_link = variable_get('mimemail_linkonly', 0);
+  $is_image = preg_match('!\.(png|gif|jpg|jpeg)!i', $url);
+  $is_absolute = file_uri_scheme($url) != FALSE || preg_match('!(mailto|callto|tel)\:!', $url);
 
-  // If we're processing to embed the file, we're done here so return.
-  if ($embed_file) {
-    return $url;
+  if (!$to_embed) {
+    if ($is_absolute) {
+      return str_replace(' ', '%20', $url);
+    }
   }
-
-  if (!preg_match('!^\?q=*!', $url)) {
-    $strip_clean = TRUE;
+  else {
+    $url = preg_replace('!^' . base_path() . '!', '', $url, 1);
+    // Exclude images from embedding if needed.
+    if ($is_image && $to_link) {
+      $url = file_create_url($url);
+      $url = str_replace(' ', '%20', $url);
+    }
+    return $url;
   }
 
   $url = str_replace('?q=', '', $url);
@@ -500,7 +498,7 @@ function _mimemail_url($url, $embed_file = NULL) {
   $url = url($path, $options);
 
   // If url() added a ?q= where there should not be one, remove it.
-  if (isset($strip_clean) && $strip_clean) {
+  if (preg_match('!^\?q=*!', $url)) {
     $url = preg_replace('!\?q=!', '', $url);
   }
 
diff --git a/tests/mimemail.test b/tests/mimemail.test
index 05bae5c..e9d7bfc 100644
--- a/tests/mimemail.test
+++ b/tests/mimemail.test
@@ -41,6 +41,10 @@ class MimeMailUnitTestCase extends DrupalUnitTestCase {
   function testUrl() {
     $result = _mimemail_url('#');
     $this->assertIdentical($result, '#', 'Hash mark URL without fragment left intact.');
+
+    $expected = $url = 'public://' . $this->randomName() . ' '. $this->randomName() . '.' . $this->randomName(3);
+    $result = _mimemail_url($url, TRUE);
+    $this->assertIdentical($result, $expected, 'Space in the filename of the attachment left intact.');
   }
 
 }
