15

Can someone help me customize an existing code sample?

I can see from the following article how to connect to gmail and download content, but I can't figure out how to search for a specific email and only download the timestamp and body?

ARTICLE: How can I download all emails with attachments from Gmail?

I specifically want to grab the emails from "Alerts@foobank.com" for the last 5 days and download the send time and body of the emails. I'll then parse this to determine which emails I need to use.

I'm self-taught and am having a hard time customizing the script above to do this.

Any help is much appreciated. Thanks.

JD

Community
  • 1
  • 1
jond
  • 189
  • 1
  • 1
  • 12
  • Which [search criterias](http://www.example-code.com/csharp/imap-search-critera.asp) have you tried so far? – sarnold Jul 07 '11 at 22:57
  • 1
    It is not Gmail related but IMAP/POP I bet, so you should search for that - except you are selfkiller and are doing it with webclient ;p – tmg Jul 07 '11 at 23:28

3 Answers3

21

I suggest using IMAPClient as it papers over many of the more esoteric aspects of IMAP.

The following snippet will pull messages based on your criteria, parse the message strings to email.message.Message instances and print the Date and From headers.

from datetime import datetime, timedelta
import email
from imapclient import IMAPClient

HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True

today = datetime.today()
cutoff = today - timedelta(days=5)

## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')

## Search for relevant messages
## see http://tools.ietf.org/html/rfc3501#section-6.4.5
messages = server.search(
    ['FROM "Alerts@foobank.com"', 'SINCE %s' % cutoff.strftime('%d-%b-%Y')])
response = server.fetch(messages, ['RFC822'])

for msgid, data in response.iteritems():
    msg_string = data['RFC822']
    msg = email.message_from_string(msg_string)
    print 'ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date'])
Rob Cowie
  • 22,259
  • 6
  • 62
  • 56
  • Thanks Rob! I just ported this over from Python2.6 to 2.7 and I now get the following error when calling message_from_string... 'UnicodeEncodeError: ascii codec can't encode character u'\xa0' in position 3041: ordinal not in range(128)'. Do I have to performs some sort of transformation on the msg_String? And I'm reading in the same email, so the email payload cannot be the issue. – jond Jun 16 '13 at 06:09
  • Did you upgrade IMAPClient to 0.10 or greater? Behaviour changed in that release; Functions now return unicode objects (see https://bitbucket.org/mjs0/imapclient/src/tip/NEWS.rst). The particular issue in this case is a non-breaking space. You can remove it safely (probably) with .replace(u'\xa0', u'') – Rob Cowie Jun 16 '13 at 20:30
  • 1
    The `server.search` line above didn't work for me in imapclient 2.0, I had to update the syntax as follows: `messages = server.search(['FROM', "Alerts@foobank.com", 'SINCE', cutoff.strftime('%d-%b-%Y')])` – SoHei Jan 19 '18 at 23:29
3
import imaplib
from datetime import datetime, timedelta

obj = imaplib.IMAP4_SSL('imap.gmail.com',993)
obj.login('username','password')
obj.select()

today = datetime.today()
cutoff = today - timedelta(days=5)
dt = cutoff.strftime('%d-%b-%Y')
typ, data = obj.search(None, '(SINCE %s) (FROM "Alerts@foobank.com")'%(dt,))
print data
Avadhesh
  • 4,519
  • 5
  • 33
  • 44
1
import datetime as dt
from imap_tools import MailBox, Q
date = dt.date.today() - dt.timedelta(days=5)
with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
    for msg in mailbox.fetch(Q(from_='Alerts@foobank.com', date_gte=date)):
        sent_time = msg.date
        body = msg.text or msg.html
        for att in msg.attachments:
            att.filename         # str: 'cat.jpg'
            att.content_type     # str: 'image/jpeg'
            att.payload          # bytes: b'\xff\xd8\xff\xe0\'

*Note that there is no imap search criteria "with attachments"

https://github.com/ikvk/imap_tools

Vladimir
  • 6,162
  • 2
  • 32
  • 36