[Python] 纯文本查看 复制代码 import email
import email.header
import requests
import imaplib
from flask import Flask, request, jsonify, render_template_string
app = Flask(__name__)
# 1. 使用refresh_token获取access_token
def get_accesstoken(refresh_token):
data = {
'client_id': '9e5f94bc-e8a4-4e73-b8be-63364c29d753',
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}
ret = requests.post('https://login.microsoftonline.com/consumers/oauth2/v2.0/token', data=data)
print(ret.text)
return ret.json()['access_token']
def generate_auth_string(user, token):
auth_string = f"user={user}\1auth=Bearer {token}\1\1"
return auth_string
def tuple_to_str(tuple_):
"""
元组转为字符串输出
:param tuple_: 转换前的元组,QQ邮箱格式为(b'\xcd\xf5\xd4\xc6', 'gbk')或者(b' <XXXX@163.com>', None),163邮箱格式为('<XXXX@163.com>', None)
:return: 转换后的字符串
"""
if tuple_[1]:
out_str = tuple_[0].decode(tuple_[1])
else:
if isinstance(tuple_[0], bytes):
out_str = tuple_[0].decode('gbk')
else:
out_str = tuple_[0]
return out_str
def fetch_email_body(mail, item):
try:
ret, data = mail.fetch(item, '(RFC822)')
msg = email.message_from_string(data[0][1].decode('utf-8'))
sub = msg.get('subject')
sub_text = email.header.decode_header(str(sub))
# 主题
sub_detail = ''
if sub_text[0]:
sub_detail = tuple_to_str(sub_text[0])
print(sub_detail)
for part in msg.walk():
if not part.is_multipart():
content_type = part.get_content_type()
name = part.get_filename()
if not name:
txt = str(part.get_payload(decode=True))
if content_type == 'text/html':
# mail.store(item, '-FLAGS', '\\SEEN') 设为已读
print(txt)
except Exception as e:
print(e)
def getmail(sel, mail):
mail.select(sel)
status, messages = mail.search(None, 'ALL') #UNSEEN 为未读邮件
all_emails = messages[0].split()
print(all_emails)
for item in all_emails:
fetch_email_body(mail, item)
# 2. 使用访问令牌连接 IMAP
def connect_imap(emailadr, access_token):
mail = imaplib.IMAP4_SSL('outlook.live.com')
print(generate_auth_string(emailadr, access_token))
mail.authenticate('XOAUTH2', lambda x: generate_auth_string(emailadr, access_token))
getmail('INBOX', mail) #读取收件箱
getmail('Junk', mail) #读取垃圾箱
# 关闭连接
mail.logout()
@app.route('/get', methods=['GET'])
def get_email():
user = request.args.get('user')
refresh_token = request.args.get('refresh_token')
access_token = get_accesstoken(refresh_token)
connect_imap(user, access_token)
# 主流程
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8888)
补充内容 (2024-10-16 00:59):
其他自己改吧 =。= 刷视频去了 |