Python3 IMAP读取企业微信邮件()-python
Python3 IMAP读取企业微信邮件()
Python3 IMAP读取企业微信邮件
import imaplib
import email
from email.header import decode_header
import re
imap_server = 'imap.exmail.qq.com'
imap_port = 993
username = 'xxxx'
password = 'xxxx'
#实例化一个对象
imap = imaplib.IMAP4_SSL(host=imap_server, port=imap_port)
try:
#登录并且获取收件箱的全部邮件
imap.login(username, password)
print('登录成功')
imap.select("Inbox")
typ, data = imap.search(None, 'ALL')
fetch_data_list = []
#读取所有信息存进列表
for num in data[0].split():
typ, fetch_data = imap.fetch(num, '(RFC822)')
fetch_data_list.append(fetch_data)
# print(fetch_data_list)
#取出列表进行分析
for fetch_data in fetch_data_list:
msg = email.message_from_bytes(fetch_data[0][1])
subject = decode_header(msg['Subject'])[0][0]
#获取主题
if isinstance(subject, bytes):
try:
subject = subject.decode('utf8')
except:
subject = subject.decode('gbk')
print(subject)
#获取发件人
sender = str(msg['From'])
try:
# print(sender)
p1 = re.compile(r'<.+@.+\..+>', re.S)
print(re.findall(p1, sender)[0])
except:
print(sender)
#获取发件日期
print(msg['Date'])
#walk进每一封邮件查看内容
for part in msg.walk():
# print(part.get_content_type())
if part.get_content_maintype() == 'text':
#获取邮件的文本内容
try:
body = part.get_payload(decode=True)
text = body.decode('utf8')
print(text)
print("=" * 20)
except:
body = part.get_payload(decode=True)
text = body.decode('gbk')
print(text)
print("=" * 20)
except :
print('登录失败')
imap.close()
————————
Python3 IMAP读取企业微信邮件
import imaplib
import email
from email.header import decode_header
import re
imap_server = 'imap.exmail.qq.com'
imap_port = 993
username = 'xxxx'
password = 'xxxx'
#实例化一个对象
imap = imaplib.IMAP4_SSL(host=imap_server, port=imap_port)
try:
#登录并且获取收件箱的全部邮件
imap.login(username, password)
print('登录成功')
imap.select("Inbox")
typ, data = imap.search(None, 'ALL')
fetch_data_list = []
#读取所有信息存进列表
for num in data[0].split():
typ, fetch_data = imap.fetch(num, '(RFC822)')
fetch_data_list.append(fetch_data)
# print(fetch_data_list)
#取出列表进行分析
for fetch_data in fetch_data_list:
msg = email.message_from_bytes(fetch_data[0][1])
subject = decode_header(msg['Subject'])[0][0]
#获取主题
if isinstance(subject, bytes):
try:
subject = subject.decode('utf8')
except:
subject = subject.decode('gbk')
print(subject)
#获取发件人
sender = str(msg['From'])
try:
# print(sender)
p1 = re.compile(r'<.+@.+\..+>', re.S)
print(re.findall(p1, sender)[0])
except:
print(sender)
#获取发件日期
print(msg['Date'])
#walk进每一封邮件查看内容
for part in msg.walk():
# print(part.get_content_type())
if part.get_content_maintype() == 'text':
#获取邮件的文本内容
try:
body = part.get_payload(decode=True)
text = body.decode('utf8')
print(text)
print("=" * 20)
except:
body = part.get_payload(decode=True)
text = body.decode('gbk')
print(text)
print("=" * 20)
except :
print('登录失败')
imap.close()