Thursday, September 16, 2021

Creating more complex html email 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.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.Save()
message.Display()
time.sleep(5)

message.Send()

No comments:

Post a Comment