I'm tired of making daily YWT emails every day ... Even if I create an email template, I still have to put today's date in the subject line ... This is an article that tried to automatically compose Outlook emails with Python. Since this is my first post, there are various strange points, but those are through ...
** For YWT [here](https://www.kikakulabo.com/tpl-ywt/#:~:text=YWT%E3%81%A8%E3%81%AF%E3%80%81%E3 % 80% 8CY% EF% BC% 9A,% E3% 81% AE% E6% B5% 81% E3% 82% 8C% E3% 81% AF% E5% 90% 8C% E3% 81% 98% E3% 81% A7% E3% 81% 99% E3% 80% 82) **
As for 3, yesterday's T is often written in today's Y, so Is there anyone else who needs it?
import win32com.client
import datetime
import re
#Get the body of yesterday's daily report email
today = datetime.datetime.now()
object = win32com.client.Dispatch("Outlook.Application")
ns = object.GetNamespace("MAPI")
folder = ns.GetDefaultFolder(6) #6 represents the Outlook Inbox folder
days_cnt = 1
flag = False #True if yesterday's daily email is found, False if not found
#Find yesterday's daily report
while flag == False:
yesterday = today - datetime.timedelta(days=days_cnt)
yesterday_sub ="Daily report[{}Month{}Day]".format(yesterday.month,yesterday.day)
for i in reversed(folder.Items):
if yesterday_sub in i.Subject:
text = i.Body #Insert the body of yesterday's daily report email
flag = True #True because yesterday's daily report mail was found
break
days_cnt += 1 #If you can't find the date's daily email, do it yesterday on that date
#Extract the contents of <What to do next>
result = re.findall('<What to do next>[^<]+',text)
b = result[0].replace('<What to do next>', '')
#Extract the contents of "..."
result2 = re.findall('・.+\r\n',b)
sentence = ""
for i in result2:
sentence += i
#send e-mail
object = win32com.client.Dispatch("Outlook.Application")
mail = object.CreateItem(0)
mail.BodyFormat = 1
#Destination setting To,CC,Bcc
mail.To = "[email protected]" #My email address
# mail.cc = "[email protected]"
# mail.Bcc = "[email protected]"
mail.Subject = "Daily report[{}Month{}Day]".format(today.month,today.day) #Email subject
#Email body
mail.Body = """\
thank you for your hard work.
I will send you a daily report.
<What I did>
"""+sentence+"""\
<What I found>
<What to do next>
that's all
"""
mail.Display(True) #View the created email
# mail.Send() #send e-mail
** The red line is your email address **
I was able to automatically compose Outlook emails in Python. This saves you a little time and effort in creating daily emails!
I want to refactor when the time is right! Teams and Mattermost sometimes submit daily reports with YWT, so I would like to create these two automatically.
Create email template Send Outlook email with Python
Recommended Posts