From 6f93f419c26b819b2d3247b07971fc14559389c0 Mon Sep 17 00:00:00 2001 From: Son NK Date: Sat, 16 Nov 2019 15:19:43 +0100 Subject: [PATCH] send by postfix only --- app/auth/views/register.py | 23 +-------- app/config.py | 1 - app/dashboard/views/setting.py | 24 ++-------- app/developer/views/new_client.py | 41 +--------------- app/email_utils.py | 79 ++++++++++++++++++++----------- 5 files changed, 57 insertions(+), 111 deletions(-) diff --git a/app/auth/views/register.py b/app/auth/views/register.py index 3400fb18..a78ae354 100644 --- a/app/auth/views/register.py +++ b/app/auth/views/register.py @@ -66,25 +66,4 @@ 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_by_sendgrid( - user.email, - f"Welcome to SimpleLogin {user.name} - just one more step!", - html_content=f""" - Welcome to SimpleLogin!

- -Our mission is to make the login process as smooth and as secure as possible. This should be easy.

- -To get started, we need to confirm your email address, so please click this link -to finish creating your account. Or you can paste this link into your browser:

- -{activation_link}

- -Your feedbacks are very important to us. Please feel free to reply to this email to let us know any -of your suggestion!

- -Thanks!

- -SimpleLogin team. - - """, - ) + email_utils.send_activation_email(user.email, user.name, activation_link) diff --git a/app/config.py b/app/config.py index e713a8d5..4bba23f5 100644 --- a/app/config.py +++ b/app/config.py @@ -39,7 +39,6 @@ ENABLE_SENTRY = "ENABLE_SENTRY" in os.environ NOT_SEND_EMAIL = "NOT_SEND_EMAIL" in os.environ EMAIL_DOMAIN = os.environ["EMAIL_DOMAIN"] SUPPORT_EMAIL = os.environ["SUPPORT_EMAIL"] -SENDGRID_API_KEY = os.environ["SENDGRID_API_KEY"] MAX_NB_EMAIL_FREE_PLAN = int(os.environ["MAX_NB_EMAIL_FREE_PLAN"]) diff --git a/app/dashboard/views/setting.py b/app/dashboard/views/setting.py index ba041710..a15844df 100644 --- a/app/dashboard/views/setting.py +++ b/app/dashboard/views/setting.py @@ -1,7 +1,5 @@ from io import BytesIO -import arrow -import stripe from flask import render_template, request, redirect, url_for, flash from flask_login import login_required, current_user from flask_wtf import FlaskForm @@ -9,9 +7,8 @@ from flask_wtf.file import FileField from wtforms import StringField, validators from app import s3, email_utils -from app.config import URL, PROMO_CODE +from app.config import URL from app.dashboard.base import dashboard_bp -from app.email_utils import notify_admin from app.extensions import db from app.log import LOG from app.models import PlanEnum, File, ResetPasswordCode @@ -55,10 +52,9 @@ def setting(): db.session.commit() flash(f"Your profile has been updated", "success") - + elif request.form.get("form-name") == "change-password": send_reset_password_email(current_user) - return redirect(url_for("dashboard.setting")) @@ -79,21 +75,7 @@ def send_reset_password_email(user): reset_password_link = f"{URL}/auth/reset_password?code={reset_password_code.code}" - email_utils.send_by_sendgrid( - user.email, - f"Reset your password on SimpleLogin", - html_content=f""" - Hi {user.name}!

- - To reset or change your password, please follow this link reset password. - Or you can paste this link into your browser:

- - {reset_password_link}

- - Cheers, - SimpleLogin team. - """, - ) + email_utils.send_reset_password_email(user.email, user.name, reset_password_link) flash( "You are going to receive an email containing instruction to change your password", diff --git a/app/developer/views/new_client.py b/app/developer/views/new_client.py index f00d8f2f..ed8f55e9 100644 --- a/app/developer/views/new_client.py +++ b/app/developer/views/new_client.py @@ -38,46 +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_by_sendgrid( - current_user.email, - "SimpleLogin questions/feedbacks", - f""" -Hi {current_user.name}!

- -This is Son, SimpleLogin CEO & Founder :)

- -Even though I lead the company, Iā€™m the "product person" and the user experience you get from our product means a lot to me.

- -Our users and developers love SimpleLogin and its simplicity (hence the "simple" in the name šŸ˜‰), but if there's anything that's bugging you, even the smallest of issues that could be done better, I want to hear about it - so hit the reply button. -

- -And ok, this is an automated email, but if you reply it comes directly to me and will be answered by me. -

- -Best regards,
-Son.
-
-----------------------------------
-Son NK
-SimpleLogin founder.
-https://simplelogin.io
-https://twitter.com/nguyenkims
- """, - plain_content=""" - Hi there! - -This is Son, SimpleLogin CEO & Founder :). - -Even though I lead the company, Iā€™m the "product person" and the user experience you get from our product means a lot to me. - -Our users and developers love SimpleLogin and its simplicity (hence the "simple" in the name šŸ˜‰), but if there's anything that's bugging you, even the smallest of issues that could be done better, I want to hear about it - so hit the reply button. - -And ok, this is an automated email, but if you reply it comes directly to me and will be answered by me. - -Best regards, -Son. -""", - ) + email_utils.send_new_app_email(current_user.email, current_user.name) return redirect( url_for("developer.client_detail", client_id=client.id, is_new=1) diff --git a/app/email_utils.py b/app/email_utils.py index 401ce29c..a40f3ac9 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -1,40 +1,62 @@ -# using SendGrid's Python Library -# https://github.com/sendgrid/sendgrid-python +import os from email.message import EmailMessage from smtplib import SMTP -from sendgrid import SendGridAPIClient -from sendgrid.helpers.mail import Mail +from jinja2 import Environment, FileSystemLoader -from app.config import SUPPORT_EMAIL, SENDGRID_API_KEY, NOT_SEND_EMAIL -from app.log import LOG +from app.config import SUPPORT_EMAIL, ROOT_DIR -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( - "send mail to %s, subject:%s, content:%s", to_email, subject, html_content - ) - return +def _render(template_name, **kwargs) -> str: + templates_dir = os.path.join(ROOT_DIR, "templates", "emails") + env = Environment(loader=FileSystemLoader(templates_dir)) - if not plain_content: - plain_content = subject + template = env.get_template(template_name) - message = Mail( - from_email=SUPPORT_EMAIL, - to_emails=to_email, - subject=subject, - html_content=html_content, - plain_text_content=plain_content, + return template.render(**kwargs) + + +def send_welcome_email(email, name): + send_by_postfix( + email, + f"{name}, welcome to SimpleLogin!", + _render("welcome.txt", name=name), + _render("welcome.html", name=name), ) - sg = SendGridAPIClient(SENDGRID_API_KEY) - response = sg.send(message) - LOG.d("sendgrid res:%s, email:%s", response.status_code, to_email) + +def send_activation_email(email, name, activation_link): + send_by_postfix( + email, + f"{name}, just one more step to join SimpleLogin", + _render("activation.txt", name=name, activation_link=activation_link), + _render("activation.html", name=name, activation_link=activation_link), + ) -def send_by_postfix(to_email, subject, content): +def send_reset_password_email(email, name, reset_password_link): + send_by_postfix( + email, + f"{name}, reset your password on SimpleLogin", + _render( + "reset-password.txt", name=name, reset_password_link=reset_password_link + ), + _render( + "reset-password.html", name=name, reset_password_link=reset_password_link + ), + ) + + +def send_new_app_email(email, name): + send_by_postfix( + email, + f"{name}, any questions/feedbacks for SimpleLogin?", + _render("new-app.txt", name=name), + _render("new-app.html", name=name), + ) + + +def send_by_postfix(to_email, subject, plaintext, html): # host IP, setup via Docker network smtp = SMTP("1.1.1.1", 25) msg = EmailMessage() @@ -42,10 +64,13 @@ def send_by_postfix(to_email, subject, content): msg["Subject"] = subject msg["From"] = f"Son from SimpleLogin <{SUPPORT_EMAIL}>" msg["To"] = to_email - msg.set_content(content) + + msg.set_content(plaintext) + if html is not None: + msg.add_alternative(html, subtype="html") smtp.send_message(msg, from_addr=SUPPORT_EMAIL, to_addrs=[to_email]) def notify_admin(subject, html_content=""): - send_by_postfix(SUPPORT_EMAIL, subject, html_content) + send_by_postfix(SUPPORT_EMAIL, subject, html_content, html_content)