Skip to content

Instantly share code, notes, and snippets.

@cesarvr
Created June 29, 2020 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesarvr/fc2d45361fffa4fe756845a772eddee1 to your computer and use it in GitHub Desktop.
Save cesarvr/fc2d45361fffa4fe756845a772eddee1 to your computer and use it in GitHub Desktop.
This script keep running looking for new emails, when it found one it sends a notification to a cloud service. If this email is a meeting it send's you a mail. You can modify this script to be an automatic email resend.
import win32com.client
import sched, time
import json
import requests
import sys
host = "https://notify-uat.e4ff.pro-eu-west-1.openshiftapps.com/notify"
REDIRECT_TO = 'your-email@server.com'
def look(obj):
object_methods = [method_name for method_name in dir(obj)
if callable(getattr(obj, method_name))]
def reSendEmail(arriving_email):
print "# Resending email #"
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = REDIRECT_TO
mail.Subject = arriving_email.Subject
mail.HTMLBody = '<h2>Meeting invitation</h2>' #this field is optional
# To attach a file to the email (optional):
print 'Attachments: ', arriving_email.Attachments
mail.ReminderSet = arriving_email.ReminderSet
mail.Attachments.Add(arriving_email)
mail.Send()
print "mail: sent."
def send(data):
try:
print "sending: ", data
response = requests.post(host, json=data, verify=False)
print("Status code: ", response.status_code)
print("Printing Entire Post Request")
print(response.json())
except ValueError as error:
print "Error Sending Data: ", error
def makeMessage(mails):
mail = mails[0]
print "last mail: ", mail
print "info mail: ", mail.SenderEmailAddress
print "type: ", mail, " body->", hasattr(mail, 'HTMLBody')
body = "<p> Probably a meeting </p>"
if hasattr(mail, 'HTMLBody'):
body = mail.HTMLBody
else:
reSendEmail(mail)
return [{
"message": mail.Subject,
"sender": mail.SenderName, "body": body
}]
def readMails():
mails = []
outlook = win32com.client.Dispatch("Outlook.Application")
try:
mapi = outlook.GetNamespace("MAPI")
accounts = outlook.Session.Accounts;
for account in accounts:
mails = mapi.Folders(account.DeliveryStore.DisplayName).Folders('Inbox').Items
print "size: ", len(mails)
except ValueError as error:
print "Getting Emails: ", error
return mails
s = sched.scheduler(time.time, time.sleep)
t = 5
delta = 0
def do_something(sc, delta):
# do your stuff
mails = readMails()
d1 = len(mails)
if delta != d1:
delta = d1
print "mails delta: ", len(mails)
msg = makeMessage(mails)
send(msg)
s.enter(t, 1, do_something, (sc,delta))
s.enter(t, 1, do_something, (s,delta))
s.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment