imaplib

Find new messages added to an imap mailbox since I last checked with python imaplib2?

江枫思渺然 提交于 2019-12-10 10:12:53
问题 I am trying to write a program that monitors an IMAP mailbox and automatically copies every new incoming message into an "Archive" folder. I'm using imaplib2 which implements the IDLE command. Here's my basic program: M = imaplib2.IMAP4("mail.me.com") M.login(username,password) lst = M.list() assert lst[0]=='OK' for mbx in lst[1]: print "Mailboxes:",mbx def process(m): print "m=",m res = M.recent() print res M.select('INBOX') M.examine(mailbox='INBOX',callback=process) while True: print

How to tell whether imaplib2 idle response resulted from timeout

别等时光非礼了梦想. 提交于 2019-12-09 19:31:16
问题 I'm using imaplib2 (docs) to interact with an IMAP server. I'm using the idle command, with a timeout and a callback. The problem is, I don't see any way of telling if the callback was triggered by the timeout being reached, or if there was a change on the server that I need to check out. I just get ('OK', ['IDLE terminated (Success)']) every time. Here's the debug output for both cases: Timedout: 15:43.94 MainThread server IDLE started, timeout in 5.00 secs 15:48.94 imap.gmail.com handler

use imaplib and oauth for connection with Gmail

China☆狼群 提交于 2019-12-08 22:58:20
问题 I want to use Oauth to connect to Gmail in Python. Right now I've got the xoauth.py script from Google (link), and generating a token works all fine, but how can I then use that in another script? It's going to be in Django. Right now my script logs in like this: m = imaplib.IMAP4_SSL("imap.gmail.com") m.login("example@gmail.com", "password") But I want something more secure. 回答1: Here's an example using the oauth2 module to authenticate using oauth, taken from the readme: import oauth2 as

Python imaplib Search using senders domain

我是研究僧i 提交于 2019-12-08 11:24:32
问题 I am trying to search using the IMAP search function to only return users that have sent emails from a specific domain. I am hoping this is actually possible. For example let's say I want to only fetch emails from users sending from addresses coming from @stackoverflow.com. We connect, login etcetera then search as below. c = imaplib.IMAP4(imapserver) c.starttls() c.login(username, password) c.list() c.select('INBOX') c.search(None, 'UNSEEN FROM "@stackoverflow.com"') However this returns no

add a custom RFC822 header via IMAP?

人走茶凉 提交于 2019-12-08 02:55:39
问题 Is there an easy way to add a custom RFC822 header to a message on an IMAP server with imaplib? I am writing a python-based program that filters my IMAP mail store. When I did this with Procmail I had the option of adding headers. But there doesn't seem to be a way to do that with the Python imap implementation. Specifically, I want to add a custom header like: X-VY32-STATUS: Very Cool So that it appears in the mail headers: To: vy32@stackoverflow.com From: Yo@mama.com Subject: Test Message X

Downloading MMS emails sent to Gmail using Python

♀尐吖头ヾ 提交于 2019-12-08 02:03:24
问题 So I tried the code below and it downloads attachments alright. The problem is on my gmail account, there are emails that were sent using MMS mail through mobile phone. Email attachments from mobile network A can be downloaded by the script below , while those that came from mobile network B fails. Here are the links to the full email details : http://pastie.org/private/ektv7yfa2xwdqzu77ys5a (From Mobile Network A, works) http://pastie.org/private/cljaaad4tz7v5jra20l0q (From Mobile Network B,

How to catch socket timeout in Python 3?

a 夏天 提交于 2019-12-07 08:04:23
Part of my script: def testConnection(self): # This code doesn't work try: self.imap.login(self.user, self.password) return True except: return False When I try to connect with imaplib to mail server with wrong settings, script always crashes with this error: Traceback (most recent call last): File "./mail-notifier.py", line 198, in <module> mail_check() File "./mail-notifier.py", line 161, in mail_check if (SettingsExist() == True and Mail().testConnection() == True): File "./mail-notifier.py", line 142, in __init__ self.imap = imaplib.IMAP4_SSL(settings.value("MailServer"), settings.value(

imaplib/gmail how to download full message (all parts) while not marking read [duplicate]

梦想与她 提交于 2019-12-07 07:33:52
问题 This question already has answers here : Fetch an email with imaplib but do not mark it as SEEN (4 answers) Closed 3 years ago . I inadvertently marked all the messages in my inbox as read with this python statement: status, data = conn.uid('fetch', fetch_uids, '(RFC822)') But I was able to walk through all the parts of the message with the following set of statments: email_message = email.message_from_string(data[0][1]) for part in email_message.walk(): print '\n' print 'Content-Type:',part

Python IMAP4 select mailbox name contain space character, error

浪子不回头ぞ 提交于 2019-12-07 03:01:31
I am using Python imaplib.IMAP4 to connect to Google email. Every thing is okay except when I use IMAP4.select() method using a mailbox name that contains a space character. Let's say my mailbox name is "Gama Sutra". When I execute imap.select('[Gmail]/Gama Sutra') it gives me this error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 682, in select typ, dat = self._simple_command(name, mailbox) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4

Finding links in an emails body with Python

只愿长相守 提交于 2019-12-06 13:27:38
问题 I am currently working on a project in Python that would be connecting to an email server and looking at the latest email to tell the user if there is an attachment or a link embedded in the email. I have the former working but not the latter. I may be having troubles with the if any() part of my script. As it seems to half work when I test. Although it may be due to how the email string is printed out? Here is my code for connecting to gmail and then looking for the link. import imaplib