Jul-04-2024, 11:41 AM
i am trying to write a function to send emails for reminders if todays date matches the dates saved on a csv file. i first go through the csv file, i add the correct reminders and create a list, i then fill that list with reminders that match todays date. i then go through that list in order to loop through all of the results and pop up a messagebox to confirm sending the email. the problem is i get every entry that matches twice in a row and i cant find the issue with my code. my code follows
def emails_func():
"""Check reminders for the current date and prompt to send notifications."""
search_results=[]
todays_date=datetime.now().strftime("%d-%m-%Y")
send_dates=[]
try:
df=pd.read_csv("data.csv")
except FileNotFoundError:
messagebox.showerror(title="OOOOOOOOps",message="You have no saved data.\nPlease save some info and search again!!\n")
else:
saved_data=df.to_dict("records")
for ent in saved_data:
if ent["recurring"]:
send_dates.extend(ent["send date"].split(","))
for ent in saved_data:
for date in send_dates:
current_date = "".join(letter for letter in date if letter.isalnum).replace("'", "").replace("[","").replace("]", "")
if todays_date == current_date:
print("Match found")
search_results.append({"name": ent["name"],"send date": current_date,"email address":ent["email address"],"phone number": ent["phone number"],"description":ent["description"]})
if len(search_results)>0:
for date in search_results:
full_name=date["name"].split(" ")
f_name=full_name[0].replace("'","").replace("]","").replace('[','').replace(']','')
l_name=full_name[1].replace("[","").replace("'","").replace('[','').replace(']','')
final_phone_number="+44"+str(date["phone number"]).replace("'","").replace("]","")[2:]
final_description=date["description"].replace("[","").replace("'","").replace("]","")
to_email_address=date["email address"].replace("[","").replace("'","").replace("'","").replace("]","")
if messagebox.askyesno(
title="Found Reminders",
message=f"On {get_due_date()}, {f_name} {l_name} will have to {final_description} "
f"They will be reminded on the {date['send date']}\n"
f"by contacting them on {to_email_address}\n"
f"Do you want to send this email now?\n "):
connection=smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user=my_email,password=password)
message_body=f"Hello {l_name}, {f_name} this is a friendly reminder that on {get_due_date()} you will have to {final_description}"
connection.sendmail(from_addr=my_email,to_addrs=to_email_address,msg=message_body)
else:
messagebox.showinfo(title="Looking for reminders", message="I have completed looking for reminders whose date matches todays date!\nI have come up with nothing!!\nSee you again tomorrow!!")please help me if you can.. thanks for your time
