py3 pop取件 email时间格式化

Python3 pop3取件

困难点

部分邮箱提示密码错误

-ERR Unable to log on

反馈密码错误,但实际上密码是正确的,该种情况,需要使用授权码,而非密码

目前已知需要授权码的邮箱列表

  • 腾讯 @qq.com
  • 网易 @163.com

Date时间解析

查询相关资料,得知可使用email.utils针对邮箱的时间格式进行解析

Date: Sat, 15 Jan 2022 14:08:55 +0800

from email.utils import parsedate
import time
date="Sat, 15 Jan 2022 14:08:55 +0800"
print(time.strftime("%Y-%m-%d %H:%M:%S", parsedate(date)))

163邮箱在长时间运行后报错

报错内容

-ERR \xc4\xfa\xb5\xc4\xd5\xca\xba\xc5\xd4\xdd\xca\xb1\xb2\xbb\xbf\xc9\xd3\xc3.\xbf\xc9\xc4\xdc\xb5\xc4\xd4\xad\xd2\xf2\xca\xc7:\xc4\xfa\xb5\xc4\xd5\xca\xba\xc5\xb6\xe0\xb4\xce\xb3\xa2\xca\xd4\xb4\xed\xce\xf3\xb5\xc4\xc3\xdc\xc2\xeb,\xb3\xf6\xd3\xda\xb0\xb2\xc8\xab\xbf\xbc\xc2\xc7,\xce\xd2\xc3\xc7\xc1\xd9\xca\xb1\xcf\xde\xd6\xc6\xc1\xcb\xc4\xfa\xb5\xc4\xd5\xca\xba\xc5\xb5\xc4pop\xb7\xc3\xce\xca\xc8\xa8\xcf\xde

尝试复现:

  • 长时间运行出现
  • 在停止运行半小时后恢复
    通过以上观测得知,可猜测为频繁请求导致【取件间隔5秒】,延长后可解决

完整代码

import poplib,quopri,requests,json,re,time
from email.header import decode_header
from email.utils import parseaddr
from email.parser import Parser
from email.utils import parsedate
from urllib.parse import urlencode
def get_info(msg):
    value = {}
    i = 0
    for header in ['From', 'To', 'Subject','Date']:  # 解析邮件头
        value[i] = msg.get(header, '')
        if value[i]:
            if header == 'Subject':  # 解析主题
                value[i] = decode_str(value[i])
            elif header=='Date':
                value[i] = time.strftime("%Y-%m-%d %H:%M:%S", parsedate(value[i]))
            else:
                hdr, addr = parseaddr(value[i])
                value[i] = addr
        i = i + 1
    return value
def decode_str(s):
    value, charset = decode_header(s)[0]
    if charset:
        value = value.decode(charset)
    return value
def get_content(msg,pop3_server):
    content = msg.get_payload()
    if pop3_server == 'pop.163.com':
        decoded_string = quopri.decodestring(content[0].get_payload())
        return decoded_string.decode('utf-8')
    decoded_string = quopri.decodestring(content)
    return decoded_string.decode('utf-8')
def parsing(msg,pop3_server):
    #获取邮箱来源 0发件人 1收件人 2主题 3收信时间
    email_info=get_info(msg)
    print('获取到新邮件:{} 来源:{} 收件人:{}'.format(email_info[2],email_info[0],email_info[1]))
    if email_info[0] not in ['[email protected]','[email protected]']:
        return False
    #邮箱内容
    email_content = get_content(msg,pop3_server)
    return email_content

if __name__=='__main__':
    server = poplib.POP3_SSL('pop.163.com')
    server.set_debuglevel(0)
    server.user('[email protected]')
    server.pass_('password 授权码')
    resp, mails, octets = server.list()
    #邮件数量
    email_count=len(mails)
    #取最新一封
    resp, lines, octets = server.retr(email_count)
    msg_content = b'\r\n'.join(lines).decode('gbk')
    lists = []
    for e in lines:
      lists.append(e.decode('gbk'))
    msg_content = '\r\n'.join(lists)
    msg = Parser().parsestr(msg_content)
    result=parsing(msg,temp[2])
    print('获取结果:',result)


你可能感兴趣的:(py3 pop取件 email时间格式化)