From 01dea31fb4d686a652db497d46e0054df917b80b Mon Sep 17 00:00:00 2001 From: tonio Date: Tue, 22 Jun 2010 11:31:34 +0200 Subject: [PATCH] Better handling of inline images... --- INSTALL | 9 +++++++++ lib/email_listing.py | 46 +++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/INSTALL b/INSTALL index 8b3368ae5..b3897f9f7 100644 --- a/INSTALL +++ b/INSTALL @@ -509,6 +509,15 @@ Just run the following command to activate the component:: # DJANGO_SETTINGS_MODULE=mailng.settings \ /scripts/extension.py webmail on +Sometimes, inline images are integrated inside emails. To display +them, MailNG stores them inside a temporary folder. Make sure the +following path exists, or create it if not:: + + # mkdir /static/tmp + +The user that is running MailNG will need the write permission inside +this folder. + Next, go to the admin panel and modify the following parameters in order to communicate with your IMAP and SMTP servers:: diff --git a/lib/email_listing.py b/lib/email_listing.py index 1c7c875f5..0e69d730c 100644 --- a/lib/email_listing.py +++ b/lib/email_listing.py @@ -161,20 +161,32 @@ class Email(object): continue if part.get_content_maintype() == "image": + m = re.match("<(.+)>", part["Content-ID"]) + cid = m is not None and m.group(1) or part["Content-ID"] + if part.has_key("Content-Location"): fname = part["Content-Location"] if re.match("^http:", fname): path = fname else: - path = os.path.abspath("static/tmp/" + fname) - fp = open(path, "wb") - fp.write(part.get_payload(decode=True)) - fp.close() - m = re.match("<(.+)>", part["Content-ID"]) - if m: - self.attached_map[m.group(1)] = path - else: - self.attached_map[part["Content-ID"]] = path + path = self.__save_image(fname, part) + elif part.has_key("Content-Disposition"): + if not part["Content-Disposition"].startswith("inline"): + continue + params = part["Content-Disposition"].split(";") + fname = None + for p in params: + lst = p.strip().split("=") + if len(p) and p[0] == "filename": + fname = p[1] + break + path = self.__save_image(fname is None and cid or fname, part) + else: + continue + + self.attached_map[cid] = path + continue + if not contents.has_key(mode) or contents[mode] == "": if mode == "html": @@ -185,14 +197,22 @@ class Email(object): self.pre, self.body = \ getattr(self, "viewmail_%s" % mode)(contents[mode], links=links) + def __save_image(self, fname, part): + if re.search("\.\.", fname): + return None + path = "static/tmp/" + fname + fp = open(path, "wb") + fp.write(part.get_payload(decode=True)) + fp.close() + return "/" + path + def map_cid(self, url): import re - map = globals()["attached_map"] - m = re.match(".*cid:([^\"]+)", url) + m = re.match(".*cid:(.+)", url) if m: - if map.has_key(m.group(1)): - return map[m.group(1)] + if self.attached_map.has_key(m.group(1)): + return self.attached_map[m.group(1)] return url def render_headers(self, **kwargs):