Thursday, September 16, 2021

Creating html mail in outlook using python

 import win32com.client as client
import datetime,time
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
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.Save()
message.Display()
time.sleep(5)

message.Send()




No comments:

Post a Comment