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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/315362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:53:38  来源:igfitidea点击:

Properly formatted example for Python iMAP email access?

pythonemailimap

提问by Brian C. Lane

tldr: Can someone show me how to properly format this Python iMAP example so it works?

tldr:有人可以告诉我如何正确格式化这个 Python iMAP 示例以使其有效吗?

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

来自 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()
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()

Assuming my email is "[email protected]" and the password is "password," how should this look? I tried M.login(getpass.getuser([email protected]), getpass.getpass(password))and it timed out. Complete newb here, so it's very likely I missed something obvious (like creating an iMAP object first? Not sure).

假设我的电子邮件是“[email protected]”,密码是“password”,这看起来如何?我试过了M.login(getpass.getuser([email protected]), getpass.getpass(password)),超时了。在这里完成新手,所以很可能我错过了一些明显的东西(比如先创建一个 iMAP 对象?不确定)。

回答by 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 protected]', '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")

The rest of your code seems correct.

您的其余代码似乎是正确的。

回答by Brian C. Lane

Here is a script I used to use to grab logwatch info from my mailbox. Presented at LFNW 2008-

这是我用来从我的邮箱中获取 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 <[email protected]>
'''
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()

回答by ayaz

Did you forget to specify the IMAP host and port? Use something to the effect of:

您是否忘记指定 IMAP 主机和端口?使用一些东西来达到以下效果:

M = imaplib.IMAP4_SSL( 'imap.gmail.com' )

or,

或者,

M = imaplib.IMAP4_SSL()
M.open( 'imap.gmail.com' )

回答by Tobias Kienzler

Instead of M.login(getpass.getuser([email protected]), getpass.getpass(password))you need to use M.login('[email protected]', 'password'), i.e. plain strings (or better, variables containing them). Your attempt actually shouldn't have worked at all, since getpass's getuserdoesn't take arguments but merely returns the user login name. And [email protected]isn't even a valid variable name (you didn't put it into quotes)...

而不是M.login(getpass.getuser([email protected]), getpass.getpass(password))您需要使用M.login('[email protected]', 'password'),即纯字符串(或更好的,包含它们的变量)。您的尝试实际上根本不应该起作用,因为getpass'sgetuser不接受参数而仅返回用户登录名。而且[email protected]甚至不是一个合法的变量名(你没有把它放到引号)...