forked from Jackiebibili/ticket_tracker_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmtp.py
More file actions
45 lines (32 loc) · 1.2 KB
/
smtp.py
File metadata and controls
45 lines (32 loc) · 1.2 KB
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
from smtplib import SMTP_SSL
from ssl import create_default_context
from email.message import EmailMessage
from . import constants
def init_server():
context = create_default_context()
server = SMTP_SSL(constants.smtp_server, constants.port, context=context)
return server
def server_login(server: SMTP_SSL, password: str):
return server.login(constants.sender_email, password)
def server_send_email(server: SMTP_SSL, receiver_emails: list[str], message: str):
em = EmailMessage()
em['From'] = constants.sender_email
em['To'] = receiver_emails
em['subject'] = constants.subject
em.set_content(message)
return server.sendmail(constants.sender_email, receiver_emails, em.as_string())
def send_email(receiver_emails: list[str], messages: list[str]):
global server
if len(messages) == 0:
return
# print(receiver_emails, messages[0])
try:
err = server_send_email(server, receiver_emails, messages[0])
if err is not None:
raise Exception('could not send email to the receiver')
except Exception as ex:
print(ex)
server = init_server()
def auth_server():
global server
server_login(server, constants.app_password)