|
| 1 | +import csv |
| 2 | +import smtplib, ssl |
| 3 | + |
| 4 | +message="""Hi {fname}, Wish you a very Happy birthday. |
| 5 | +Hope you had a great time.""" |
| 6 | + |
| 7 | +sender_address="forpythonbirthdayproject@gmail.com" |
| 8 | +sender_password="qwerty@12345" |
| 9 | + |
| 10 | +context = ssl.create_default_context() |
| 11 | +with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: |
| 12 | + server.login(sender_address,sender_password) |
| 13 | + |
| 14 | + with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv") as file: |
| 15 | + reader=csv.reader(file) |
| 16 | + next(reader) |
| 17 | + for fname,lname,email,dob in reader: |
| 18 | + server.sendmail( |
| 19 | + sender_address, |
| 20 | + email, |
| 21 | + message.format(fname=fname), |
| 22 | + ) |
| 23 | + |
| 24 | +add=input( |
| 25 | + """Do you wish to add or remove names to the csv file? |
| 26 | + if you want add names type add |
| 27 | + otherwise if you want remove names type remove |
| 28 | + if you wish to exit, type exit |
| 29 | + """ |
| 30 | +) |
| 31 | + |
| 32 | +if add=="add": |
| 33 | + new_data=input("Enter data as first name,lastname,email,date of birth: ") |
| 34 | + new_data=new_data.split(",") |
| 35 | + with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv",'r+') as file: |
| 36 | + writer_object=csv.writer(file) |
| 37 | + next(file) |
| 38 | + writer_object.writerow(new_data) |
| 39 | +elif add=="remove": |
| 40 | + lines=[] |
| 41 | + removal=input("Enter the first name of the person to be removed: ") |
| 42 | + with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv",'r') as file: |
| 43 | + reader=csv.reader(file) |
| 44 | + for row in reader: |
| 45 | + lines.append(row) |
| 46 | + for fields in row: |
| 47 | + if fields==removal: |
| 48 | + lines.remove(row) |
| 49 | + with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv",'w',newline="") as file: |
| 50 | + writer=csv.writer(file) |
| 51 | + writer.writerows(lines) |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
0 commit comments