Home Project Source Code Best way to make Automatic Birthday mail sending with Python

Best way to make Automatic Birthday mail sending with Python

0
Automatic Birthday mail sending with Python
Automatic Birthday mail sending with Python

How to make Automatic Birthday mail sending with Python.

Hey friends, Welcome to CodeWithNepal today in this blog you’ll learn How to build Automatic Birthday mail sending with Python. To make an Automatic Birthday mail sending program, we are going to use python. In the earlier blog, I shared Random Quote Generator in JavaScript which I recommend you to watch.

Are you bored with sending birthday wishes to your buddies 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 easy task by coding a Python script?

The first thing we do is import six python libraries:

  • pandas
  • datetime
  • smtplib
  • time
  • requests
  • win10toast

Asunder from this, Also make an Excel sheet containing records like this: Name, Email, Contact, Birthday, and Year.

Automatic Birthday mail sending

For the sending email parts, We define a sendEmail() function which will start a Gmail session, send the email, and quit the session.

For the SMS function, we must have an account on fast2sms from where we will get an API key. This API key is used to send SMS over mobile numbers using your account on fast2sms then We create a sendsms() function which will verify the API key and send SMS.

In the driver code area, we read the data from the Excel sheet and check today’s date with any of the birthdays. If there is a match, we call the sendEmail() and sendsms() procedures and even we add the present year to the Excel sheet. Also, we have used ToastNotifier from the win10toast library to show desktop reports once the e-mail and SMS have been sent successfully.

You might like this:

Automatic Birthday mail sending code Below is the implementation:

# import required packages
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()

# define a function for sending email
def sendEmail(to, sub, msg):

	# connection to gmail
	gmail_obj = smtplib.SMTP('smtp.gmail.com', 587)
	
	# starting the session
	gmail_obj.starttls()	
	
	# login using credentials
	gmail_obj.login(GMAIL_ID, GMAIL_PWD)
	
	# sending email
	gmail_obj.sendmail(GMAIL_ID, to,
				f"Subject : {sub}\n\n{msg}")
	
	# quit the session
	gmail_obj.quit()
	
	print("Email sent to " + str(to) + " with subject "
		+ str(sub) + " and message :" + str(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)

# define a function for sending sms	
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_obj = requests.request("POST", url,
								data = payload,
								headers = headers)
	print(response_obj.text)
	print("SMS sent to " + str(to) + " with subject :" +
		str(sub) + " and message :" + str(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)

# driver code
if __name__=="__main__":

	# read the excel sheet having all the details
	dataframe = pd.read_excel("excelsheet.xlsx")
	
	# today date in format : DD-MM
	today = datetime.datetime.now().strftime("%d-%m")
	
	# current year in format : YY
	yearNow = datetime.datetime.now().strftime("%Y")
	
	# writeindex list
	writeInd = []												

	for index,item in dataframe.iterrows():
	
		msg = "Many Many Happy Returns of the day dear " + str(item['NAME'])
				
		# stripping the birthday in excel
		# sheet as : DD-MM
		bday = item['Birthday'].strftime("%d-%m")	
		
		# condition checking
		if (today == bday) and yearNow not in str(item['Year']):
			
			# calling the sendEmail function
			sendEmail(item['Email'], "Happy Birthday",
					msg)
			
			# calling the sendsms function
			sendsms(item['Contact'], msg, item['NAME'],
					"Happy Birthday")
			
			writeInd.append(index)								

	for i in writeInd:
	
		yr = dataframe.loc[i,'Year']
		
		# this will record the years in which
		# email has been sent
		dataframe.loc[i,'Year'] = str(yr) + ',' + str(yearNow)			

	dataframe.to_excel('excelsheet.xlsx',
				index = False)

That’s all, now you’ve successfully built Automatic Birthday mail sending with Python If your code doesn’t work or you’ve faced any problems, please free to comment down I will provide the source code files.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version