|
@ -37,35 +37,34 @@ class IrMailServer(models.Model): |
|
|
email_to, |
|
|
email_to, |
|
|
subject, |
|
|
subject, |
|
|
body, |
|
|
body, |
|
|
email_cc, |
|
|
|
|
|
email_bcc, |
|
|
|
|
|
reply_to, |
|
|
|
|
|
attachments, |
|
|
|
|
|
message_id, |
|
|
|
|
|
references, |
|
|
|
|
|
object_id, |
|
|
|
|
|
subtype, |
|
|
|
|
|
headers, |
|
|
|
|
|
body_alternative, |
|
|
|
|
|
subtype_alternative, |
|
|
|
|
|
|
|
|
email_cc=email_cc, |
|
|
|
|
|
email_bcc=email_bcc, |
|
|
|
|
|
reply_to=reply_to, |
|
|
|
|
|
attachments=attachments, |
|
|
|
|
|
message_id=message_id, |
|
|
|
|
|
references=references, |
|
|
|
|
|
object_id=object_id, |
|
|
|
|
|
subtype=subtype, |
|
|
|
|
|
headers=headers, |
|
|
|
|
|
body_alternative=body_alternative, |
|
|
|
|
|
subtype_alternative=subtype_alternative, |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
def replace_img_src(msg, attachments): |
|
|
|
|
|
"""replace_img_src Given a message, find it's img tags and if they |
|
|
|
|
|
are URLs, replace them with cids |
|
|
|
|
|
|
|
|
def _build_email_replace_img_src(msg, attachments): |
|
|
|
|
|
""" Given a message, find it's img tags and if they |
|
|
|
|
|
are URLs, replace them with cids. |
|
|
|
|
|
|
|
|
:param msg: A email.message.Message |
|
|
:param msg: A email.message.Message |
|
|
""" |
|
|
""" |
|
|
if msg.is_multipart(): |
|
|
if msg.is_multipart(): |
|
|
for part in msg.get_payload(): |
|
|
for part in msg.get_payload(): |
|
|
replace_img_src(part, attachments) |
|
|
|
|
|
|
|
|
_build_email_replace_img_src(part, attachments) |
|
|
else: |
|
|
else: |
|
|
if msg.get_content_subtype() == 'html': |
|
|
if msg.get_content_subtype() == 'html': |
|
|
body = msg.get_payload(decode=True) |
|
|
body = msg.get_payload(decode=True) |
|
|
root = fromstring(body) |
|
|
root = fromstring(body) |
|
|
for img in root.xpath('//img'): |
|
|
|
|
|
|
|
|
for img in root.xpath("//img[@src^='http']"): |
|
|
src = img.get('src') |
|
|
src = img.get('src') |
|
|
if src.startswith('http'): |
|
|
|
|
|
content = requests.get(src).content |
|
|
content = requests.get(src).content |
|
|
content_base64 = b64encode(content) |
|
|
content_base64 = b64encode(content) |
|
|
cid = uuid.uuid4().hex |
|
|
cid = uuid.uuid4().hex |
|
@ -84,5 +83,5 @@ class IrMailServer(models.Model): |
|
|
result.attach(part) |
|
|
result.attach(part) |
|
|
img.set('src', 'cid:%s' % (cid)) |
|
|
img.set('src', 'cid:%s' % (cid)) |
|
|
msg.set_payload(etree.tostring(root)) |
|
|
msg.set_payload(etree.tostring(root)) |
|
|
replace_img_src(result, attachments) |
|
|
|
|
|
|
|
|
_build_email_replace_img_src(result, attachments) |
|
|
return result |
|
|
return result |