Last active
July 3, 2019 15:22
-
-
Save tyleha/fe00a92d01346b29b002 to your computer and use it in GitHub Desktop.
A simple IMAP manager class for Gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from imaplib import IMAP4_SSL | |
import email as em | |
from email.utils import parsedate, parsedate_tz | |
from email.parser import HeaderParser | |
class GmailAccount(object): | |
def __init__(self, username=None, password=None, folder=None): | |
self.username = username | |
self.password = password | |
self.folder = folder | |
def login(self): | |
self.conn = IMAP4_SSL('imap.gmail.com') | |
response = self.conn.login(self.username, self.password) | |
return response | |
def search(self, query, folder=None, readonly=False): | |
ff = self.folder if self.folder else folder | |
self.conn.select(ff, readonly) | |
resp, data = self.conn.search(None, query) | |
return data | |
def fetch(self, uids, query): | |
uid_arr = b','.join(uids[0].split()) | |
resp, data = self.conn.fetch(uid_arr, query) | |
return data | |
def fetch_and_parse(self, uids, query): | |
data = self.fetch(uids, query) | |
parser = HeaderParser() | |
emails = [] | |
for email in data: | |
if len(email) < 2: | |
continue | |
msg = em.message_from_bytes(email[1]).as_string() | |
emails.append(parser.parsestr(msg)) | |
return emails | |
def load_parse_query(self, search_query, fetch_query, folder=None, readonly=False): | |
'''Perform search and fetch on an imap Gmail account. After fetching relevant info | |
from fetch query, parse into a dict-like email object, return list of emails.''' | |
uids = self.search(search_query, folder, readonly) | |
return self.fetch_and_parse(uids, fetch_query) |
After importing GmailAccount, it gives me error...
import GmailAccount # my package
ImportError: No module named 'GmailAccount'
I am trying to use this but no matter what I try I can't seem to get the email body to appear. Any ides?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am getting error running this script...