10

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」で、パスワードが「password」だとすると、これはどのようになりますか? 試しM.login(getpass.getuser(email@gmail.com), getpass.getpass(password)) ましたが、タイムアウトしました。ここで newb を完成させたので、明らかな何かを見逃している可能性が非常に高いです (最初に iMAP オブジェクトを作成するなど? よくわかりません)。

4

4 に答える 4

11

これは、メールボックスからログウォッチ情報を取得するために使用していたスクリプトです。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 <bcl@brianlane.com>
'''
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()
于 2008-11-25T05:42:27.147 に答える
11
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")

コードの残りの部分は正しいようです。

于 2008-11-24T22:18:52.130 に答える
2

IMAPホストとポートを指定するのを忘れましたか?次のような効果をもたらすものを使用します。

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

また、

M = imaplib.IMAP4_SSL()
M.open( 'imap.gmail.com' )
于 2008-11-24T20:39:52.750 に答える
0

代わりに、プレーンな文字列 (またはそれらを含む変数)M.login(getpass.getuser(email@gmail.com), getpass.getpass(password))を使用する必要があります。は引数を取らず、ユーザーのログイン名を返すだけなのでM.login('email@gmail.com', 'password')、実際にはまったく機能しないはずです。そして、有効な変数名でさえありません(引用符で囲みませんでした)...getpassgetuseremail@gmail.com

于 2015-04-19T18:01:18.053 に答える