DEV Community

Ashutosh Krishna
Ashutosh Krishna

Posted on • Edited on • Originally published at 157.230.8.23

2

Automated Birthday Wisher using Python

Hello guys, To kaise hain aaplog?
Ashutosh here again with another Python mini project.
Are you bored of sending birthday wishes to your friends (well,some people are) or do you forget to send wishes to your friends or do you want to wish them at 12 AM but you always fall asleep? Why not automate this simple task using our friend, Python!!!
To chaliye shuru karte hain…
The first thing you have to do is to install pandas on your system using pip install pandas command. We shall use datetime module and SMTP library here to send the mail.
Also create an excel sheet containing Name , Email , Contact, Birthday and Year.

import pandas as pd
import datetime
import smtplib
import time
import requests
from win10toast import ToastNotifier
#your gmail credentials here
GMAIL_ID = 'your_email_here'
GMAIL_PWD = 'your_password_here'
#for desktop notification
toast = ToastNotifier()
#Function for sending email
def sendEmail(to,sub,msg):
s = smtplib.SMTP('smtp.gmail.com',587) #conncection to gmail
s.starttls() #starting the session
s.login(GMAIL_ID,GMAIL_PWD) #login using credentials
s.sendmail(GMAIL_ID,to,f"Subject : {sub}\n\n{msg}") #sending email
s.quit() #quit the session
print(f"Email sent to {to} with subject {sub} and message : {msg}")
toast.show_toast("Email Sent!" , f"{name} was sent e-mail", threaded=True, icon_path=None, duration=6)
while toast.notification_active():
time.sleep(0.1)
def sendsms(to,msg,name,sub):
url = "https://www.fast2sms.com/dev/bulk"
payload = f"sender_id=FSTSMS&message={msg}&language=english&route=p&numbers={to}"
headers = {
'authorization': "API_KEY_HERE",
'Content-Type': "application/x-www-form-urlencoded",
'Cache-Control': "no-cache",
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
print(f"SMS sent to {to} with subject : {sub} and message : {msg}")
toast.show_toast("SMS Sent!" , f"{name} was sent message", threaded=True, icon_path=None, duration=6)
while toast.notification_active():
time.sleep(0.1)
if __name__=="__main__":
df = pd.read_excel("excelsheet.xlsx") #read the excel sheet having all the details
today = datetime.datetime.now().strftime("%d-%m") #today's date in format : DD-MM
yearNow = datetime.datetime.now().strftime("%Y") #current year in format : YY
writeInd = [] #writeindex list
for index,item in df.iterrows():
msg = f"Many Many Happy Returns of the day dear {item['NAME']} !!!!!!\n\n\nThis is an automated email from ******** sent using Python.\n"
bday = item['Birthday'].strftime("%d-%m") #stripping the birthday in excel sheet as : DD-MM
if (today==bday) and yearNow not in str(item['Year']): #condition checking
sendEmail(item['Email'], "Happy Birthday", msg) #calling the sendEmail function
sendsms(item['Contact'], msg, item['NAME'], "Happy Birthday") #calling the sendsms function
writeInd.append(index)
for i in writeInd:
yr = df.loc[i,'Year']
df.loc[i,'Year'] = str(yr) + ',' + str(yearNow) #this will record the years in which email has been sent
df.to_excel('excelsheet.xlsx', index=False) #saving all the changes into the same excel sheet
view raw wisher.py hosted with ❤ by GitHub

First thing we do is import five libraries : pandas, datetime, smtplib, requests and win10toast.
Then we put our gmail credentials in order to login. We define a sendEmail() function which will start a gmail session , send the email and quit the session.
For the SMS part, we must have an account on www.fast2sms.com from where we will get an API key. This API key is used to send SMS over mobile numbers using your account on fast2sms. We have a sendsms() function which will verify the API key and send SMS.
In the main function, we read the excel sheet and match today’s date with any of the birthdays. If there is a match, we call the sendEmail() and sendsms() functions and also we add the current year in the excel sheet.
Also, we have used ToastNotifier from win10toast library to show desktop notifications once the e-mail and SMS has been sent successfully.
To automate the task, we use Task Scheduler in Windows.
I have mentioned all the steps to automate the task in my Github repository :

GitHub logo ashutoshkrris / Automated-Birthday-Wisher

This is a program which will automatically send birthday wishes to your friends using your gmail credentials.

I will also soon be publishing it on GeeksforGeeks.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay