-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtp_server.py
36 lines (29 loc) · 1.15 KB
/
smtp_server.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
import smtplib, ssl
from getpass import getpass
from email.message import EmailMessage
from email.utils import formataddr
from fastapi import FastAPI
class SMTPClient:
def __init__(self, hostname: str, port: int, email: str, password: str):
context = ssl.create_default_context()
self.server = smtplib.SMTP_SSL(hostname, port, context=context)
self.server.login(email, password)
def send_message(self, subject: str, body: str, *, name: str, email: str,
to: str):
msg = EmailMessage()
msg['From'] = formataddr((name, email))
msg['To'] = to
msg['Subject'] = subject
msg.set_content(body)
self.server.send_message(msg)
app = FastAPI()
client = SMTPClient('mail.dhaownconstruction.com', 465,
'[email protected]', getpass())
receiver = '[email protected]'
print('Logged in')
@app.post('/contact')
async def contact(comment: str, name: str, email: str):
client.send_message('Contact', comment, name=name, email=email, to=receiver)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='localhost', port=8000)