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()




Creating html mail in outlook using VBA

     'Used with GetDefaultFolder of the NameSpace
    Public Const olFolderCalendar = 9 'The Calendar folder.
    Public Const olFolderConflicts = 19 'The Conflicts folder (subfolder of the Sync Issues folder). Only available for an Exchange account.
    Public Const olFolderContacts = 10 'The Contacts folder.
    Public Const olFolderDeletedItems = 3 'The Deleted Items folder.
    Public Const olFolderDrafts = 16 'The Drafts folder.
    Public Const olFolderInbox = 6 'The Inbox folder.
    Public Const olFolderJournal = 11 'The Journal folder.
    Public Const olFolderJunk = 23 'The Junk E-Mail folder.
    Public Const olFolderLocalFailures = 21 'The Local Failures folder (subfolder of the Sync Issues folder). Only available for an Exchange account.
    Public Const olFolderManagedEmail = 29 'The top-level folder in the Managed Folders group. For more information on Managed Folders, see the Help in Microsoft Outlook. Only available for an Exchange account.
    Public Const olFolderNotes = 12 'The Notes folder.
    Public Const olFolderOutbox = 4 'The Outbox folder.
    Public Const olFolderSentMail = 5 'The Sent Mail folder.
    Public Const olFolderServerFailures = 22 'The Server Failures folder (subfolder of the Sync Issues folder). Only available for an Exchange account.
    Public Const olFolderSuggestedContacts = 30 'The Suggested Contacts folder.
    Public Const olFolderSyncIssues = 20 'The Sync Issues folder. Only available for an Exchange account.
    Public Const olFolderTasks = 13 'The Tasks folder.
    Public Const olFolderToDo = 28 'The To Do folder.
    Public Const olPublicFoldersAllPublicFolders = 18 'The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account.
    Public Const olFolderRssFeeds = 25 'The RSS Feeds folder.

Sub setoutlookNS()

Dim olApp, olAccts As Object
Set olApp = CreateObject("Outlook.Application")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Dim olNewMail As Outlook.MailItem
Set olNewMail = olFolder.Items.Add 'creating new mail
With olNewMail
'.SentOnBehalfOfName = """Sourav IT Faculty"" <allsourav@gmail.com>"
.To = "allsourav2@gmail.com;allsourav@gmail.com;"
.CC = "souravandamiya@gmail.com"

.Subject = "Happy Birthday on 14.09.2021"
.Body = "Wish you a happy birthday ,\n learning outlook using python again"
.Importance = 2
.ReadReceiptRequested = True
.OriginatorDeliveryReportRequested = True

Set olAccts = olApp.Session.Accounts
For Each olAcct In olAccts
If olAcct.SmtpAddress = "allsourav@gmail.com" Then
.SendUsingAccount = olAcct
Exit For
End If


Next olAcct
'MsgBox (olApp.Session.Accounts.Count)

.BodyFormat = 2 'olFormatHTML
.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>"


.Display
'.Send


End With

End Sub

Send outlook mail from non primary account using VBA

 

   'Used with GetDefaultFolder of the NameSpace
    Public Const olFolderCalendar = 9 'The Calendar folder.
    Public Const olFolderConflicts = 19 'The Conflicts folder (subfolder of the Sync Issues folder). Only available for an Exchange account.
    Public Const olFolderContacts = 10 'The Contacts folder.
    Public Const olFolderDeletedItems = 3 'The Deleted Items folder.
    Public Const olFolderDrafts = 16 'The Drafts folder.
    Public Const olFolderInbox = 6 'The Inbox folder.
    Public Const olFolderJournal = 11 'The Journal folder.
    Public Const olFolderJunk = 23 'The Junk E-Mail folder.
    Public Const olFolderLocalFailures = 21 'The Local Failures folder (subfolder of the Sync Issues folder). Only available for an Exchange account.
    Public Const olFolderManagedEmail = 29 'The top-level folder in the Managed Folders group. For more information on Managed Folders, see the Help in Microsoft Outlook. Only available for an Exchange account.
    Public Const olFolderNotes = 12 'The Notes folder.
    Public Const olFolderOutbox = 4 'The Outbox folder.
    Public Const olFolderSentMail = 5 'The Sent Mail folder.
    Public Const olFolderServerFailures = 22 'The Server Failures folder (subfolder of the Sync Issues folder). Only available for an Exchange account.
    Public Const olFolderSuggestedContacts = 30 'The Suggested Contacts folder.
    Public Const olFolderSyncIssues = 20 'The Sync Issues folder. Only available for an Exchange account.
    Public Const olFolderTasks = 13 'The Tasks folder.
    Public Const olFolderToDo = 28 'The To Do folder.
    Public Const olPublicFoldersAllPublicFolders = 18 'The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account.
    Public Const olFolderRssFeeds = 25 'The RSS Feeds folder.

Sub setoutlookNS()

Dim olApp, olAccts As Object
Set olApp = CreateObject("Outlook.Application")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Dim olNewMail As Outlook.MailItem
Set olNewMail = olFolder.Items.Add 'creating new mail
With olNewMail
'.SentOnBehalfOfName = """Sourav IT Faculty"" <allsourav@gmail.com>"
.To = "allsourav2@gmail.com;allsourav@gmail.com;"
.CC = "souravandamiya@gmail.com"

.Subject = "Happy Birthday on 14.09.2021"
.Body = "Wish you a happy birthday ,\n learning outlook using python again"
.Importance = 2
.ReadReceiptRequested = True
.OriginatorDeliveryReportRequested = True

Set olAccts = olApp.Session.Accounts
For Each olAcct In olAccts
If olAcct.SmtpAddress = "allsourav@gmail.com" Then
.SendUsingAccount = olAcct
Exit For
End If


Next olAcct
'MsgBox (olApp.Session.Accounts.Count)



.Display
.Send


End With

End Sub

Wednesday, September 15, 2021

Send outlook mail from non primary account using python

 import win32com.client as client
import datetime
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.Save()
message.Display()
message.Send()

Tuesday, September 14, 2021

Instruct outlook to send a mail in future in a predetermined time using python(deferred delivery)

 import win32com.client as client
import datetime
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"
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.Save()
message.Display()
#message.Send()