python imaplib to get gmail inbox subjects titles and sender name

后端 未结 5 1327
攒了一身酷
攒了一身酷 2020-12-04 13:40

I\'m using pythons imaplib to connect to my gmail account. I want to retrieve the top 15 messages (unread or read, it doesn\'t matter) and display just the subjects and sen

5条回答
  •  庸人自扰
    2020-12-04 14:21

        c.select('INBOX', readonly=True)
    
        for i in range(1, 30):
            typ, msg_data = c.fetch(str(i), '(RFC822)')
            for response_part in msg_data:
                if isinstance(response_part, tuple):
                    msg = email.message_from_string(response_part[1])
                    for header in [ 'subject', 'to', 'from' ]:
                        print '%-8s: %s' % (header.upper(), msg[header])
    

    This should give you an idea on how to retrieve the subject and from?

提交回复
热议问题