Опираясь на ответ Авадеша:
#! /usr/bin/env python3.4
import getpass
import imaplib
mail = imaplib.IMAP4_SSL('imap.server.com')
mypassword = getpass.getpass("Password: ")
address = 'your@email.com'
mail.login(address, mypassword)
mail.select("inbox")
print("Checking for new e-mails for ",address,".", sep='')
typ, messageIDs = mail.search(None, "UNSEEN")
messageIDsString = str( messageIDs[0], encoding='utf8' )
listOfSplitStrings = messageIDsString.split(" ")
if len(listOfSplitStrings) == 0:
print("You have no new e-mails.")
elif len(listOfSplitStrings) == 1:
print("You have",len(listOfSplitStrings),"new e-mail.")
else:
print("You have",len(listOfSplitStrings),"new e-mails.")