Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update outlook.py #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions outlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import email.mime.multipart
import config
import base64

import quopri

class Outlook():
def __init__(self):
Expand All @@ -22,7 +22,7 @@ def login(self, username, password):
self.imap = imaplib.IMAP4_SSL(config.imap_server,config.imap_port)
r, d = self.imap.login(username, password)
assert r == 'OK', 'login failed: %s' % str (r)
print(" > Signed in as %s" % self.username, d)
print(" > Signed in as %s" % self.username, d[0].decode('utf-8'))
return
except Exception as err:
print(" > Sign in error: %s" % str(err))
Expand Down Expand Up @@ -74,7 +74,9 @@ def sendEmail(self, recipient, subject, message):
if attempts < 3:
continue
raise Exception("Send failed. Check the recipient email address")

def list_folders(self):
resp, data = self.imap.list()
return [i.decode().split(' "/" ')[1].replace('"','') for i in data]
def list(self):
# self.login()
return self.imap.list()
Expand All @@ -97,41 +99,41 @@ def since_date(self, days):

def allIdsSince(self, days):
r, d = self.imap.search(None, '(SINCE "'+self.since_date(days)+'")', 'ALL')
list = d[0].split(' ')
list = d[0].decode('utf-8').split(' ')
return list

def allIdsToday(self):
return self.allIdsSince(1)

def readIdsSince(self, days):
r, d = self.imap.search(None, '(SINCE "'+self.date_since(days)+'")', 'SEEN')
list = d[0].split(' ')
list = d[0].decode('utf-8').split(' ')
return list

def readIdsToday(self):
return self.readIdsSince(1)

def unreadIdsSince(self, days):
r, d = self.imap.search(None, '(SINCE "'+self.since_date(days)+'")', 'UNSEEN')
list = d[0].split(' ')
list = d[0].decode('utf-8').split(' ')
return list

def unreadIdsToday(self):
return self.unreadIdsSince(1)

def allIds(self):
r, d = self.imap.search(None, "ALL")
list = d[0].split(' ')
list = d[0].decode('utf-8').split(' ')
return list

def readIds(self):
r, d = self.imap.search(None, "SEEN")
list = d[0].split(' ')
list = d[0].decode('utf-8').split(' ')
return list

def unreadIds(self):
r, d = self.imap.search(None, "UNSEEN")
list = d[0].split(' ')
list = d[0].decode('utf-8').split(' ')
return list

def hasUnread(self):
Expand All @@ -147,8 +149,8 @@ def getIdswithWord(self, ids, word):
return stack

def getEmail(self, id):
r, d = self.imap.fetch(id, "(RFC822)")
self.raw_email = d[0][1]
r, d = self.imap.fetch(bytes(str(id),'utf-8'), "(RFC822)")
self.raw_email = quopri.decodestring(d[0][1]).decode('utf-8')
self.email_message = email.message_from_string(self.raw_email)
return self.email_message

Expand Down Expand Up @@ -181,8 +183,8 @@ def writeEnable(self, folder):
def rawRead(self):
list = self.readIds()
latest_id = list[-1]
r, d = self.imap.fetch(latest_id, "(RFC822)")
self.raw_email = d[0][1]
r, d = self.imap.fetch(bytes(latest_id,'utf-8'), "(RFC822)")
self.raw_email = d[0][1].decode('utf-8')
return self.raw_email

def mailbody(self):
Expand Down