Skip to content

Add files via upload #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions gmail_birthday_sender/birthdays.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Email,Name,Date of Birth
[email protected],Madeeha Hanif,2004-07-08
[email protected],Sarah Kathuria,2004-03-13
[email protected],Anna Khan,2004-09-12
[email protected],Arissa Latif,2005-06-11
[email protected],Alisha Saeed,2004-06-10
[email protected],Ayan Sanaulla,2006-09-08
[email protected],Adam Shaikh,2006-11-02
[email protected],Hassan Tariq,2003-09-19


94 changes: 94 additions & 0 deletions gmail_birthday_sender/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import csv
from datetime import datetime
# File path for the CSV "Contact Book"
contact_book = 'birthdays.csv'

# prompt the user for their/sender information
while True:
try:
sender_eaddress = input("Enter your email address: ").strip()
if not sender_eaddress.endswith("gmail.com") or sender_eaddress == "gmail.com":
raise ValueError("Error:")
# user email must contain a name before 'gmail.com' and /gmail.com"
# print(f"Email '{sender_eaddress}' is valid.") # debug message
break # exit loop, email address shows valid input

except ValueError as e:
print(f"{e} Invalid Email! Please try again.")

# if the email is valid, the program continues + password input
sender_password = input("Enter your password: ")

# to read emails from the CSV file (user's contact book)
def read_address():
recipients = []
try:
with open(contact_book, 'r', newline='', encoding='ascii') as file:
contacts = csv.DictReader(file) # using DictReader to access columns per contact row
for contact in contacts:
email = contact.get('Email', '').strip()
name = contact.get('Name', '').strip()
dob = contact.get('Date of Birth', '').strip()

# to skip empty or incorrectly formatted contacts
if not email or not name or not dob:
continue

try:
dob_object = datetime.strptime(dob, '%Y-%m-%d').date()
recipients.append({'Email': email, 'Name': name, 'Date of Birth': dob_object})
except ValueError:
print(f"Skip {name} ({email}): Invalid date format '{dob}', expected YYYY-MM-DD.")

except FileNotFoundError:
print(f"Error: The file '{contact_book}' was not found.")
except Exception as e:
print(f"{e}.There are no contacts in your contact book.")
# print(f"An unexpected error occurred: {e}") debug message
return recipients


# Function to display recipients and allow user to select one
def select_recipient(recipients):
if not recipients:
print("No valid recipients found in your contact book.")
return None

print("\nList of addresses from the Mailing List:")
for i, recipient in enumerate(recipients, start=1):
print(f"{i}. {recipient['Name']} ({recipient['Email']}) Birthday: {recipient['Date of Birth']}") # dont adjust!

while True:
try:
choice = int(input("\nSelect a contact in your list. Enter the according number: ")) - 1
if 0 <= choice < len(recipients):
return recipients[choice]
else:
print("Invalid contact. Please enter a valid number.")
except ValueError:
print("Invalid input. Please enter a number.")


# Read CSV/ contact book and select recipient
print("Reading Mailing List...")
recipients = read_address()
print(f"You have {len(recipients)} addresses.")

selected_recipient = select_recipient(recipients)

if selected_recipient:
recipient_eaddress = selected_recipient['Email']
recipient_name = selected_recipient['Name']

# Get custom message input
birthday_message = input(f"Enter birthday message for {recipient_name}: ")
print(f"\n--- Ready to Send ---")
print(f"From: {sender_eaddress}")
print(f"To: {recipient_eaddress}")
print(f"Subject: Happy Birthday {recipient_name}!")
print(f"\n{birthday_message}")
print("\n--- End of Message ---")

else:
print("No recipient selected.")
# ends program