|
|
@ -37,52 +37,51 @@ class IrMailServer(models.Model): |
|
|
|
email_to, |
|
|
|
subject, |
|
|
|
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 |
|
|
|
""" |
|
|
|
if msg.is_multipart(): |
|
|
|
for part in msg.get_payload(): |
|
|
|
replace_img_src(part, attachments) |
|
|
|
_build_email_replace_img_src(part, attachments) |
|
|
|
else: |
|
|
|
if msg.get_content_subtype() == 'html': |
|
|
|
body = msg.get_payload(decode=True) |
|
|
|
root = fromstring(body) |
|
|
|
for img in root.xpath('//img'): |
|
|
|
for img in root.xpath("//img[@src^='http']"): |
|
|
|
src = img.get('src') |
|
|
|
if src.startswith('http'): |
|
|
|
content = requests.get(src).content |
|
|
|
content_base64 = b64encode(content) |
|
|
|
cid = uuid.uuid4().hex |
|
|
|
filename_rfc2047 = encode_header_param(cid) |
|
|
|
part = MIMEBase('application', 'octet-stream') |
|
|
|
part.set_param('name', filename_rfc2047) |
|
|
|
part.add_header( |
|
|
|
'Content-Disposition', |
|
|
|
'attachment', |
|
|
|
cid=cid, |
|
|
|
filename=filename_rfc2047, |
|
|
|
content=content_base64, |
|
|
|
) |
|
|
|
part.set_payload(content) |
|
|
|
Encoders.encode_base64(part) |
|
|
|
result.attach(part) |
|
|
|
img.set('src', 'cid:%s' % (cid)) |
|
|
|
content = requests.get(src).content |
|
|
|
content_base64 = b64encode(content) |
|
|
|
cid = uuid.uuid4().hex |
|
|
|
filename_rfc2047 = encode_header_param(cid) |
|
|
|
part = MIMEBase('application', 'octet-stream') |
|
|
|
part.set_param('name', filename_rfc2047) |
|
|
|
part.add_header( |
|
|
|
'Content-Disposition', |
|
|
|
'attachment', |
|
|
|
cid=cid, |
|
|
|
filename=filename_rfc2047, |
|
|
|
content=content_base64, |
|
|
|
) |
|
|
|
part.set_payload(content) |
|
|
|
Encoders.encode_base64(part) |
|
|
|
result.attach(part) |
|
|
|
img.set('src', 'cid:%s' % (cid)) |
|
|
|
msg.set_payload(etree.tostring(root)) |
|
|
|
replace_img_src(result, attachments) |
|
|
|
_build_email_replace_img_src(result, attachments) |
|
|
|
return result |