Showing posts with label Adding signature other than default in outlook using python. Show all posts
Showing posts with label Adding signature other than default in outlook using python. Show all posts

Saturday, September 18, 2021

Adding signature other than default in outlook using python

import win32com.client as client
import datetime,time
from os import path
from tkinter import *
from tkinter import filedialog
data=""
def openSig(filepath):
    
    print(filepath)
    file=open(filepath,'r')
    sig=(file.read())
    file.close()
    return sig


def sendmail(data):
    
    outlook=client.Dispatch("Outlook.Application")
    message=outlook.Createitem(0)
    namespace=outlook.GetNameSpace('MAPI')
    inbox=namespace.GetDefaultFolder(6)
    message=inbox.items.add
    message.To="allsourav@gmail.com;allsourav2@gmail.com"
    message.CC="souravandamiya@gmail.com"
    message.BCC="allsourav2@gmail.com"
    From = None
    #below lines are for default signature
    #currentSig = "<br/>Thank You<br/><br/>Sourav Bhattacharya"
    #olInspect = message.GetInspector #activates email,prompts signature to appear,not display
    #currentSig = message.HTMLBody #contains nothing but signature now
    filepath=path.expandvars(r'%APPDATA%\Microsoft\Signatures\demo (allsourav@hotmail.com).htm')
    currentSig=openSig(filepath)
    
    for myEmailAddress in outlook.Session.Accounts:
        
        if str(myEmailAddress)=="allsourav@gmail.com":
            From = myEmailAddress
            break

    if From != None:
        # This line basically calls the "mail.SendUsingAccount = xyz@email.com" outlook VBA command
        message._oleobj_.Invoke(*(64209, 0, 8, 0, From))

    ##message.SentOnBehalfOfName='"Sourav IT Faculty" <allsourav@gmail.com>'
    ##this method does not work with new version of outlook,it is for sending
    ##mail from non primary account set up in outlook

       
    message.Subject="Happy Birthday on 14.09.2021"
    message.Body="Wish you a happy birthday ,\n learning outlook using python again"
    message.Importance=2
    message.ReadReceiptRequested=True
    message.OriginatorDeliveryReportRequested=True
    #message.DeferredDeliveryTime="15/09/2021 01:31:05 AM"
    #cSendDate=datetime.date.today() #todays date
    #cSendDate=(datetime.date.today() + datetime.timedelta (days=1)).strftime("%d-%m-%y") #tomorrows date
    #cSendDate=datetime.datetime.now() + datetime.timedelta(days=1) #1 day from current datetime
    #cSendDate=datetime.datetime.now() + datetime.timedelta(minutes=2) #2 minutes from now
    #cSendDate=cSendDate.strftime("%d/%m/%Y %H:%M:%S %p") #formats the date like the outlook wants it in "15/09/2021 01:31:05 AM" in this format
    #print(cSendDate)
    #message.DeferredDeliveryTime=cSendDate

    message.BodyFormat = 2 #olFormatHTML
    #message.HTMLBody = "<html><h2>The body <span style='color:red'>of <b>Our Email </b></span></h2> <body>Regular Stuff Here <br/> New Line </body></html>"
    #message.htmlbody="<h2>Welcome To The Best Online HTML Web Editor!</h2><p style=""font-size: 1.5em;"">You can <strong style=""background-color: #317399; padding: 0 5px; color: #fff;"">type your text</strong> directly in the editor or paste it from a Word Doc, PDF, Excel etc.</p><p style=""font-size: 1.5em;"">The <strong>visual editor</strong> on the right and the <strong>source editor</strong> on the left are linked together and the changes are reflected in the other one as you type! <img src=""https://html5-editor.net/images/smiley.png"" alt=""smiley"" /></p>"
    message.htmlbody=data + "<br />" + currentSig
    
    message.Save()
    message.Display()
    time.sleep(5)

    #message.Send()

def openFile():
    filepath = filedialog.askopenfilename(initialdir="C:\\Users\\allso\\Desktop\\outlook programming",
                                          title="Open file okay?",
                                          filetypes= (("text files","*.txt"),
                                          ("all files","*.*")))
    print(filepath)
    file=open(filepath,'r')
    data=(file.read())
    file.close()
    sendmail(data)

    
window=Tk()
button=Button(text="Open",command=openFile)

button.pack()

window.mainloop()