Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Mime: Email::embed doesn't work for background images #42921

Closed
@flack

Description

@flack

Symfony version(s) affected: 5.3.4

Description

I have an email template which sets a background image on a td element like this:

<td background="bgimage.png">

This used to work fine in Swiftmailer with more or less the following code:

$cid = $message->embed(Swift_Image::fromPath($path));
$html_body = str_replace($path, $cid, $html_body);

How to reproduce

In Symfony Mailer, I have the code from the documentation:

$name = basename($path);
$message->embed(fopen($path, 'r'), $name);
$html_body = str_replace($path, 'cid:' . $name, $html_body);

Which works fine for img tags and src attributes, but it completely ignores the background attribute, because the regex is only looking at img tags:

preg_match_all('(<img\s+[^>]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+)))i', $html, $names);

The HTML in the email looks like this in the end:

<td background="cid:bgimage.png">

The image gets attached, but is missing a Content-ID:

Content-Type: application/octet-stream; name=bgimage.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; name=bgimage.png; filename=bgimage.png

Possible Solution

I'm not a 100% sure, but one thing that could help is if Symfony could make sure that each (inline) attachment always gets a Content-ID, then I could work around the regex problem on my end.

An alternative would be to make the regex a bit more loose, basically to have it find anything that looks like "cid:xxxx". Not sure if that would be a 100% safe, but there's still a matching of found CIDs to attachment names, so it should work, no?

Additional context

I know that the background attribute has probably been deprecated for well over a decade now, and in the normal web context I wouldn't bother trying to support it, but this is email we're talking about, and to this day Outlook (on Windows) uses an HTML rendering engine from 2007, so you have to get pretty old school to get decent looking mails. I guess if you look long and hard enough, there will be some other way to get background images on Outlook, but I also really don't want to re-do (and re-test) all the email templates I have, just because Swiftmailer is EOL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions