django - How do I reply to an email using the Python imaplib and include the original message? -
I am currently using a server to fetch email messages and process content and attachments.
I would like to respond to the message with status / error message and the link to the content generated on my site if they can be processed. It should include the original message, but leave any attachments (which will be large) and preferably replace them with their filenames / sizes.
Since I'm already running MIME message parts, I am assuming what I need to do is create a new MIME message tree and have a copy of the original message and delete / replace the alignment node. for.
Before I started that path, I expected someone to give me some ideas. Is there any library work to do this? Any kind of standard behavior should stick to me?
I am currently using / imaplib
, smtplib
and email
module, but there is something in it Can be clear whether you are there. It is also running in Django, so can use anything in django.core.email
if it makes it easy.
The original MIME tree structure of the incoming message is as follows ( email.iterators._structure (msg) ) By using
:
Multipart / mixed text / html (message) episode / octet-stream (attachment 1) application / octet-stream (attachment 2)
Answer in the following structure via GMail results:
Multilanguage / Optional Text / Plain Text / html
Ie They were as thick as I thought There are, smart is not just excluding attachments (good) and text and HTML version clearly restructure "quoted" material.
I think all I should do is also do this, just reply with a simple message, which is not much about keeping the original message after leaving the attachment.
However, I can also answer my original question because I know how it is so far.
First of all, replace all attachments in the original message with text / plain placeholder:
Import email originally for the part in the original = email.message_from_string (.. ) .walk (): If (part.get ('content-disposition') and part.get ('content-break') starts with ("attachment")): part.set_type ("text / plain ") Part.set_payload (" Attachment deleted:% s (% s,% d bytes) "% (part.get_filename (), part.get_content_type (), lane (equivalent T.get_payload (decode = true)) Dell Part ["Content-Displacement"] Del-Part ["Content-Transfer-Encoding"]
Create a reply message: then attach and send the original MIME message object:
new.attach (MIMEMessage (original) ) S = smtplib.SMTP () s.sendmail ("Me@mysite.com", [new ["to"]], new.as_string ()) s.quit ()
< P> The resultant structure is: Multipart / Multilingual / mixed text / plain text / html message / RFC 822 Multilingual / mixed text / html text / plain text / plain
or it's a little easier using Django:
the result ends (at least In less GMail) showing the original message "---- Forwarded message ----" which is not quite what I was after, but the general thought was working And I hope this answer tries to understand how someone responds with MIME messages.
Comments
Post a Comment