【问题标题】:IndexError using stmplib in Python在 Python 中使用 stmplib 的 IndexError
【发布时间】:2017-10-12 10:50:40
【问题描述】:

我正在尝试从 gmail 帐户转发和删除某些电子邮件,并通过单独的过程运行其他电子邮件。我一直在关注this example,但由于某种原因,我遇到了IndexError,我的trycatch 我包含在另一个问题(如果收件箱为空)上。

# Connect and login to email
imap = imaplib.IMAP4_SSL('imap.gmail.com')
imap.login('sender','password')
imap.list()
imap.select('inbox')

smtp = smtplib.SMTP_SSL('smtp.gmail.com')
smtp.login('sender','password')

try:
    result, data = imap.search(None,'ALL') # search and return sequential ids
    ids_list = data[0].split()
    print 'Total emails: '+str(len(ids_list))
    latest_id = ids_list[-1]

    #Process each email in inbox
    for i in ids_list:
        t, d = imap.fetch(i, '(RFC822)')
        for res_part in d:
            if isinstance(res_part, tuple):
                msg = email.message_from_string(res_part[1])
                subject = msg['subject']
                print 'Subject: '+subject
        if subject != 'Subject of email I care about':
            print 'Dealing with it'
            #Forward email to another account
            #Attempt to forward an email
            text = res_part[0][1] #Something is wrong with this line, throws exception
            smtp.sendmail('sender', 'destination', text)
            imap.store(i, '+FLAGS', '\\Deleted')
            imap.expunge()
        else:
            #Process email
            print 'Don\'t delete this'

except IndexError:
    print 'Error'
    #Inbox is empty (Or in this case apparently not)

我似乎无法弄清楚为什么会收到此错误。任何见解将不胜感激。

【问题讨论】:

  • 您的缩进是否正确,如图所示?我原以为if subject 行会缩进到if isinstance 行下方,但事实并非如此。
  • 呃!哇,我太傻了。

标签: python smtp imap


【解决方案1】:

你需要缩进

if subject != 'Subject of email I care about':
            print 'Dealing with it'
            #Forward email to another account
            #Attempt to forward an email
            text = res_part[0][1] #Something is wrong with this line, throws exception
            smtp.sendmail('sender', 'destination', text)
            imap.store(i, '+FLAGS', '\\Deleted')
            imap.expunge()
else:
            #Process email
            print 'Don\'t delete this'

如果你不这样做,那么res_part 就不会是一个变量。

【讨论】:

    【解决方案2】:

    我认为您的缩进中有错字,因为这不是您遇到的问题。但是,您又犯了一个错误,使用res_part。在您的代码中,您使用res_part 作为元素循环数据d。现在,这意味着res_part 每次迭代将只包含d 的一部分。因此,它将具有较低的维度。你可能想要这样的东西:

    # Connect and login to email
    imap = imaplib.IMAP4_SSL('imap.gmail.com')
    imap.login('sender','password')
    imap.list()
    imap.select('inbox')
    
    smtp = smtplib.SMTP_SSL('smtp.gmail.com')
    smtp.login('sender','password')
    
    try:
        result, data = imap.search(None,'ALL') # search and return sequential ids
        ids_list = data[0].split()
        print 'Total emails: '+str(len(ids_list))
        latest_id = ids_list[-1]
    
        #Process each email in inbox
        for i in ids_list:
            t, d = imap.fetch(i, '(RFC822)')
            text = d[0][1]
            msg = email.message_from_string(text)
            subject = msg['subject']
            print 'Subject: '+subject
            if subject != 'Subject of email I care about':
                print 'Dealing with it'
                #Forward email to another account
                #Attempt to forward an email
                smtp.sendmail('sender', 'destination', text)
                imap.store(i, '+FLAGS', '\\Deleted')
                imap.expunge()
            else:
                #Process email
                print 'Don\'t delete this'
    
    except IndexError:
        print 'Error'
        # Probably won't happen for empty inbox either, as for loop will not execute
        # To catch an empty inbox you could you an `else:` construct.
        #Inbox is empty (Or in this case apparently not)
    

    我没有测试过这段代码。特别是我假设您从数据部分获取消息的方式与您实现它的方式相同。此外,这不会测试d 的第一个元素,即d[0] 是一个元组。我假设,正如您链接到的示例中所假设的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      相关资源
      最近更新 更多