- 準(zhǔn)備
pycharm中先把自身所需要的庫更新完全
pip install pipreqs
pipreqs . --encoding=utf-8
pip install -r requirements.txt
- 讀郵件
SMTP_SERVER = 'coremail.xxxx.com.cn' # 更改為你的SMTP服務(wù)器
def read_mail(folder = 'inbox', USERNAME = USERNAME, PASSWORD = PASSWORD):
"""
這個(gè)函數(shù)用于讀取指定收件箱的最近一封郵件
"""
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
mail.login(USERNAME, PASSWORD)
if folder != 'inbox':
folder_utf7 = imapclient.imap_utf7.encode('量化部')
folder = f"INBOX/{folder_utf7.decode('utf-8')}"
mail.select(folder)
result, data = mail.uid('search', None, "ALL")
latest_email_uid = data[0].split()[-1]
result, email_data = mail.uid('fetch', latest_email_uid, '(BODY.PEEK[HEADER.FIELDS (SUBJECT FROM)])')
raw_email = email_data[0][1].decode("utf-8")
email_message = email.message_from_string(raw_email)
subject = decode_header(email_message['Subject'])[0][0]
if isinstance(subject, bytes):
# 如果是bytes類型画畅,需要進(jìn)行解碼
subject = subject.decode()
from_ = decode_header(email_message['From'])[0][0]
if isinstance(from_, bytes):
from_ = from_.decode()
print('From :', from_)
print('Subject :', subject)
3.寫郵件