diff --git a/apps/.DS_Store b/apps/.DS_Store new file mode 100644 index 0000000..dc1d597 Binary files /dev/null and b/apps/.DS_Store differ diff --git a/apps/pushnotification/constants.py b/apps/pushnotification/constants.py new file mode 100644 index 0000000..4623b0c --- /dev/null +++ b/apps/pushnotification/constants.py @@ -0,0 +1,7 @@ +from ...secret import MAILER_PW + +port = 465 # For starttls +smtp_server = "smtp.gmail.com" +sender_email = "noreply.ticketmasterbestseat@gmail.com" +subject = "Message from Ticketmaster Ticket Tracker" +app_password = MAILER_PW diff --git a/apps/pushnotification/smtp.py b/apps/pushnotification/smtp.py new file mode 100644 index 0000000..b07b527 --- /dev/null +++ b/apps/pushnotification/smtp.py @@ -0,0 +1,41 @@ +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 auth_server(server: SMTP_SSL): + server_login(server, constants.app_password) + + +server = init_server() +# auth_server(server) + + +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], message: str): + try: + err = server_send_email(server, receiver_emails, message) + if err is not None: + raise Exception('could not send email to the receiver') + except Exception as ex: + print(ex) diff --git a/secret.py b/secret.py index ed02f63..e396b52 100644 --- a/secret.py +++ b/secret.py @@ -1,3 +1,4 @@ import os -CONN_SRV = os.environ.get('CONN_SRV') \ No newline at end of file +CONN_SRV = os.environ.get('CONN_SRV') +MAILER_PW = os.environ.get('MAILER_PW', '')