for some reason unknown to me, sending mail with images didn't work for me.
the preg_replace expression always got me \-escaped double-quotes around the image src-attribute, which resulted in no image displayed ...

changing mimemail.inc a bit with the following got rid of the problem for me:

function mimemail_extract_files($html, $name) {
  $pattern = '/(<link[^>]+href="?|<object[^>]+codebase="?|@import |src="?)\/?([^"]+)("?)/mis';
  $html = preg_replace_callback($pattern, '_mimemail_extract_files_rep_cb', $html);

  $document = array(array(
    'Content-Type' => 'text/html; charset=utf-8',
    'Content-Transfer-Encoding' => 'base64',
    'content' => chunk_split(base64_encode($html)),
  ));

  $files = _mimemail_file();

  return array_merge($document, $files);
}

function _mimemail_extract_files_rep_cb(&$m) {
  return "$m[1]"._mimemail_file($m[2]).$m[3];
}

and I got 2 more questions:

  1. is it necessary to always encode the html body with base64?
  2. wouldn't it be a good idea to have an option to not send the images inline, but convert the links to absolute server references?

    smaller mails would be a nice thing sometimes ...
CommentFileSizeAuthor
#23 mimemail.114312_01.patch509 bytessgabe
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ray007’s picture

no idea what went wrong yesterday, but just 2 minutes ago the old preg_replace() call also worked out right.

which only leaves the change to the 'Content-Type' line, that one seems to be necessary:
'Content-Type' => 'text/html; charset=utf-8', and no name at the end.

ray007’s picture

I just got to another problem with attaching images to the mail:

We have an installation at a subdirectory of httpdocs, so the url for the drupal-installation is http://hostname.com/d5/
When generating a newsletter with tinyMCE and inserting an image, the src-attribute of the image is "/d5/files/myimage.jpg".

Since this is no path relative to the drupal-installtion, the file_exists() check in _mimemail_file() fails and the mail only contains a broken image, since "/d5/files/myimage.jpg" is no valid remote url.

I'll try to come up with a fix soon, if nobody else beats me to it.

ray007’s picture

here a little patch that seems to do the right thing:

