2
import imaplib

usr = 'someuser'
pwd = 'somepwd'

imap_server = imaplib.IMAP4_SSL('imap.gmail.com', 993)
imap_server.login(USER, PASSWORD)
imap_server.select('Inbox')

for message_id in imap_server.search(None, '(FROM "example email.com")')[1][0].split(" "):
    response = imap_server.fetch(message_id, "RFC822.TEXT")
    print response[1][0][1]

I want to search for email with a certain subject and from email address. How do I adjust this code to fit incorporate both check?

Also, once i open a message, I want to search the message for certain content. What is the best way to go about this? Thanks!

2
  • 1
    Please don't make your entire question boldface. It looks horrid and it feels like you're screaming at me. Commented Feb 13, 2012 at 18:47
  • Also, take a look at this: libgmail.sourceforge.net Commented Feb 13, 2012 at 18:47

1 Answer 1

2

The python imap wrapper is very thin - it simply exposes the imap specification, see http://www.faqs.org/rfcs/rfc3501.html

in the fetch command you can fetch several parts of the mail, you choose to fetch the entire RFC formated mail. You can also search like this:

typ, data = imap_server.search(None, '(SUBJECT "Whatever you are searching for")')

You already showed how to fetch a mail. When you have the RFC-mail in your hands. Search in this mail, depends on what your criteria is: a regex, something in the headers, ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.