邮件检查并读取python3

时间:2018-12-26 16:16:54

标签: python python-3.x gmail-imap

我已经建立了用于从gmail读取邮件的代码。 我正在尝试制作从邮件中提取数据的程序。但是,当我输入正确的电子邮件和密码时,没有任何反应发生,代码可能会冻结, 另外,我禁用了所有安全选项,并且没有收到任何错误。

import smtplib
import time
import imaplib
import email

ORG_EMAIL   = "@gmail.com"
FROM_EMAIL  = "mymail" + ORG_EMAIL
FROM_PWD    = "pass"
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT   = 993

def read_email_from_gmail():

    try:
        mail = imaplib.IMAP4_SSL(SMTP_SERVER)
        mail.login(FROM_EMAIL, FROM_PWD)
        mail.select('inbox')
        print(FROM_EMAIL)

        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]

        id_list = mail_ids.split()
        for i in reversed(id_list):
            typ, data = mail.fetch(i, '(RFC822)')

        for response_part in data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1].decode('utf-8'))
                email_subject = msg['subject']
                email_from = msg['from']
                print('From : ' + email_from + '\n')
                print('Subject : ' + email_subject + '\n')

    except Exception as e:
        print(str(e))

read_email_from_gmail()

0 个答案:

没有答案