-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.py
90 lines (70 loc) · 2.62 KB
/
mailer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from tornado.httpclient import AsyncHTTPClient
from urllib.parse import urlencode
from tornado.httpclient import HTTPRequest
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import os
import json
server = None # Initializing global email server object to prevent regional login requests
DESTINATION_EMAIL = os.environ["TOSITE"]
SOURCE_EMAIL = os.environ["FROMSITE"]
CAPTCHA_SECRET = os.environ["CAPTCHA_SECRET"]
try:
password = os.environ["PASSWORD"]
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(SOURCE_EMAIL, password)
print("Successful Login")
except:
print("Mail Server Non-Functional at the moment. Unable to log in.")
server = None
async def validate_captcha(recieved_captcha):
try:
http_client = AsyncHTTPClient()
data = {
'secret': CAPTCHA_SECRET,
'response': recieved_captcha
}
request = HTTPRequest("https://www.google.com/recaptcha/api/siteverify", "POST", body=urlencode(data))
x = await http_client.fetch(request)
captcha_status = json.loads(x.body)
if captcha_status['success'] != True:
return False
return True
except:
return False
def compose_email(name: str, from_addr: str, message: str, phone_number: str):
toaddr = DESTINATION_EMAIL
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = toaddr
msg['Subject'] = "Contact from my website"
body = f"""
Name: {name}
Phone Number: {phone_number}
From: {from_addr}
Message:
{message}
"""
msg.attach(MIMEText(body, 'plain'))
return msg
async def issue_message(name, from_addr, phone_number, message, captcha) -> (bool, str):
global server
captcha_validation = await validate_captcha(captcha)
if not captcha_validation:
return False, "This message has been flagged as coming from a bot account please try through other means!"
else:
try:
message = compose_email(name, from_addr, message, phone_number)
if not server:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(SOURCE_EMAIL, password)
text = message.as_string()
server.sendmail(from_addr, DESTINATION_EMAIL, text)
return True, "Success!\nYou should receive a response shortly"
except:
return False, "Unfortunately, the mail server is down. Please contact Sharabesh directly through sharabesh (at) berkeley.edu"