--- mimemail/mimemail.inc   2007-01-27 16:48:57.600973750 +0100
+++ mimemail/mimemail.inc       2007-01-31 15:21:55.805282000 +0100
@@ -166,12 +166,12 @@
  *                  )
  */
 function mimemail_extract_files($html, $name) {
-  $pattern = '/(<link[^>]+href="?|<object[^>]+codebase="?|@import |src="?)\/?([^"]+)("?)/emis';
+  $pattern = '/(<link[^>]+href="?|<object[^>]+codebase="?|@import |src="?)([^"]+)("?)/emis';
   $html = preg_replace($pattern,'"\\1". _mimemail_file("\\2") ."\\3"', $html);

   $document = array(array(
     'Content-Type' => 'text/html; charset=utf-8',
     'Content-Transfer-Encoding' => 'base64',
     'content' => chunk_split(base64_encode($html)),
   ));
@@ -191,6 +191,9 @@
 function _mimemail_file($file = NULL, $type = '') {
   static $files = array();

+  if($file[0] == '/') { // convert absolute uri to relative path
+    $file = substr($file, strlen(base_path()));
+  }
   if ($file && !preg_match('@://|mailto:@', $file) && file_exists($file)) {
     $content_id = md5($file) .'@'. $_SERVER['HTTP_HOST'];
ray007’s picture

Nobody else experiencing problem with sending images, when drupal is installed in a subdirectory of a site?

Allie Micka’s picture

I don't doubt that you're experiencing this issue, and your solution looks correct.

I think that the number of people who run Drupal in a subdirectory AND have mimemail installend AND are sending images AND have the wherewithal to notice these things. Well, I think that number is pretty small.

I don't have the time to test this right away, but if someone else can RTBC this I would be most grateful.

ray007’s picture

*Bump*

Is there anything I can do to make you more comfortable with this patch?

kingandy’s picture

wouldn't it be a good idea to have an option to not send the images inline, but convert the links to absolute server references?

I'd just like to add my voice to this, and add the thought that perhaps any links to files that aren't found should be converted to absolute server references as well (assuming they aren't already absolute URIs). In theory it should be as simple as prepending it with a $_SERVER['SERVER_NAME'] or something, whenever there's no '://' in the link string ... shouldn't it?

kingandy’s picture

add the thought that perhaps any links to files that aren't found should be converted to absolute server references as well

Oh, wait, that already happens in _mimemail_file. Never mind. I still agree the option to switch off image encoding, though!

kingandy’s picture

Wait, no it doesn't.

Note to self, finish reading code before commenting. Sorry...

kingandy’s picture

FWIW - if you'll forgive me adding yet another followup to this issue - I installed the above patch on my drupal install and initially it appears to work perfectly. Thanks!

yurtboy’s picture

what is the proper way to apply this?
Sorry if I should know.
My drupal 5x install is doing the same thing. Send html but no images. BUT if you look at the source of the email there is not even a reference to the image even though the page it makes online is fine.
Thanks. Below is the source code to the email if it helps?
Also I believe I set all the needed "Input" setting to Non-filtered html if that matters?
Alfred Nutile

----------------start email source-------------------------------
Return-Path:
X-Spam-Checker-Version: SpamAssassin 3.1.8 (2007-02-13) on
server1.rivervalleywebhosting.com
X-Spam-Level: *
X-Spam-Status: No, score=1.2 required=2.2 tests=AWL,HTML_IMAGE_ONLY_12,
HTML_MESSAGE,NO_RELAYS,UPPERCASE_25_50 autolearn=no version=3.1.8
X-Original-To: alfred@rivervalleytechsupport.com
Delivered-To: web4_alfred@server1.rivervalleywebdesign.com
Received: by server1.rivervalleywebhosting.com (Postfix)
id 7617DB293B1; Sun, 10 Jun 2007 19:00:38 -0400 (EDT)
Delivered-To: web1_alfred@server1.rivervalleywebdesign.com
Received: by server1.rivervalleywebhosting.com (Postfix)
id 6DFA2B293B5; Sun, 10 Jun 2007 19:00:38 -0400 (EDT)
Delivered-To: web5_alfred@server1.rivervalleywebdesign.com
Received: by server1.rivervalleywebhosting.com (Postfix, from userid 33)
id 54883B293B2; Sun, 10 Jun 2007 19:00:38 -0400 (EDT)
To: alfred@rivervalleytechsupport.com
Subject: [The End of money newsletter] Testing HTML with and Image
Errors-To: support@theendofmoney.com
From: "The End of money"
X-Security: MIME headers sanitized on server1.rivervalleywebhosting.com
See http://www.impsec.org/email-tools/sanitizer-intro.html
for details. $Revision: 1.138 $Date: 2003-01-26 11:25:54-08
X-Security: The postmaster has not enabled quarantine of poisoned messages.
Content-Type: multipart/alternative;
charset=utf-8;
boundary="878090a81d585f97f05e9edd170a486e"
Reply-To: support@theendofmoney.com
X-Mailer: Drupal
MIME-Version: 1.0
Message-Id: <20070610230038.54883B293B2@server1.rivervalleywebhosting.com>
Date: Sun, 10 Jun 2007 19:00:38 -0400 (EDT)

This is a multi-part message in MIME format.

--878090a81d585f97f05e9edd170a486e
Content-Type: multipart/alternative; boundary="0098ff8b7897d880a02b918484ae2205"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit

--0098ff8b7897d880a02b918484ae2205
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit

The End of money newsletter: Testing HTML with and Image

THIS IS THE DEFAULT HEADER

TESTING HTML WITH AND IMAGE
============================================================

image is to the left

THIS IS THE DEFAULT FOOTER

--
Footer will be appended here
--0098ff8b7897d880a02b918484ae2205
Content-Type: text/html; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: base64

PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEgVHJhbnNpdGlvbmFs
Ly9FTiI+CjxIVE1MPgo8SEVBRD4KPE1FVEEgSFRUUC1FUVVJVj0iQ29udGVudC1UeXBlIiBDT05U
RU5UPSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiPgo8VElUTEU+VGhlIEVuZCBvZiBtb25leSBu
ZXdzbGV0dGVyOiBUZXN0aW5nIEhUTUwgd2l0aCBhbmQgSW1hZ2U8L1RJVExFPgo8L0hFQUQ+CjxC
T0RZIGJnY29sb3I9IiNmZmZmZmYiPgo8U1RZTEUgVFlQRT0idGV4dC9jc3MiPgo8IS0tCgotLT4K
PC9TVFlMRT4KPFRBQkxFIGNsYXNzPSJzaW1wbGVuZXdzLXRlbXBsYXRlLWJhY2tncm91bmQiIHdp
ZHRoPSIxMDAlIiBiZ2NvbG9yPSIjZmZmZmZmIiBjZWxscGFkZGluZz0iMCIgY2VsbHNwYWNpbmc9
IjAiPjxUUj48VEQgdmFsaWduPSJ0b3AiIGFsaWduPSJjZW50ZXIiPgo8VEFCTEUgY2xhc3M9InNp
bXBsZW5ld3MtdGVtcGxhdGUtY29udGVudCIgY2VsbHBhZGRpbmc9IjAiIGNlbGxzcGFjaW5nPSIw
Ij48VFI+PFREPgo8ZGl2IGNsYXNzPSJzaW1wbGVuZXdzLXRlbXBsYXRlLWhlYWRlciI+CjxwPlRI
SVMgSVMgVEhFIERFRkFVTFQgSEVBREVSPC9wPgoKPC9kaXY+CjxkaXYgY2xhc3M9InNpbXBsZW5l
d3MtdGVtcGxhdGUtYm9keSI+CjxoMT5UZXN0aW5nIEhUTUwgd2l0aCBhbmQgSW1hZ2U8L2gxPgo8
cD48aW1nIHdpZHRoPSIxOTQiIGhlaWdodD0iMTg5IiBzcmM9ImNtcy9maWxlcy9pbWFnZS9sb2dv
LnBuZyIgYWx0PSIiIC8+aW1hZ2UgaXMgdG8gdGhlIGxlZnQ8L3A+Cgo8L2Rpdj4KPGRpdiBjbGFz
cz0ic2ltcGxlbmV3cy10ZW1wbGF0ZS1mb290ZXIiPgo8cD5USElTIElTIFRIRSBERUZBVUxUIEZP
T1RFUjwvcD4KCjwvZGl2PgoKPC9URD48L1RSPjwvVEFCTEU+CjwvVEQ+PC9UUj48L1RBQkxFPgo8
L0JPRFk+CjwvSFRNTD4KCi0tIApGb290ZXIgd2lsbCBiZSBhcHBlbmRlZCBoZXJl

--0098ff8b7897d880a02b918484ae2205--

--878090a81d585f97f05e9edd170a486e--

jsloan’s picture

I was having the same problem and this patch works fine on my install of 5.1 and I do use clean url's and sites as subdirectories.

Also; today I posted a patch that fixes the _mimemail_url bug [ http://drupal.org/node/133107 ]

can somebody test and then can these patches be applied to HEAD?

... thanks for the patch ray007!

ShutterFreak’s picture

Just an update: the patch posted in #3 is not yet complete.

I get a file_get_contents(.) error when clicking on "Request new password" when I apply that patch.

What seems to solve the problem for me (and in addition stops sending zero-length file attachments to email messages), is the following version of _mimemail_file():

function _mimemail_file($file = NULL, $type = '', $disposition = 'related') {
  static $files = array();

  // Added the following 3 lines (see node/114312 #13)
  if ($file == '.' ) {
	  $file = NULL;
  }
  // Added the following 3 lines (see node/114312 #3)
  if ($file[0] == '/') { // convert absolute uri to relative path
    $file = substr($file, strlen(base_path()));
  }
  if ($file && !preg_match('@://|mailto:@', $file) && file_exists($file)) {
    $content_id = md5($file) .'@'. $_SERVER['HTTP_HOST'];

    $new_file = array('name' => substr($file, strrpos($file, '/') + 1),
                      'file' => $file,
                      'Content-ID' => $content_id,
                      'Content-Disposition' => $disposition,
                     );
    $new_file['Content-Type'] = _mimemail_mimetype($file,$type);

    $files[] = $new_file;

    return 'cid:'. $content_id;
  }
  
  if ($file) {
    return $file;
  }
  
  $ret = $files;
  $files = array();
  return $ret;
}

Best regards,

Olivier

ray007’s picture

It doesn't make sense to try to attach a file '.' - as it doesn't make sense to try to attach any directory.
But I think this check should come earlier and not in _mimemail_file().
And the check should probably be is_file() or !is_dir() ...

ShutterFreak’s picture

Maybe the checks should go into a new function file_is_valid_attachment()?

jerdavis’s picture

Status: Needs review » Needs work

If someone can roll a final version of the patch based on the comments on this issue I can test it.

Allie Micka’s picture

Status: Needs work » Closed (fixed)

We had a pile of path-related changes last winter, and there's no longer any traffic on this issue. I'm optimistically marking this as closed.

Please re-open if you can confirm it's still an issue and/or provide a patch.

Thanks!

allartk’s picture

Status: Closed (fixed) » Needs work

hi,

I have drupal in a subdirectory and and... I think this bug is still in the current version.

The path to the image in the mail send is
file://drupal/sites/vogeldagboek.nl/files/image/test/PICT0016.JPG
of course that's not correct..

Greetings

allartk’s picture

Hi,

adding

global $base_path;
$file = str_replace(ltrim ($base_path,"/"),"",$file); 

below

function _mimemail_file($file = NULL, $type = '', $disposition = 'related') {
  static $files = array();

fixes this issue. Otherwise file_exist will fail and the file is not attached.

jerdavis’s picture

Category: bug » support
Status: Needs work » Closed (fixed)

This should be fixed with changes committed to HEAD in http://drupal.org/cvs?commit=141336.

Please test on this version or re-test once a new release is built. If you still have issues, please let us know!

allartk’s picture

also in alpha 2 this is an issue

add
$file = str_replace(ltrim($base_path,"/"),"",$file);

below
$file = $url;

in the function _mimemail_file in mimemail.inc

allartk’s picture

Status: Closed (fixed) » Active
sgabe’s picture

Title: sending images » Embedded images when Drupal is in a subdirectory
Version: 5.x-1.x-dev » 6.x-1.0-alpha2
Category: support » bug
Status: Active » Needs review
FileSize
509 bytes

Changing title to a more descriptive one.
Attached patch applies to 6.x-1.0-alpha2.

allartk’s picture

It seems I was confused by another issue as described here: http://www.ecitadel.net/blog/2010/01/06/drupal-mimemail-missing-embedded...

Anyhow I reverted the above change I made to alpha 2.

sgabe’s picture

@allartk: So, is this issue still valid or not (or duplicate)? Please change the status, if needed.

ikeigenwijs’s picture

subscribe

sgabe’s picture

Status: Needs review » Closed (fixed)

No activity and it seems to be fixed. Closing issue.