-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.py
56 lines (40 loc) · 1.26 KB
/
emails.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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(email, code):
FROM_ADDR = "ChangeMe"
FROM_PASSWD = "ChangeMe"
Subject = "OTP for Chat App"
Body = 'Here is the OTP for your login on Python chat app: ' + code
send(FROM_ADDR, FROM_PASSWD, email, Subject, Body)
def send(fromaddr, frompasswd, toaddr, msg_subject, msg_body):
try:
msg = MIMEMultipart()
except:
print("[-] Error in Creating Message Object")
return
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = msg_subject
body = msg_body
msg.attach(MIMEText(body, 'plain'))
try:
# s = smtplib.SMTP('smtp.gmail.com', 587)
s = smtplib.SMTP('stud.iitp.ac.in', 587) # Host used is for stud.iitp.ac.in accounts
print("[+] SMTP Session Created")
except:
print("[-] Error in creating SMTP session")
return
s.starttls()
try:
s.login(fromaddr, frompasswd)
print("[+] Login Successful")
except:
print("[-] Login Failed")
text = msg.as_string()
try:
s.sendmail(fromaddr, toaddr, text)
print("[+] Mail Sent successfully")
except:
print('[-] Mail not sent')
s.quit()