美文网首页
py3 pop取件 email时间格式化

py3 pop取件 email时间格式化

作者: 归隐小赵 | 来源:发表于2022-01-16 15:47 被阅读0次

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 ['AccountSupport@ubi.com','noreply@steampowered.com']:
        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('xxx@163.com')
    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时间格式化

    Python3 pop3取件 困难点 部分邮箱提示密码错误 反馈密码错误,但实际上密码是正确的,该种情况,需要使用...

  • Fiori-like Prototype (Send Email

    Email (Electronic mail ) POP3, SMTP, IMAP POP3 POP3是Post ...

  • 时间格式处理

    格式化时间类型 取当天时间,以YYYY年MM月DD日形式显示 2.任意时间戳格式化,以YYYY-MM-DD HH:...

  • Java.SOP.JSP

    更新:-- 2018/10/24 添加selected demo 取序号: 格式化时间: 重置 Radio che...

  • 泡泡玛特相关

    采购 POP MART全国渠道暂不开放任何加盟合作,但可接受大宗团购 联系人:JOJO Email: liyanj...

  • Python_format 用法

    作者:Gakki % 用法 取模运算符:% 代表取模,返回除法的余数。 输出结果: 字符串格式化:表示字符串格式化...

  • 英汉音译

    author:明天依旧可好email:mtyjkh@outlook.comtime:2018-11-24 在英文取...

  • SpringBoot时间格式化的5种方法,你知道几种?

    在我们日常工作中,时间格式化是一件经常遇到的事儿,所以本文我们就来盘点一下 Spring Boot 中时间格式化的...

  • JS时间戳与格式化时间互转

    JS时间戳与格式化时间互转 Javascript 获取当前时间戳(毫秒级别) 时间戳转成格式化时间 格式化时间转成...

  • 日期时间格式化

    日期时间格式化格式化

网友评论

      本文标题:py3 pop取件 email时间格式化

      本文链接:https://www.haomeiwen.com/subject/mgmfhrtx.html