Home |

Trigat

Python E-mail Banner

10-11-2017

Outlook Subject Banner by Josh M
This is a little Python program that grabs the most recent subject line in your Outlook e-mail. It displays the subject and searches for a word of your choosing in the subject line. If the word is found, the banner will turn red in color.

Language or Platform: Python

Code:

# Outlook Subject Banner by Josh M
# This is a little Python program that grabs the most recent subject line in your Outlook e-mail
# It displays the subject and searches for a word of your choosing in the subject line.
# If the word is found, the banner will turn red in color.

# It still needs some work done.

import win32com.client
import os
# import threading # use the Timer
import tkinter

from tkinter import Tk, Label, Button

class myGUI:
            
    def timer(self):

        import pythoncom           # These 2 lines are here because COM library
        pythoncom.CoInitialize()   # is not initialized in the new thread
    
        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
        
        inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                            # the inbox. You can change that number to reference
                                            # any other folder
        
        messages = inbox.Items
        message = messages.GetLast()
        body_content = message.Subject # can be Body, To, REcipients, Sender, etc
    
        if ' Migration ' in body_content:

            self.answer_label['text'] = (body_content) # orginally used body_content.encode("utf-8") to fixed character encoding issue
                             # caused by Windows CMD.  But then figured out you can type 'chcp 65001' in cmd
        else:

            self.answer_label2['text'] = (body_content)
            
#       threading.Timer(5, self.timer).start() # refresh rate goes here   ## Originally used threading but that does not work
                                                                          ## well with Tkinter GUI
        self.master.after(20, self.timer) # after needs to be called from an existing widget such as master
                                          # In this case, the after method is used to refresh the e-mails instead of threading


    def __init__(self, master):
        self.master = master
        master.title("CheckStat")

        
        self.answer_label = Label(master, text='', fg="light green", bg="dark green", font="Helvetica 64 bold italic")
        self.answer_label.grid(column=0, row=0)
        
        self.answer_label2 = Label(master, text='', fg="red", bg="dark red", font="Helvetica 64 bold italic")
        self.answer_label2.grid(column=0, row=0)
        
        self.timer()
        
        
root = Tk()
my_gui = myGUI(root)
root.mainloop()
root.geometry("10x10")


    
    

Back