好在很快就在bytes parser找到了一个可以使用的类email.parser.BytesParser,该类自动解码,测试可以对MIME邮件进行解析。 问题三: 出现unknown-8bit编码 在采用BytesParser之后,依旧使用msg.get('Subject')获取邮件主题,使用email.header.decode_header()对base64进行解码,却在解码后出现
raw_email = msg_data[0][1].decode('utf-8') email_message = email.message_from_bytes(raw_email) # 解析邮件主体内容,考虑到多语种和编码问题 subject, encoding = decode_header(email_message['Subject'])[0] if encoding: subject = subject.decode(encoding) content = '' for part in email_me...
Header.Header(subject) dh = email.Header.decode_header(h) subject = unicode(dh[0][0], dh[0][1]).encode('utf8') mailName = "mail%d.%s" % (i, subject) f = open('%d.log'%(i), 'w'); print >> f, "Date: ", message["Date"] print >> f, "From: ", email.utils....
h=email.Header.Header(name) dh=email.Header.decode_header(h) fname=dh[0][0] print'附件名:', fname data=par.get_payload(decode=True)#解码出附件数据,然后存储到文件中 try: f=open(fname,'wb')#注意一定要用wb来打开文件,因为附件一般都是二进制文件 except: print'附件名有非法字符,自动换一...
dh=email.Header.decode_header(h) fname=dh[0][0] print'附件名:', fname data=par.get_payload(decode=True)#解码出附件数据,然后存储到文件中 try: f=open(fname,'wb')#注意一定要用wb来打开文件,因为附件一般都是二进制文件 except: print'附件名有非法字符,自动换一个' ...
fromemail.headerimportdecode_header decoded_filenames=[]forattachmentinattachments:ifattachment:# 使用decode_header解码decoded_header=decode_header(attachment)# 处理解码结果fordecoded_byte,charsetindecoded_header:ifisinstance(decoded_byte,bytes):# 将字节转换为字符串decoded_filename=decoded_byte.decode(chars...
importreimportosimportemailfromemail.headerimportdecode_headerfromemail.utilsimportparsedate_to_datetimedefparse_eml(eml_fp, attr_dir):""" eml文件解析 :params eml_fp: eml文件路径 :params attr_dir: 附件保存目录 """ifnotos.path.exists(attr_dir): ...
fetch(unread_msgs[0], '(RFC822)') raw_email = msg_data[0][1].decode('utf-8') # 使用email库解析邮件内容,查找附件等 import email msg = email.message_from_string(raw_email) for part in msg.walk(): if part.get_content_maintype() == 'multipart': continue if part.get('Content-...
import imaplib import email import time from email.header import decode_header # 邮箱服务器地址,参数有:邮箱服务器地址,邮箱账号,邮箱密码 def connect_to_email_server(imap_host, imap_user, imap_pass): """ 连接到 IMAP 邮件服务器并登录。 """ mail = imaplib.IMAP4_SSL(imap_host) mail.login...
import poplib from email import parser from email.header import decode_header def get_email_encoding(email, password, pop3_server): # 连接到邮件服务器 server = poplib.POP3(pop3_server) server.user(email) server.pass_(password) # 获取邮件数量和邮件列表 stat, mails, octets = server.list() ma...