Advertisement
mayankjoin3

Teams Marks Send to Students

Nov 21st, 2024 (edited)
320
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. EMAIL = "your_email@gmail.com"
  14. PASSWORD = "your_password"
  15.  
  16. # Function to send an email
  17. def send_email(to_email, subject, body):
  18.     try:
  19.         # Create the email
  20.         message = MIMEMultipart()
  21.         message["From"] = EMAIL
  22.         message["To"] = to_email
  23.         message["Subject"] = subject
  24.  
  25.         # Attach the email body
  26.         message.attach(MIMEText(body, "plain"))
  27.  
  28.         # Connect to the server and send the email
  29.         server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
  30.         server.starttls()
  31.         server.login(EMAIL, PASSWORD)
  32.         server.sendmail(EMAIL, to_email, message.as_string())
  33.         server.quit()
  34.         print(f"Email sent to {to_email}")
  35.     except Exception as e:
  36.         print(f"Failed to send email to {to_email}: {e}")
  37.  
  38. # Iterate through the rows of the DataFrame (skipping the header)
  39. for _, row in data.iterrows():
  40.     name = row["Name"]
  41.     total_points = row["Total points"]
  42.     to_email = row["Email"]
  43.  
  44.     # Email content
  45.     subject = "MidSem Marks"
  46.     body = f"""Dear {name},\n\nMidSem Marks Obtained are {total_points}/120.\n\nRegards,\nCourse Coordinator"""
  47.  
  48.     # Send the email
  49.     send_email(to_email, subject, body)
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement