Advertisement
mayankjoin3

Teams Marks Send to Students

Nov 21st, 2024 (edited)
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import pandas as pd
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from email.mime.multipart import MIMEMultipart
  5.  
  6. # Load the Excel file
  7. file_path = "marks.xlsx"
  8. data = pd.read_excel(file_path)
  9.  
  10. # Email credentials
  11. SMTP_SERVER = "smtp.gmail.com"  # Use the appropriate SMTP server
  12. SMTP_PORT = 587
  13. PASSWORD = "your_password"
  14.  
  15. # Function to send an email
  16. def send_email(to_email, subject, body):
  17.     try:
  18.         # Create the email
  19.         message = MIMEMultipart()
  20.         message["From"] = EMAIL
  21.         message["To"] = to_email
  22.         message["Subject"] = subject
  23.  
  24.         # Attach the email body
  25.         message.attach(MIMEText(body, "plain"))
  26.  
  27.         # Connect to the server and send the email
  28.         server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
  29.         server.starttls()
  30.         server.login(EMAIL, PASSWORD)
  31.         server.sendmail(EMAIL, to_email, message.as_string())
  32.         server.quit()
  33.         print(f"Email sent to {to_email}")
  34.     except Exception as e:
  35.         print(f"Failed to send email to {to_email}: {e}")
  36.  
  37. # Iterate through the rows of the DataFrame (skipping the header)
  38. for _, row in data.iterrows():
  39.     name = row["Name"]
  40.     total_points = row["Total points"]
  41.     to_email = row["Email"]
  42.  
  43.     # Email content
  44.     subject = "MidSem Marks"
  45.     body = f"""Dear {name},\n\nMidSem Marks Obtained are {total_points}/120.\n\nRegards,\nCourse Coordinator"""
  46.  
  47.     # Send the email
  48.     send_email(to_email, subject, body)
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement