如何为python3调整这个gmail阅读代码? (来自python)

时间:2017-08-21 03:18:21

标签: python-3.x email

以下代码在python2中用于读取gmail收件箱。但是,即使在我需要打印的东西周围放置括号之后,它也无法在python3中运行。

import smtplib
import time
import imaplib
import email

FROM_EMAIL  = "email"
FROM_PWD    = "pass"
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT   = 993

mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL,FROM_PWD)
mail.select('inbox')

type, data = mail.search(None, 'ALL')
mail_ids = data[0]

id_list = mail_ids.split()   
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])


for i in range(latest_email_id,first_email_id, -1):
    typ, data = mail.fetch(i, '(RFC822)' )

    for response_part in data:
        if isinstance(response_part, tuple):
            msg = email.message_from_string(response_part[1])
            email_subject = msg['subject']
            email_from = msg['from']
            print('From : ' + email_from + '\n')
            print('Subject : ' + email_subject + '\n')

它给了我错误:

    data = data + b' ' + arg                                                                  
  TypeError: can't concat bytes to int  

1 个答案:

答案 0 :(得分:0)

需要更改两个位置:

  1. typ, data = mail.fetch(str(i), '(RFC822)' )
  2. msg = email.message_from_string(response[1].decode())
  3. 希望这有帮助。