Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inlining images #22

Open
MaxGfeller opened this issue Jun 10, 2022 · 5 comments
Open

Inlining images #22

MaxGfeller opened this issue Jun 10, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@MaxGfeller
Copy link

First of all, thanks a lot for this library and the great amount of work that went into it🙏🏻

As far as i can tell images that are appended as inline attachments are not automatically inlined in the resulting body HTML string.

For example, this element is in an email by Confluence:

<img style=3D"vertical-align: top; display: block;" src=3D"cid:page-icon" alt=3D"page icon" title=3D="page icon" height=3D"16" width=3D"16" border=3D"0">

This references an attachment, page-icon, by Content-ID which is appended later in the email body:

------=_Part_565_1697167297.1654852801463
Content-Type: image/png; name=page-icon.png
Content-Transfer-Encoding: base64
Content-ID: <page-icon>
Content-Disposition: inline; filename=page-icon.png

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAPFBMVEX///+1tbWwsLCtra3////5
+fmLi4vZ2dnT09P8/PzPz8+rq6uhoaHR0dFycnJwcHB6enp4eHiDg4OAgIBog/vRAAAADnRSTlMA
IiJV3e7u7u7u7u7u7rDOyYEAAABUSURBVHhepcpLDoAwCABRqkBbP9Dq/e9qLYS1ibN8GQBYWFVG
fQWLWyFEJG0uknGmuz+CDnjYEzDqDpF8BrV+HBRHNThjyBP42qpBufmFxOIpJ3gAPTUGaYiilrsA
AAAASUVORK5CYII=
------=_Part_565_1697167297.1654852801463

Now when i get the HTML body of the email like this:

message.get_html_body(0).unwrap()

...the image source still says cid:page-icon. Is there a way to automatically inline those attachments? Or is this something that should be handled by the user?

Other libraries, such as the Nodemailer mailparser do this automatically.

@mdecimus
Copy link
Member

Hi,
Replacing the inline content with a link to the image should be done when displaying the message to the user and cannot be done beforehand automatically because mail-parser does not know how and where the image will be stored, perhaps it will be accessible from a URL or perhaps it will be served from memory/local disk (when writing a desktop client for instance).
So, to implement this feature you would have to tell the library what URL you want to replace each inline content with. For the time being you could simply run a replace_with("\"cid:page-icon\"", "\"http://localhost/my-url/page-icon.png\"") but perhaps I could add a function to parse the HTML properly and do all the replacements.
Out of curiosity, how is Nodemailer mailparser handling this? Which URL are replacing the cid references with?

@MaxGfeller
Copy link
Author

As far as i understand Nodemailer replaces the cid:xxx references with the inline attachments, in this case it would create a base64 URL:

<img style="vertical-align: top; display: block;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAPFBMVEX///+1tbWwsLCtra3////5+fmLi4vZ2dnT09P8/PzPz8+rq6uhoaHR0dFycnJwcHB6enp4eHiDg4OAgIBog/vRAAAADnRSTlMAIiJV3e7u7u7u7u7u7rDOyYEAAABUSURBVHhepcpLDoAwCABRqkBbP9Dq/e9qLYS1ibN8GQBYWFVGfQWLWyFEJG0uknGmuz+CDnjYEzDqDpF8BrV+HBRHNThjyBP42qpBufmFxOIpJ3gAPTUGaYiilrsAAAAASUVORK5CYII=" alt="page icon" title="page icon" height="16" width="16" border="0">

At least in the cases where there's a message part with that referenced Content-ID.

@MaxGfeller
Copy link
Author

The exact position where that happens would be here. There's also an option to skip these links.

@mdecimus mdecimus added the enhancement New feature or request label Jun 11, 2022
@mdecimus
Copy link
Member

I'll leave this open as an enhancement. In the meantime you could perform a regex replace like the JS library is doing and replace it with a link to the local filesystem or a URL.

@MaxGfeller
Copy link
Author

Perfect, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants