Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Automated software testing with Python
Next article icon

Are you bored with sending birthday wishes to your friends 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 by writing a Python script.

The first thing we do is import six libraries: 

Apart from this, Also create an Excel sheet for containing records like this:Name,Email,Contact,Birthday,andYear

Approach:

  • For the sending email part, 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 onwww.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 then We create asendsms() function which will verify the API key and send SMS.
  • In the driver code section, we read the data from Excel sheet and match today’s date with any of the birthdays. If there is a match, we call thesendEmail() andsendsms() functions and also we add the current year in the Excel sheet. Also, we have usedToastNotifierfromwin10toast library to show desktop notifications once the e-mail and SMS has been sent successfully.

Below is the implementation:

Python
# import required packagesimportpandasaspdimportdatetimeimportsmtplibimporttimeimportrequestsfromwin10toastimportToastNotifier# your gmail credentials hereGMAIL_ID='your_email_here'GMAIL_PWD='your_password_here'# for desktop notificationtoast=ToastNotifier()# define a function for sending emaildefsendEmail(to,sub,msg):# connection to gmailgmail_obj=smtplib.SMTP('smtp.gmail.com',587)# starting the sessiongmail_obj.starttls()# login using credentialsgmail_obj.login(GMAIL_ID,GMAIL_PWD)# sending emailgmail_obj.sendmail(GMAIL_ID,to,f"Subject :{sub}\n\n{msg}")# quit the sessiongmail_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)whiletoast.notification_active():time.sleep(0.1)# define a function for sending smsdefsendsms(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)whiletoast.notification_active():time.sleep(0.1)# driver codeif__name__=="__main__":# read the excel sheet having all the detailsdataframe=pd.read_excel("excelsheet.xlsx")# today date in format : DD-MMtoday=datetime.datetime.now().strftime("%d-%m")# current year in format : YYyearNow=datetime.datetime.now().strftime("%Y")# writeindex listwriteInd=[]forindex,itemindataframe.iterrows():msg="Many Many Happy Returns of the day dear "+str(item['NAME'])# stripping the birthday in excel# sheet as : DD-MMbday=item['Birthday'].strftime("%d-%m")# condition checkingif(today==bday)andyearNownotinstr(item['Year']):# calling the sendEmail functionsendEmail(item['Email'],"Happy Birthday",msg)# calling the sendsms functionsendsms(item['Contact'],msg,item['NAME'],"Happy Birthday")writeInd.append(index)foriinwriteInd:yr=dataframe.loc[i,'Year']# this will record the years in which# email has been sentdataframe.loc[i,'Year']=str(yr)+','+str(yearNow)dataframe.to_excel('excelsheet.xlsx',index=False)

Automatic Birthday Mail Sending using Python
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp