In case you have to send emails to a list of persons in a specific day every month you can adapt the following script in Python.
This little project has 2 steps:
- Creating the Python Script
- Schedule Task with Windows to automatically execute the script
Let’s first understand the need: in financial area, workday -1 means the last working day of the current month, workday -2 the working day before the last working day of the month, day 1 the first working day of next month and so on.
Every month, you need to send an email in workday -2 to a list of persons in order to remind them about the deadline of the activities that need to be completed in that day.
The issue is that if the last day of month is in weekend, the workday-2 is not last day minus 1. For example, if last day is Saturday, the last working day is Friday and the workday -2 is Thursday. If the last day of the month is on Monday, the workday-2 is not Sunday, it is the previous’s week Friday and so on.
So, let’s create the script which calculates for the current month the workday-2, checks if today is workday -2 and if it is, sends the email to a list of persons:
- Creating the script:
- in this example I used Yahoo e-mail, but it can be modified for Google, Windows, Outlook etc as well.
- If it is the case install Python from here: https://www.python.org/
import datetime import calendar import smtplib from email.mime.text import MIMEText now = datetime.datetime.now() today_date=datetime.date.today() #today's date cy=now.year #current year cm=now.month #current month last_day=calendar.monthrange(cy,cm)[1] #last day of currrent month #the date and name of the last day of month date_ld=datetime.date(cy,cm,last_day) date_ld_name=calendar.day_name[date_ld.weekday()] #the date of last Workday of month if date_ld_name=="Saturday": date_ld=date_ld - datetime.timedelta(days=1) elif date_ld_name=="Sunday": date_ld=date_ld - datetime.timedelta(days=2) #if last day is Monday, the wk-2 is previous friday, else it is last workday minus 1 if date_ld_name=="Monday": date_ldm2=date_ld-datetime.timedelta(days=3) else: date_ldm2=date_ld-datetime.timedelta(days=1) #if today is workday-2 then send the reminder via e-mail if today_date==date_ldm2: FROM = "[email protected]" TO = ["[email protected]","2ndaddress","etc"] # must be a list SUBJECT = "Hello!" TEXT = """Hi all, Please note that today is workday -2 for current month and tasks deadline is 1 pm ! This is an automatic email reminder.""" # Prepare actual message message = """From: %srnTo: %srnSubject: %srn %s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) # Send the mail username = str("[email protected]") password = str("your password") server = smtplib.SMTP("smtp.mail.yahoo.com",587,timeout=10) server.set_debuglevel(1) try: server.starttls() server.login(username,password) server.sendmail(FROM, TO, message) print ("The reminder e-mail for WK-2 was sent !") except: print("Couldn't send e-mail regarding WK-2") finally: server.quit() input("Press any key to exit..") #if today si not workday -2 do nothing ! else: print("Today is not workday -2") input("Press any key to exit")
* Save the script on your computer. I named mine: “Check day-2 and send mail.py”
2. Schedule the script to run every day at 10:00 AM:
- Go to Windows- Start and Find the Task Scheduler:
- Press “Create Basic Task“:
- Enter task name and description
- At “Trigger” select Daily:
- Then specify the starting date and time:
- Action: select Start a Program:
- Add the parameters as follows:
At program/script: the path to your python.exe file. In my case it was:
C:UsersUSERAppDataLocalProgramsPythonPython35-32python.exe
At Arguments the path and name of your script between quotes. In my case it was:
“C:UsersUSERDesktopCheck day-2 and send mail.py”
- Click Finish:
–> Now your task is scheduled, will run daily and the script will send the e-mail only in Workdays -2 dates.
This way you will not forget to send the reminder and you will not spend time adding the email addresses and writing the email every month !
Download the Python Script:
Usually I never comment on blogs but your article is so convincing that I never
stop myself to say something about it. You’re doing a great job Man,Keep it
up.
I am an accountant and also a passionate python coder. And I use python coding to do all my daily task. Currently, I was stuck at building an email client for my personal use.
Your guide helped me a lot.