[Python] I tried to automatically create a daily report of YWT with Outlook mail

Overview

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

Thing you want to do

  1. I want to automatically create a YWT template for daily report mail
  2. I want to put today's date in the subject
  3. Change the of T of yesterday's daily report mail to of Y of today's daily report mail.

As for 3, yesterday's T is often written in today's Y, so Is there anyone else who needs it?

code

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

result

Click here for yesterday's daily report email

#### Automatically create today's daily report

** The red line is your email address **

Summary

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.

reference

Create email template Send Outlook email with Python

Recommended Posts

[Python] I tried to automatically create a daily report of YWT with Outlook mail
[Outlook] I tried to automatically create a daily report email with Python
I tried to automatically create a report with Markov chain
I tried to create a list of prime numbers with python
I tried to automatically generate a password with Python3
I tried to improve the efficiency of daily work with Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to create a table only with Django
I tried to draw a route map with Python
When I tried to create a virtual environment with Python, it didn't work
I tried to easily create a fully automatic attendance system with Selenium + Python
I tried to create a model with the sample of Amazon SageMaker Autopilot
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to find the entropy of the image with python
I tried various methods to send Japanese mail with Python
Create a Mastodon bot with a function to automatically reply with Python
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
I tried a stochastic simulation of a bingo game with Python
I tried scraping food recall information with Python to create a pandas data frame
I tried to automatically generate OGP of a blog made with Hugo with tcardgen made by Go
I tried to create a Python script to get the value of a cell in Microsoft Excel
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
A memorandum when I tried to get it automatically with selenium
I tried to make a regular expression of "amount" using Python
[Python] A memo that I tried to get started with asyncio
I tried to make a regular expression of "date" using Python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to create Bulls and Cows with a shell program
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a mechanism of exclusive control with Go
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I tried a functional language with Python
Python: I tried to make a flat / flat_map just right with a generator
I tried to communicate with a remote server by Socket communication with Python.
I made a tool to automatically browse multiple sites with Selenium (Python)
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to automatically extract the movements of PES players with software
I tried to create a plug-in with HULFT IoT Edge Streaming [Execution] (3/3)
I tried to discriminate a 6-digit number with a number discrimination application made with python
A beginner of machine learning tried to predict Arima Kinen with python
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried to create a plug-in with HULFT IoT Edge Streaming [Setup] (1/3)
I tried to build a Mac Python development environment with pythonz + direnv
I tried to create a sample to access Salesforce using Python and Bottle
I tried to implement a card game of playing cards in Python
I tried hundreds of millions of SQLite with python
I want to create a window in Python
I want to make a game with Python
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
Steps to create a Twitter bot with python
I tried to automate sushi making with python