File size: 1,562 Bytes
26b5643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import smtplib, ssl
import datetime

def send_mails(ids):
    all_mails = [f"cse22000100{i}" for i in range (1, 10)] + [f"cse2200010{i}" for i in range (10, 84)]
    port = 587  # For starttls
    smtp_server = "smtp.gmail.com"
    sender_email = "contactkhelogames@gmail.com"
    password = open("password.txt", "r").read()
    # receiver_email = "cse210001083@iiti.ac.in"
    message_present = f"""\
    Subject: Attendace for {datetime.date.today()}
    Your attendance for the class has been marked present."""
    message_absent = f"""\
    Subject: Attendace for {datetime.date.today()}
    Your attendance for the class has been marked absent."""
    for receiver_email in ids:
        context = ssl.create_default_context()
        with smtplib.SMTP(smtp_server, port) as server:
            server.ehlo() 
            server.starttls(context=context)
            server.ehlo() 
            server.login(sender_email, password)
            server.sendmail(sender_email, receiver_email, message_present) 
            print(f"Mail sent to {receiver_email}")
        all_mails.remove(receiver_email)

    for receiver_email in all_mails:
        context = ssl.create_default_context()
        with smtplib.SMTP(smtp_server, port) as server:
            server.ehlo() 
            server.starttls(context=context)
            server.ehlo() 
            server.login(sender_email, password)
            server.sendmail(sender_email, receiver_email, message_absent) 
            print(f"Mail sent to {receiver_email}")
    print("All mails sent successfully")