-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmail.py
57 lines (33 loc) · 1.17 KB
/
Email.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
import smtplib as sm
#for having hidden password
from getpass import getpass
#imports related to structured emails
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
FROM_EMAIL = "[email protected]"
PASSWORD = getpass()
TO_EMAILS = ["[email protected]","[email protected]"]
#create connection with host
server = sm.SMTP(host = "smtp.gmail.com", port = 587)
print("CONNECTION CREATED WITH GMAIL")
#Enable security layers
server.starttls()
print("SECURITY LAYERS ENABLED")
#Login
server.login(FROM_EMAIL,PASSWORD)
print("LOGGED IN SUCCESSFULLY")
#compose email and send
for to in TO_EMAILS:
#constructing structured email
mail = MIMEMultipart()
mail['Subject'] = "Python testing email"
mail['To'] = to
msg = "this is testing email using python"
attachable_text = MIMEText(msg,"plain")
mail.attach(attachable_text)
sendable_email = mail.as_string()
server.sendmail(FROM_EMAIL,to,sendable_email)
print('EMAILS SENT')
#logout and destroy connection
server.quit()
print('LOGGED OUT AND KILLED THE CONNECTION')