Answer the question
In order to leave comments, you need to log in
How to skip email attachments in python?
Good day!
I am writing a small email client in python.
There are letters with attachments up to 20MB.
The question is - is it possible to get only the text of the letter without downloading the extra 20MB of base64 code?
I would be grateful for an example.
Now I get the text like this (a piece of code):
if msg.is_multipart():
for part in msg.walk():
ctype = part.get_content_type()
cdispo = str(part.get('Content-Disposition'))
if ctype == 'text/html' and 'attachment' not in cdispo:
body = part.get_payload(decode=True).decode('UTF-8') # decode
# если не multipart - т.е plain text, без вложений
else:
body = msg.get_payload(decode=True).decode('UTF-8')
print(body)
Answer the question
In order to leave comments, you need to log in
Why did you attach this piece of code - it's not clear. You need to look above where you download letters.
1) The protocol POP3
does not support downloading email without attachment, but it does IMAP
. That is, perhaps you are writing to the wrong protocol.
2) You can just load HEADER and read the type of email. If it content type
is mixed
, then there is an attachment and the letter can simply not be downloaded.
3) Attachments usually take the third place and further in the letter, so we just load ["HEADER", "1", "1.MIME"]
4) Read about the general standardization of mail: tools.ietf.org/html/rfc3501.
And finally, I recently wrote an IMAP client for downloading emails from a mailbox. All letters always had attachments. Just use multithreading, asynchrony and the problem will be solved.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question