-
Notifications
You must be signed in to change notification settings - Fork 2
/
sendmail.py
29 lines (22 loc) · 871 Bytes
/
sendmail.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
import smtplib
import ssl
from socket import gaierror
from aiosmtpd.smtp import Envelope
def send_mail(host: str, port: int, user: str, password: str, e: Envelope) -> str:
context = ssl.create_default_context()
try:
server = smtplib.SMTP(host, port)
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(user, password)
server.sendmail(user, e.rcpt_tos, e.content, e.mail_options, e.rcpt_options)
except (gaierror, ConnectionRefusedError):
return "421 Failed to connect to the server. Bad connection settings?"
except smtplib.SMTPAuthenticationError:
return "530 Failed to connect to the server. Wrong user/password?"
except smtplib.SMTPException as e:
return "554 SMTP error occurred: " + str(e)
finally:
server.quit()
return "250 OK"