I want to grab all emails from an inbox using the Python IMAPLIB module… how can I do this?

别说谁变了你拦得住时间么 提交于 2019-12-25 18:23:13

问题


This is where I am at, but I'm not sure where to go from here:

import imaplib

import email

conn = imaplib.IMAP4()

conn.login("username", "password")

status, messages = conn.select('INBOX')    

if status != "OK":

        print ("Incorrect mail box")

        exit()

print (messages)

回答1:


I have test it under gmx and it works, please take a look at https://docs.python.org/3/library/email.html and https://docs.python.org/3/library/imaplib.html

import imaplib
import email
mail = imaplib.IMAP4_SSL('imap')
mail.login('username', 'password')

status, messages = mail.select('INBOX')    

if status != "OK": exit("Incorrect mail box")

for i in range(1, int(messages[0])):
    res, msg = mail.fetch(str(i), '(RFC822)')
    for response in msg:
        if isinstance(response, tuple):
            msg = email.message_from_bytes(response[1])
            print (msg["subject"])


来源:https://stackoverflow.com/questions/50892218/i-want-to-grab-all-emails-from-an-inbox-using-the-python-imaplib-module-how-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!