当前位置:  开发笔记 > 编程语言 > 正文

Python iMAP电子邮件访问的格式正确的示例?

如何解决《PythoniMAP电子邮件访问的格式正确的示例?》经验,为你挑选了2个好方法。

tldr:有人可以告诉我如何正确格式化这个Python iMAP示例,以便它可以工作吗?

来自 https://docs.python.org/2.4/lib/imap4-example.html

import getpass, imaplib

M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
M.close()
M.logout()

假设我的电子邮件是"email@gmail.com",密码是"密码",这应该怎么样?我试过了M.login(getpass.getuser(email@gmail.com), getpass.getpass(password)) ,它超时了.在这里完成newb,所以我很可能错过了一些明显的东西(比如先创建一个iMAP对象?不确定).



1> tzot..:
import imaplib

# you want to connect to a server; specify which server
server= imaplib.IMAP4_SSL('imap.googlemail.com')
# after connecting, tell the server who you are
server.login('email@gmail.com', 'password')
# this will show you a list of available folders
# possibly your Inbox is called INBOX, but check the list of mailboxes
code, mailboxen= server.list()
print mailboxen
# if it's called INBOX, then…
server.select("INBOX")

其余的代码似乎是正确的.


只是为了节省他人可能会看到这个的时间......它的"IMAP4_SSL"不仅仅是"IMAP_SSL".

2> Brian C. Lan..:

这是我用来从我的邮箱中获取logwatch信息的脚本.在LFNW 2008上发表 -

#!/usr/bin/env python

''' Utility to scan my mailbox for new mesages from Logwatch on systems and then
    grab useful info from the message and output a summary page.

    by Brian C. Lane 
'''
import os, sys, imaplib, rfc822, re, StringIO

server  ='mail.brianlane.com'
username='yourusername'
password='yourpassword'

M = imaplib.IMAP4_SSL(server)
M.login(username, password)
M.select()
typ, data = M.search(None, '(UNSEEN SUBJECT "Logwatch")')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
#   print 'Message %s\n%s\n' % (num, data[0][1])

    match = re.search(  "^(Users logging in.*?)^\w",
                        data[0][1],
                        re.MULTILINE|re.DOTALL )
    if match:
        file = StringIO.StringIO(data[0][1])
        message = rfc822.Message(file)
        print message['from']
        print match.group(1).strip()
        print '----'

M.close()
M.logout()


Caveat程序员:自Python 2.3以来,不推荐使用Python模块"rfc822".(来源:http://docs.python.org/2.6/library/rfc822.html)请考虑使用电子邮件模块.
推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有