Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import smtplib
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- # Load the Excel file
- file_path = "marks.xlsx"
- data = pd.read_excel(file_path)
- # Email credentials
- SMTP_SERVER = "smtp.gmail.com" # Use the appropriate SMTP server
- SMTP_PORT = 587
- EMAIL = "your_email@gmail.com"
- PASSWORD = "your_password"
- # Function to send an email
- def send_email(to_email, subject, body):
- try:
- # Create the email
- message = MIMEMultipart()
- message["From"] = EMAIL
- message["To"] = to_email
- message["Subject"] = subject
- # Attach the email body
- message.attach(MIMEText(body, "plain"))
- # Connect to the server and send the email
- server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
- server.starttls()
- server.login(EMAIL, PASSWORD)
- server.sendmail(EMAIL, to_email, message.as_string())
- server.quit()
- print(f"Email sent to {to_email}")
- except Exception as e:
- print(f"Failed to send email to {to_email}: {e}")
- # Iterate through the rows of the DataFrame (skipping the header)
- for _, row in data.iterrows():
- name = row["Name"]
- total_points = row["Total points"]
- to_email = row["Email"]
- # Email content
- subject = "MidSem Marks"
- body = f"""Dear {name},\n\nMidSem Marks Obtained are {total_points}/120.\n\nRegards,\nCourse Coordinator"""
- # Send the email
- send_email(to_email, subject, body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement