Created
January 21, 2015 18:56
-
-
Save exit99/ee14d38580cf1594eef2 to your computer and use it in GitHub Desktop.
Connecting via Imap to 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
EMAIL_USE_TLS = True | |
EMAIL_HOST = 'smtp.gmail.com' | |
EMAIL_HOST_USER = 'YOUR GMAIL USERNAME' | |
EMAIL_HOST_PASSWORD = 'YOUR GMAIL PASSWORD' | |
EMAIL_PORT = 587 | |
DEFAULT_FROM_EMAIL = 'YOUR FROM EMAIL FOR SENDING EMAIL' | |
SERVER_EMAIL = 'SAME AS ABOVE' | |
""" | |
There is also a setting in gmail to have to do. "Allow less secure apps" has to be True. | |
""" |
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
class GmailHelper(object): | |
def __init__(self, *args, **kwargs): | |
"""Defaults to selecting Inbox.""" | |
super(GmailHelper, self).__init__(*args, **kwargs) | |
self.connect() | |
def connect(self, selected="INBOX"): | |
self.imap_server = imaplib.IMAP4_SSL("imap.gmail.com", 993) | |
self.imap_server.login(settings.EMAIL_HOST_USER, | |
settings.EMAIL_HOST_PASSWORD) | |
self.imap_server.select(selected) | |
def get_email_ids(self, tag="(UNSEEN)"): | |
status, email_ids = self.imap_server.search(None, tag) | |
if email_ids: | |
return email_ids[0].split() | |
return None | |
def get_emails(self, email_ids=None, tag="(RFC822)"): | |
"""Defaults to unread.""" | |
data = {} | |
for e_id in email_ids: | |
_, response = self.imap_server.fetch(e_id, tag) | |
data[e_id] = response[0][1] | |
return data | |
def get_email_data(self, email_id, email): | |
date = parser.parse(self._do_regex('Date: .*?:\d\d:\d\d', email)[6:]) | |
subject = self._do_regex('Subject: .*?\r\n', email)[9:] | |
from_addr = self._do_regex('<.*?>', self._do_regex('From: .*?>', | |
email))[1:-1] | |
if not from_addr: | |
data = self._do_regex('Sender: .+', email) | |
from_addr = self._do_regex('\d+?@.+?\..+?\r', data)[:-1] | |
text = self._do_regex(data + '\n\r\n.+', email)[len(data):] | |
msg_type = "SMS" | |
else: | |
text = email.split('Content-Type: text/html')[-1] | |
text = 'Content-Type: text/html' + text | |
msg_type = "email" | |
return { | |
'email_id': email_id, | |
'date': date, | |
'subject': subject, | |
'from': from_addr, | |
'text': text, | |
'type': msg_type, | |
} | |
def _do_regex(self, regex, data): | |
cleaned_data = re.findall(regex, data) | |
try: | |
cleaned_data = cleaned_data[0] | |
except IndexError: | |
return "" | |
else: | |
return cleaned_data | |
@property | |
def unread_emails(self): | |
emails = self.get_emails(self.get_email_ids()) | |
return [self.get_email_data(email_id, email) | |
for email_id, email in emails.iteritems()] | |
def move_to_folder(self, e_id, folder): | |
# TODO make this work | |
self.connect(folder) | |
apply_lbl_msg = self.imap_server.uid('COPY', e_id, folder) | |
if apply_lbl_msg[0] == 'OK': | |
mov, data = self.imap_server.uid('STORE', e_id, '+FLAGS', '(\Deleted)') | |
self.imap_server.expunge() | |
self.connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment