以下是应该读取我的Gmail帐户的代码。
import smtplib
import time
import imaplib
import email
from reportlab.pdfgen import canvas
ORG_EMAIL = '@gmail.com'
FROM_EMAIL = 'amitesh.sahay@gmail.com'
FROM_PWD = '<my_password>'
SMTP_SERVER = 'imap.gmail.com'
SMTP_PORT = 993
# -------------------------------------------------
#
# Utility to read email from Gmail Using Python
#
# ------------------------------------------------
def read_email_from_gmail():
try:
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL,FROM_PWD)
mail.select('inbox')
type1, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
for i in range(latest_email_id,first_email_id, -1):
type1, data = mail.fetch(i, '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
email_subject = msg['subject']
email_from = msg['from']
print('From : ' + email_from + '\n')
print('Subject : ' + email_subject + '\n')
except ValueError:
print("Oops! That was no valid mailbox. Try again...")
当我执行代码时,它会运行而不会抛出任何错误,但它也没有提供任何输出。我正在使用python 3.6和安装在PyCharm中内置的所有python包。是否有人可以指出哪些可能是一个可能的问题?
如果需要进一步的详细信息,请与我们联系。