How to Send Birthday Wishes Email with Python?
You might search for How to Send Birthday Wishes Email with Python. There is no doubt that finding relevant sources of information is daunting. However, in this blog, you will find answers to your question on how to Send Birthday Wishes Emails with Python.
Do you always want to wish your family and friends happy birthday at midnight but constantly forget to do so? Well, you are not the only one. Frankly speaking, I always forget. As a programmer, getting stuck in codes and forgetting special dates is quite usual. But with advanced technology, you can send automated birthday emails easily and effortlessly. All you need to do is write a Python script that must read as Happy Birthday. You can also customize your messages by simple editing. I’m pretty sure that these automated happy birthday emails will bring happiness and joy to your friends and family.
Table of contents
Send Happy Birthday Emails with Python Automatically in 4 Steps
Follow these 4 critical steps on how to Send Birthday Wishes Email with Python.
Step 1: Import the necessary libraries.
Step 2: Import email address and password.
Step 3: Send an email to a friend function
Step 4: Compose the primary function
Let’s discuss them in detail!
Import the Necessary Libraries
# import required packages
import pandas as pd
import datetime
import smtplib
import time
import requests
from win10toast import ToastNotifier
The first step in How to Send Birthday Wishes Email with Python is to import the six libraries, Pandas, DateTime, Smtplib, Time, Requests, and ToastNotifier. It will be easy to understand if you are well-versed in programming languages. On the contrary, you can also hire a Python Developer.
Import Your Email Address and Password
# your gmail credentials here
GMAIL_ID = 'your_email_here'
GMAIL_PWD = 'your_password_here'
Once you import libraries, it’s time to add your Gmail address into the ‘your_email_here’ field. Then, add your Gmail password to the ‘your_password_here’ field.
Note: For security concerns, it is advisable to create a new Email id. Anonymous users that have access to your script can manipulate your personal information.
Send an Email To a Friend Function
# 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)
The next step in sending Birthday Wishes Email with Python is the send email function. Follow these instructions Send an Email to Friend function.
- First, connect to the Gmail servers.
- Once connected to the server, log in to your Gmail with the help of the login() function. Add variables to GMAIL_ID and GMAIL_PWD as defined in the previous step.
- In the next step, add your and your friend’s email variables along with the message in the ‘msg’ field.
Note: We will define the message in the Main Function.
- Close the connection using ‘gmail_obj.quit()’.
- Print out the email that has been successfully sent.
- Use ToastNotifier from the win10toast library to enable desktop notifications.
The Main Function
# 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)
The final step in how to Send Birthday Wishes Email with Python is the Main function. Follow these instructions for coding the Main function.
- Import friend’s data from an excel sheet.
- Use the DateTime library to match the current date and year with the birthday list.
Note: You can also create an empty list to store information about friends.
- If there is any match, then call the sendEmail or sendsms function.
- Update the index on the excel sheet and set it to False for every friend the script has sent an email to.
Final Output: How the Birthday Wishes Email Will Look
Now, as you have learned how to Send Birthday Wishes Email with Python, here I am attaching one of the python birthday email outputs. Let’s look at the attached reference email.
Ready to Send Birthday Wishes Email with Python?
If you code details correctly, an Email birthday message will be sent on your friend(s) birthday. But if anything goes wrong, you can hire a Python developer from a top web and application development company, APPWRK IT Solutions. Our professional python programmers can review and create a script per your requirement and customization. Contact APPWRK for more details! If you find the blog on How to Send Birthday Wishes Email with Python helpful, do leave a comment below!
Related Links:
- How To Make Jarvis With Dialogflow And Python
- HOW TO IMPLEMENT DWAVE QBSOLVE IN PYTHON
- Top 10 Python Libraries for Machine Learning
- Making Apps with Python-a complete Guide
- Node.js vs Python: Which is best for backend development in 2023?
- ReactJS vs Python: Which Technology is Best For Your Next Project in 2023?
About author
Whether you are planning a start-up or want to enhance your existing business, APPWRK is a one-stop solution to satisfy your goals and expectations. We have action-oriented experience in UI/UX, Mobile, and Web App development. Also, you can knock on our door for Quality Assurance and Digital Marketing services.
Book A Consultation Now!