Search code examples
pythonhtmlpywin32win32com

Python Outlook - Add date to email


I have automated an outlook email with a python script. What I'd like to do now is enter next weeks date in the body of the email.

Is there any function that will allow me to do this?

For example I want to send an email and in the email I want the ask the recipient to respond by the 29th of April (Exactly a week from todays date). Is there a way I can read todays date and then print out a date that is 7 days later in the email?

Sample code: import win32com.client as client import pathlib import pandas as pd

outlook = client.Dispatch('Outlook.Application')

#Mail item
message = outlook.CreateItem(0)


df = pd.read_excel(r'Desktop\Review.xlsx',index_col=False,sheet_name='Review',  usecols = "A:H")

#Display message
body = df.to_html()

message.Display()

message.To = "[email protected]"
message.Subject = "Review"
message.HTMLBody = "Hi All, <br> <br>Please respond by this day next week **Enter date here**
#message.Send()

Solution

  • import datetime
    
    # how many days allowance?
    N = 7
    
    # assign the deadline date
    deadline = datetime.date.today() + datetime.timedelta(days=N)
    
    # on its own, it already works...
    print(f"Please respond by {deadline}.")
    
    # prints out Please respond by 2021-04-29.
    
    # but perhaps you want to format it 
    s = deadline.strftime("%d %b %Y")
    
    print(f"Please respond by {s}.")
    
    # prints out Please respond by 29 Apr 2021.
    

    By the way, please check out https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior on your own for the format codes.