Skip to content

Commit

Permalink
Something is wrong with sending messages. I'm trying to sort it out
Browse files Browse the repository at this point in the history
but it is being a really pain in the neck.
  • Loading branch information
Toben Archer committed May 7, 2015
1 parent c4f68f3 commit 5760863
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 37 deletions.
30 changes: 26 additions & 4 deletions O365/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, json=None, auth=None):
self.hasAttachments = json['HasAttachments']

else:
self.json = {}
self.json = {'Message':{'Body':{}},'ToRecipients':{}}
self.hasAttachments = False

self.auth = auth
Expand Down Expand Up @@ -101,13 +101,24 @@ def sendMessage(self):

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

data = json.dumps(self.json)
# data = json.dumps(self.json)

data = {'Message':{'Body':{}}}
data['Message']['Subject'] = self.json['Subject']
data['Message']['Body']['Content'] = self.json['Body']['Content']
data['Message']['Body']['ContentType'] = self.json['Body']['ContentType']
data['Message']['ToRecipients'] = self.json['ToRecipients']
data['SaveToSentItems'] = "false"

log.debug(str(data))

response = requests.post(self.send_url,data,headers=headers,auth=self.auth)
log.debug('response from server for sending message:'+str(response))

return True




def markAsRead(self):
'''marks analogous message as read in the cloud.'''
Expand Down Expand Up @@ -176,14 +187,25 @@ def addRecipient(self,name,address):
name -- the name of the person you are sending to. mostly just a decorator.
address -- the email address of the person you are sending to. <<< Important that.
'''
if name is None:
name = address[:address.index('@')]
self.json['ToRecipients'].append({'EmailAddress':{'Address':address,'Name':name}})

def setSubject(self,val):
'''Sets the subect line of the email.'''
self.json['Subject']
self.json['Subject'] = val

def setBody(self,val):
'''Sets the body content of the email.'''
self.json['Body']['Content']
cont = False

while not cont:
try:
self.json['Body']['Content'] = val
self.json['Body']['ContentType'] = 'Text'
cont = True
except:
self.json['Body'] = {}


#To the King!
79 changes: 47 additions & 32 deletions examples/emailprinting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from O365 import *
from printing import *
import json
import os
import sys
import time

def userFromEmail(email):
name = email[:email.index('@')]
Expand All @@ -20,41 +23,53 @@ def verifyUser(email):
return True
# name = email[:email.index(

def getLock():
f = open('emailprinting.lock','r').read()
lock = int(f)
return lock

emails = open('./pw/emails.pw','r').read().split('\n')

if __name__ == '__main__':
print "checking for emails"
with open('./pw/ep.pw','r') as configFile:
config = configFile.read()
cjson = json.loads(config)

e = cjson ['email']
p = cjson ['password']

i = Inbox(e,p)

printer = getRicoh()
print "messages: ",len(i.messages)
for m in i.messages:
m.fetchAttachments()
m.markAsRead()
if not verifyUser(m.json['From']['EmailAddress']['Address']):
print "NOT OMER!"
continue
print "\t attachments: ",len(m.attachments),"from:",userFromEmail(m.json['From']['EmailAddress']['Address']),m.json['Subject']
for att in m.attachments:
printer.setFlag('U',userFromEmail(m.json['From']['EmailAddress']['Address']))
if '.pdf' not in att.json['Name'].lower():
print 'not a pdf. skipping!'
continue
p = att.getByteString()
if not p:
newpid = os.fork()
if newpid > 0:
print newpid
sys.exit(0)

while getLock():
print "checking for emails"
with open('./pw/ep.pw','r') as configFile:
config = configFile.read()
cjson = json.loads(config)

e = cjson ['email']
p = cjson ['password']

i = Inbox(e,p)

printer = getRicoh()
print "messages: ",len(i.messages)
for m in i.messages:
m.fetchAttachments()
m.markAsRead()
if not verifyUser(m.json['From']['EmailAddress']['Address']):
print "NOT OMER!"
continue
print "length of byte string: ",len(p),"for attachment:",att.json['Name']
if p:
print "ready. set. PRINT!"
printer.setFlag('t',att.json['Name'])
ret = printer.sendPrint(p)
print ret
print "\t attachments: ",len(m.attachments),"from:",userFromEmail(m.json['From']['EmailAddress']['Address']),m.json['Subject']
for att in m.attachments:
printer.setFlag('U',userFromEmail(m.json['From']['EmailAddress']['Address']))
if '.pdf' not in att.json['Name'].lower():
print 'not a pdf. skipping!'
continue
p = att.getByteString()
if not p:
continue
print "length of byte string: ",len(p),"for attachment:",att.json['Name']
if p:
print "ready. set. PRINT!"
printer.setFlag('t',att.json['Name'])
ret = printer.sendPrint(p)
print ret
time.sleep(55)

#To the King!
31 changes: 31 additions & 0 deletions examples/simple-message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from O365 import *
import getpass
import json

uname = raw_input('Enter your user name: ')

password = getpass.getpass('Enter your password: ')

auth = (uname,password)

rec = raw_input('Reciving address: ')

subject = raw_input('Subject line: ')

line = 'flarg!'
body = ''
print 'Now enter the body of the message. leave a blank line when you are done.'
while line != '':
line = raw_input()
body += line


m = Message(None,auth)
m.setRecipients(rec)
m.setSubject(subject)
m.setBody(body)

print 'Sending message...'
print json.dumps(m.json)
print m.sendMessage()

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
]

setup(name='O365',
version='0.6',
version='0.6.1',
description='Python library for working with Microsoft Office 365',
author='Toben Archer',
author_email='[email protected]',
Expand Down

0 comments on commit 5760863

Please sign in to comment.