Advertisement
mayankjoin3

email python generic

Dec 2nd, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. import csv
  4.  
  5. # SMTP Configuration
  6. SMTP_SERVER = 'mail.iitp.ac.in'  # Replace with your SMTP server
  7. SMTP_PORT = 587  # Common SMTP port for TLS
  8. EMAIL_ADDRESS = 'email'  # Your email address
  9. EMAIL_PASSWORD = 'avcd'  # Your email password
  10.  
  11. def send_email(to_email, subject, body):
  12.     try:
  13.         # Create the email message
  14.         msg = MIMEText(body)
  15.         msg['Subject'] = subject
  16.         msg['From'] = EMAIL_ADDRESS
  17.         msg['To'] = to_email
  18.        
  19.         # Connect to the SMTP server and send the email
  20.         with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
  21.             server.starttls()  # Secure the connection
  22.             server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
  23.             server.sendmail(EMAIL_ADDRESS, to_email, msg.as_string())
  24.         print(f"Email sent to {to_email}")
  25.     except Exception as e:
  26.         print(f"Failed to send email to {to_email}: {e}")
  27.  
  28. def main():
  29.     # Read the CSV file
  30.     csv_file = 'Book1.csv'  # Replace with the actual CSV file path
  31.     with open(csv_file, newline='') as file:
  32.         reader = csv.DictReader(file)
  33.         for row in reader:
  34.             # Prepare email content
  35.             name = row['Name Roll'].split(' ', 1)[1]  # Extract full name
  36.             midsem = row['MidSem'].strip()
  37.             endsem = row['EndSem'].strip()
  38.             to_email = row['email'].strip()
  39.            
  40.             subject = "CN Marks MidSem Endsem"
  41.             body = f"""
  42. Dear {name},
  43.  
  44. Your CN marks are:
  45.  
  46. MidSem (Out of 150): {midsem}
  47. EndSem (Out of 150): {endsem}
  48.  
  49. Students whose marks got updated today are not reflected in this email. However, for final grading, the updated marks will be considered.
  50.  
  51. Best regards,
  52. Mayank Agarwal
  53. """
  54.             # Send the email
  55.             send_email(to_email, subject, body)
  56.  
  57. if __name__ == "__main__":
  58.     main()
  59.  
  60.  
  61. csv sample
  62.  
  63. #Name Roll,MidSem,EndSem,roll,email
  64. #2201AI01 ADIL CHADACHEEMADE,66,66,2201AI01,2201ai01_adil@iitp.ac.in
  65. #2201AI02 AKASH SINHA,88,127,2201AI02,2201ai02_akash@iitp.ac.in
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement