A
A
Artem2016-07-13 02:06:27
Python
Artem, 2016-07-13 02:06:27

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

1 answer(s)
J
JRazor, 2016-07-19
@JRazor

Why did you attach this piece of code - it's not clear. You need to look above where you download letters.
1) The protocol POP3does 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 typeis 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 question

Ask a Question

731 491 924 answers to any question