I am trying to send an e-mail with embedded images. In order to embed images, the "src" has to be a relative path. Also, there is an option in Mimemail module "Link images only" and I have it turned off, as default.
I've included two types of images: one non-styled (just the original image) and one styled (generated automatically by Drupal in "styles" directory).
In the node body, the non-styled image has the "src" value as:
src="/sites/files/image/newsletter-header_1.png"
and the styled image has the "src" value:
src="/sites/files/styles/newsletter/public/images/img1_1.jpg?itok=L7G7Chx8"
The itok is added since 7.20
As the result, when the mail is received, the first image is displayed at once (without clicking on "show images" in the email client) and the second image is not embedded at all (after clicking on "show images", the email html code shows img with malformed "src" as described below).
I noticed that the problem might be around line 178 in mimemail.inc.
In line
else {
$url = _mimemail_url($url, 'TRUE');
// The $url is absolute, we're done here.
$scheme = file_uri_scheme($url);
if ($scheme == 'http' || $scheme == 'https' || preg_match('!mailto:!', $url)) {
return $url;
}
// The $url is a non-local URI that needs to be converted to a URL.
else {
$file = (drupal_realpath($url)) ? drupal_realpath($url) : file_create_url($url);First, the $url gets the leading slash stripped and becomes "sites/files/styles/newsletter/public/images/img1_1.jpg?itok=L7G7Chx8".
Then, the file_create_url("sites/files/styles/newsletter/public/images/img1_1.jpg?itok=L7G7Chx8") returns this:
http://example.com/sites/files/styles/newsletter/public/images/img1_1.jpg%3Fitok%3DL7G7Chx8
And then, there is a check whether this is an existing file, which fails. When typing the addres with html entities in the browser address there is a blank screen with just one message: "Error generating image".
I've suspended the itok generation for now (by using $conf['image_allow_insecure_derivatives'] = TRUE;) and the mail is ok. But it's a security issue.
[Edit] I am not sure whether it is mimemail problem or other module problem.
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | fix-itok-images-2145659-20.patch | 1.74 KB | anybody |
| #20 | fix-itok-images-2145659-20.patch | 827 bytes | anybody |
| #9 | mimemail-2145659-9.patch | 1.17 KB | sgabe |
| #8 | mimemail_embedding-2145659.patch | 814 bytes | fatherguddha |
Comments
Comment #1
raincloud commentedComment #2
raincloud commentedComment #3
raincloud commentedComment #4
fatherguddha commentedThe problem appears to be that drupal_realpath($url) won't handle the new security key (?itok=L7G7Chx8).
Stripping off the parameter appears to solve the problem. I think this is safe when images are embedded (as opposed to linked).
I've replaced the following code which appears just a few lines above the code you referenced:
with:
Comment #5
sgabe commentedThis should be already fixed by #2152705: Images with 'itok' token not showing up. I am marking this as a duplicate.
Comment #6
fatherguddha commentedHmm...I was still experiencing this problem with the 7.x-1.x-dev build on January 19th, which did include the patch on issue 2152705.
Comment #7
sgabe commentedOn second fought, I see this is about embedding images, the other issue is about linking images, so this is a different issue after all. My bad, sorry.
Comment #8
fatherguddha commentedI've created a patch for the Feb 3rd 7.x-1.x-dev version. Hopefully the solution really is as straightforward as it seems.
Comment #9
sgabe commentedI think we can handle this in _mimemail_url() when returning an URL to embed a file. See the attached patch.
Comment #11
sgabe commentedTest does not run online, but still needs review.
Comment #13
fatherguddha commentedPatch at #9 works for me.
Comment #14
sgabe commentedCommitted, thanks!
Comment #15
fatherguddha commentedThanks for your work on this - appreciate it :)
Comment #17
matysek145 commentedHi!
So, maybe I don't get something, but how is removing itok from the url supposed to make my images embed correctly?
I use Mime Mail in Simplenews with Entity Reference and this is my case:
1) Create some nodes with images
2) Create Simplenews newsletter with previous nodes referenced. Images are shown with some image style.
3) So far so good. New styled images are generated and i can see them in sent (test) newsletter
4) But now i upload new image in one of the nodes. Newsletter is already generated, so i don't even visit its node page, so new image has no chance to generate its style.
5) Send newsletter again - there is no itok, so if image doesn't already exist, is not going to be created, and finally is not displayed in newsletter
When i commented this line
$url = preg_replace('/\\?itok=.*$/', '', $url);
everything started to work :)
So I'm pretty sure, we need this itok. Or, if there is something obvious i miss, please, tell me :)
Cheers!
Comment #18
sgabe commented@matysek145: This issue is about existing images. What you are looking for is in #1309248: Force generation of Image Styles.
Comment #19
killes@www.drop.org commentedI think this patch is not sufficient.
The check in line 170 of mimemail.inc still fails:
$image = preg_match('!\.(png|gif|jpg|jpeg)$!i', $url);
If I strip the itok parameter before calling this, it works in my case.
This is for images that have been embedded into the mail text.
Comment #20
anybodyI can confirm the problem still exists!
To fix this I'd suggest to simply move the preg_replace two lines lower into the $to_link!
I think that's correct and what we need. Let's see what testbot says. Patch attached!
Comment #21
anybodySetting up the priority because the image embedding functionality is broken for all images with an itok!
Comment #23
anybodyNetbeans on windows makes me crazy :P
Comment #25
anybodyWell now I think the logic of the tests themselves should be checked. From my point of view they seem to test the broken functionality. Is there a maintainer who can have a look?
Comment #26
adamps commented@Anybody @matysek145 I think the brief comment in #18 is important to understand. To expand, as I understand it:
Now I guess that you two are interested in a different scenario - when the option "Link images only" is selected. The commit for this issue seems to have made things worse for linked images. Previously, if the image didn't exist, it was generated on the fly. Without the itok this no longer works.
@sgabe the maintainer has already closed this issue twice because he believes the original issue is solved. Seeing as this is a different bug, I have created a new issue: #2552613
@Anybody, the patch #25 seems wrong to me. Surely "Remove security token from URL" needs to be done if not $to_link. I think this patch breaks the tests because it breaks the fix - maybe you could check by testing your fix in the scenario described in this issue? Maybe you have not actually selected "Link images only", but are happy to accept linked images rather than embedded ones? If so I suggest you turn this option on.
Comment #27
anybodyThank you, @AdamPS.
I'm currently very busy and can not have a look in the next weeks. So I'd hope for feedback from other issue contributors here first.
I'll reply ASAP.
Comment #28
adamps commented@Anybody Thanks for the update. I think it would be best if you put any further discussion in the new linked issue.