How To Create an Automatic Birthday Wisher To Auto - Wish Friends In Python



 In this post we are going to create  a Python application which will automatically wish someone o their birthday. This application will wish someone automatically on schedule. So let us see how to make it.

Let's open our favourite IDE and create a file named main.py. Now we need to install a few models which will help this program to run, such as pandas, openpyxl etc. We will type the following in the terminal and CMD to install the modules:

1. "pip install pandas"

2.   "pip install openpyxl"

Now we will create an excel sheet on that folder and open it. We will add the following rows, such as Sno , Name, Birthday, few entries to it. Now let's get back to the main.py file. Now we need to create a function which will read the excel file and extract emails we will use the smtblib library. We will be using Gmail for this application. Now we need to create a function which will send emails using a gmail id. Now to use this services you need to enable less secure apps on the message. Now our program is ready. Now to schedule the program you need t6o do the following:

1. We need to create a task scheduler task which will run our main.py file on 12:00 AM everyday so that it can check and wish person on their respective birthday.

2. To do that, you need to copy the path of the file.

3. Now open the task scheduler.

4. Now you need to click on create a task and then triggers.

5. Now click on new and set that as daily and time as 00:00:00 (12:00 AM)

6.Now get back to the general tab and add name and description

7. Now go to action then new and then click on browse.

8. Now on program/script click browse and then choose python.exe by browsing and for arguments just paste the path of the file within the double quote.

9. Now press ok and exit it.

Now our program is ready to run .

Here is the code



import pandas as pd
import datetime
import smtplib
import os
os.chdir(r"Your File/Folder Full Path")
# os.mkdir("testing") 

# Enter your authentication details
GMAIL_ID = ''
GMAIL_PSWD = ''


def sendEmail(to, sub, msg):
    print(f"Email to {to} sent with subject: {sub} and message {msg}" )
    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.starttls()
    s.login(GMAIL_ID, GMAIL_PSWD)
    s.sendmail(GMAIL_ID, to, f"Subject: {sub}\n\n{msg}")
    s.quit()
    

if __name__ == "__main__":
    #just for testing
    # sendEmail(GMAIL_ID, "subject", "test message")
    # exit()

    df = pd.read_excel("data.xlsx")
    # print(df)
    today = datetime.datetime.now().strftime("%d-%m")
    yearNow = datetime.datetime.now().strftime("%Y")
    # print(type(today))
    writeInd = []
    for index, item in df.iterrows():
        # print(index, item['Birthday'])
        bday = item['Birthday'].strftime("%d-%m")
        # print(bday) 
        if(today == bday) and yearNow not in str(item['Year']):
            
            sendEmail(item['Email'], "Happy Birthday", item['Dialogue']) 
            writeInd.append(index)

    # print(writeInd)
    for i in writeInd:
        yr = df.loc[i, 'Year']
        df.loc[i, 'Year'] = str(yr) + ', ' + str(yearNow)
        # print(df.loc[i, 'Year'])

    # print(df) 
    df.to_excel('data.xlsx', index=False)   
    

Post a Comment

Previous Post Next Post