4

I am working on my own email client (powered by Django 1.10 and Python 3). Currently, I am trying to render inbox messages using python's IMAPClient library. It looks like I succeeded in parsing emails with mixed and alternative subtypes, but now I am stuck trying to render parts of the body with subtype relative. That is, parts containing HTML with embedded inline attachments.

Currently, I am going to download one-by-one all the inline images to my server using the respective fetch command, and after than insert links on those images in the HTML of the target letter.

To illustrate, let's say email HTML representation contains an inline image:

...<td><img src="cid:part1.06030702.04060203@studinter.ru"></td>...

...and thebodystruture part containing the inline image description looks like this:

(b'IMAGE', b'JPEG', (b'NAME', b'ban1.jpg'), b'<part1.06030702.04060203@studinter.ru>', None, b'BASE64', 15400, None, (b'INLINE', (b'FILENAME', b'ban1.jpg')), None)

So, in theory, I could download the image on my server, and replace the src tag's value(namely, cid:part1.06030702.04060203@studinter.ru) by the url of the image on my server.

My concern here is that this very process of inserting inline attachments into the target HTML message body is something that libraries like IMAPClient or python's email package have already implemented, and whether I am going to reinvent bicycle. I am completely lost in this topic.

The question is, do I really have to implement it on my own? If yes, is the described method appropriate? And if no, I would really appreciate a hint on how to do this with IMAPClient, or standard library's imaplib.

Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121
  • 1
    I think this functionality is not built into the libraries you mentioned because the reverse (assembling a message with embedded media) was not there last time I checked (but there is a while since then). That said, I would try to replace the `src` attribute with a [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) using something like BeautifulSoup. – Paulo Scardine Aug 22 '17 at 16:40
  • 1
    BTW, check this [related answer](https://stackoverflow.com/questions/1579133/replace-src-of-all-img-elements-using-parser); see also [Strategies for serving Base64 encoded images in HTML](https://www.codementor.io/tips/8112473532/strategies-for-serving-base64-encoded-images-in-html) – Paulo Scardine Aug 22 '17 at 17:26
  • Super. Thanks for the link !!! – Edgar Navasardyan Aug 22 '17 at 18:02

1 Answers1

0

My external lib https://github.com/ikvk/imap_tools

from imap_tools import MailBox, A

with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch(A(all=True)):
        msg.html  # str: '<b>Hello 你 Привет</b>'
        for att in msg.attachments:
            att.filename             # str: 'cat.jpg'
            att.payload              # bytes: b'\xff\xd8\xff\xe0\'
            att.content_id           # str: 'part45.06020801.00060008@mail.ru'
            att.content_type         # str: 'image/jpeg'
            att.content_disposition  # str: 'inline'

There is enough data for rendering here.

You can analyze att.content_id and find it in html.

Vladimir
  • 6,162
  • 2
  • 32
  • 36