【问题标题】:python3 - imaplib: randomly(?) can' fetch received emailpython3 - imaplib:随机(?)可以'获取收到的电子邮件
【发布时间】:2016-04-26 08:22:44
【问题描述】:
def get_email_body(self, email):
    user = self.email_usr
    password = self.app_email_pwd
    connection = imaplib.IMAP4_SSL('imap.gmail.com')
    connection.login(user, password)
    connection.list()
    connection.select('"INBOX"')
    time.sleep(5)
    result_search, data_search = connection.search(None, 'TO', email, 'SUBJECT', '"some subject"')
    required_email = data_search[0]
    result_fetch, data_fetch = connection.fetch(required_email, '(RFC822)')
    email_body_string = data_fetch[0][1].decode('utf-8')
    confirmation_link = self.parse_confirmation_link(email_body_string)
    return confirmation_link

此功能的工作原理类似于 4 次运行的 2 次。通常它会失败:

self = <imaplib.IMAP4_SSL object at 0x7fa853614b00>, name = 'FETCH'

tag = b'JAAL5'
def _command_complete(self, name, tag):
    # BYE is expected after LOGOUT
    if name != 'LOGOUT':
        self._check_bye()
    try:
        typ, data = self._get_tagged_response(tag)
    except self.abort as val:
        raise self.abort('command: %s => %s' % (name, val))
    except self.error as val:
        raise self.error('command: %s => %s' % (name, val))
    if name != 'LOGOUT':
        self._check_bye()
    if typ == 'BAD':
      raise self.error('%s command error: %s %s' % (name, typ, data))
E           imaplib.error: FETCH command error: BAD [b'Could not parse command']

/usr/lib/python3.4/imaplib.py:964: error

我的建议是,有时在 .search 时没有发送电子邮件,这就是我添加 time.sleep 的原因(我在发送电子邮件后立即搜索)。 否则,当 result_fetch 不是“OK”但也没有帮助时,我确实尝试过搜索。 还有其他建议吗?

【问题讨论】:

    标签: python gmail imaplib


    【解决方案1】:

    oooops,我的建议是正确的,但是 time.sleep 的位置不正确。在连接前移动睡眠,一切顺利

    【讨论】:

      猜你喜欢
      • 2012-12-11
      • 2015-04-29
      • 2012-10-24
      • 2015-04-17
      • 2014-02-02
      • 2014-10-03
      • 2017-04-04
      • 2021-11-01
      • 2021-07-29
      相关资源
      最近更新 更多