From f366e1c383f18056d78d7a56a3707e92daf1fd60 Mon Sep 17 00:00:00 2001 From: Son NK Date: Fri, 15 Nov 2019 14:11:51 +0100 Subject: [PATCH] add send_by_postfix, to distinct with send_by_sendgrid --- app/auth/views/register.py | 2 +- app/dashboard/views/index.py | 2 +- app/dashboard/views/setting.py | 2 +- app/developer/views/new_client.py | 2 +- app/email_utils.py | 27 ++++++++++++++++++--------- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/auth/views/register.py b/app/auth/views/register.py index 29e09bb7..3400fb18 100644 --- a/app/auth/views/register.py +++ b/app/auth/views/register.py @@ -66,7 +66,7 @@ def send_activation_email(user, next_url): LOG.d("redirect user to %s after activation", next_url) activation_link = activation_link + "&next=" + encode_url(next_url) - email_utils.send( + email_utils.send_by_sendgrid( user.email, f"Welcome to SimpleLogin {user.name} - just one more step!", html_content=f""" diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index b7519538..c32b9b68 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -27,7 +27,7 @@ def index(): gen_email = GenEmail.get(gen_email_id) LOG.d("trigger an email to %s", gen_email) - email_utils.send( + email_utils.send_by_sendgrid( gen_email.email, "A Test Email", f""" diff --git a/app/dashboard/views/setting.py b/app/dashboard/views/setting.py index 48b10591..ba041710 100644 --- a/app/dashboard/views/setting.py +++ b/app/dashboard/views/setting.py @@ -79,7 +79,7 @@ def send_reset_password_email(user): reset_password_link = f"{URL}/auth/reset_password?code={reset_password_code.code}" - email_utils.send( + email_utils.send_by_sendgrid( user.email, f"Reset your password on SimpleLogin", html_content=f""" diff --git a/app/developer/views/new_client.py b/app/developer/views/new_client.py index 99a6d825..f00d8f2f 100644 --- a/app/developer/views/new_client.py +++ b/app/developer/views/new_client.py @@ -38,7 +38,7 @@ app: {client.name} # if this is the first app user creates, sends an email to ask for feedback if db.session.query(Client).filter_by(user_id=current_user.id).count() == 1: LOG.d(f"send feedback email to user {current_user}") - email_utils.send( + email_utils.send_by_sendgrid( current_user.email, "SimpleLogin questions/feedbacks", f""" diff --git a/app/email_utils.py b/app/email_utils.py index feb5e9bb..92cb9ae6 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -1,5 +1,7 @@ # using SendGrid's Python Library # https://github.com/sendgrid/sendgrid-python +from email.message import EmailMessage +from smtplib import SMTP from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail @@ -8,7 +10,7 @@ from app.config import SUPPORT_EMAIL, SENDGRID_API_KEY, NOT_SEND_EMAIL from app.log import LOG -def send(to_email, subject, html_content, plain_content=None): +def send_by_sendgrid(to_email, subject, html_content, plain_content=None): # On local only print out email content if NOT_SEND_EMAIL: LOG.d( @@ -26,17 +28,24 @@ def send(to_email, subject, html_content, plain_content=None): html_content=html_content, plain_text_content=plain_content, ) + sg = SendGridAPIClient(SENDGRID_API_KEY) response = sg.send(message) LOG.d("sendgrid res:%s, email:%s", response.status_code, to_email) +def send_by_postfix(to_email, subject, content): + # host IP, setup via Docker network + smtp = SMTP("1.1.1.1", 25) + msg = EmailMessage() + + msg["Subject"] = subject + msg["From"] = SUPPORT_EMAIL + msg["To"] = to_email + msg.set_content(content) + + smtp.send_message(msg, from_addr=SUPPORT_EMAIL, to_addrs=[to_email]) + + def notify_admin(subject, html_content=""): - send( - SUPPORT_EMAIL, - subject, - f""" - - {html_content} - """, - ) + send_by_postfix(SUPPORT_EMAIL, subject, html_content)