-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemailService.py
57 lines (46 loc) · 1.59 KB
/
emailService.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
###########################################################
############ Create by ORASHAR on 08/05/2020 ############
###########################################################
import creds
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def formMessage(sender, receiver, PINCODE, centers_list):
msg = MIMEMultipart('alternative')
msg['Subject'] = "Vaccine Centers Availability"
msg['From'] = sender
msg['To'] = receiver[0]
text = f'Available centers for Pincode {PINCODE} are:'
html = """\
<html>
<head></head>
<body>
"""
i = 0
for center in centers_list:
i += 1
html += f"""\
<b>{i}-</b> <h3>{center['name']}</h3><h4> ({center['date']})</h4>><br/>
<p>Minimum Age : {center['min_age_limit']}<br/>Seats available - {center['available_capacity']}</p><br/><br/>
"""
html += """\
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
return msg
def sendEmail(centers_list, to, PINCODE):
sender = "[email protected]"
receiver = [to]
message = formMessage(sender, receiver, PINCODE, centers_list)
try:
smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtpObj.login(creds.sender_id, creds.sender_pass)
smtpObj.sendmail(sender, receiver, message.as_string())
print("Email sent to ", to)
smtpObj.quit()
except Exception as e:
print(e)