From f8293bc61868314494830ced53ae69852a706097 Mon Sep 17 00:00:00 2001 From: Son NK Date: Mon, 16 Dec 2019 19:36:59 +0200 Subject: [PATCH] make ADMIN_EMAIL optional --- .env.example | 16 ++++++++++------ app/config.py | 2 +- app/email_utils.py | 26 ++++++++------------------ cron.py | 16 +++++++++++----- email_handler.py | 4 ++-- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.env.example b/.env.example index de90f3f9..b0d87dd4 100644 --- a/.env.example +++ b/.env.example @@ -4,18 +4,22 @@ URL=http://localhost:7777 # If you want to enable sentry for error tracking, put your sentry dsn here. # SENTRY_DSN=your_sentry_dsn -# apply colored log -COLOR_LOG=true +# apply colored log to facilitate local development +# COLOR_LOG=true # <<< Email related settings >>> -# Only print email content, not sending it -NOT_SEND_EMAIL=true +# Only print email content, not sending it, for local development +# NOT_SEND_EMAIL=true + # domain used to create alias EMAIL_DOMAIN=sl.local + # transactional email is sent from this email address SUPPORT_EMAIL=support@sl.local -# to send general stats -ADMIN_EMAIL=admin@sl.local + +# to receive general stats. +# ADMIN_EMAIL=admin@sl.local + # Max number emails user can generate for free plan MAX_NB_EMAIL_FREE_PLAN=3 # custom domain needs to point to these MX servers diff --git a/app/config.py b/app/config.py index dd6fd9a3..09fa3854 100644 --- a/app/config.py +++ b/app/config.py @@ -40,7 +40,7 @@ SENTRY_DSN = os.environ.get("SENTRY_DSN") NOT_SEND_EMAIL = "NOT_SEND_EMAIL" in os.environ EMAIL_DOMAIN = os.environ["EMAIL_DOMAIN"] SUPPORT_EMAIL = os.environ["SUPPORT_EMAIL"] -ADMIN_EMAIL = os.environ["ADMIN_EMAIL"] +ADMIN_EMAIL = os.environ.get("ADMIN_EMAIL") MAX_NB_EMAIL_FREE_PLAN = int(os.environ["MAX_NB_EMAIL_FREE_PLAN"]) # allow to override postfix server locally POSTFIX_SERVER = os.environ.get("POSTFIX_SERVER", "1.1.1.1") diff --git a/app/email_utils.py b/app/email_utils.py index 40dc5353..54fe9808 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -5,13 +5,7 @@ from smtplib import SMTP from jinja2 import Environment, FileSystemLoader -from app.config import ( - SUPPORT_EMAIL, - ROOT_DIR, - POSTFIX_SERVER, - ADMIN_EMAIL, - NOT_SEND_EMAIL, -) +from app.config import SUPPORT_EMAIL, ROOT_DIR, POSTFIX_SERVER, NOT_SEND_EMAIL from app.log import LOG @@ -25,7 +19,7 @@ def _render(template_name, **kwargs) -> str: def send_welcome_email(email, name): - send_by_postfix( + send_email( email, f"{name}, welcome to SimpleLogin!", _render("welcome.txt", name=name), @@ -34,7 +28,7 @@ def send_welcome_email(email, name): def send_activation_email(email, name, activation_link): - send_by_postfix( + send_email( email, f"{name}, just one more step to join SimpleLogin", _render( @@ -47,7 +41,7 @@ def send_activation_email(email, name, activation_link): def send_reset_password_email(email, name, reset_password_link): - send_by_postfix( + send_email( email, f"{name}, reset your password on SimpleLogin", _render( @@ -60,7 +54,7 @@ def send_reset_password_email(email, name, reset_password_link): def send_change_email(new_email, current_email, name, link): - send_by_postfix( + send_email( new_email, f"{name}, confirm email update on SimpleLogin", _render( @@ -81,7 +75,7 @@ def send_change_email(new_email, current_email, name, link): def send_new_app_email(email, name): - send_by_postfix( + send_email( email, f"{name}, any questions/feedbacks for SimpleLogin?", _render("new-app.txt", name=name), @@ -90,7 +84,7 @@ def send_new_app_email(email, name): def send_test_email_alias(email, name): - send_by_postfix( + send_email( email, f"{name}, this email is sent to {email}", _render("test-email.txt", name=name, alias=email), @@ -98,7 +92,7 @@ def send_test_email_alias(email, name): ) -def send_by_postfix(to_email, subject, plaintext, html): +def send_email(to_email, subject, plaintext, html): if NOT_SEND_EMAIL: LOG.d( "send email with subject %s to %s, plaintext: %s, html:%s", @@ -132,10 +126,6 @@ def send_by_postfix(to_email, subject, plaintext, html): smtp.send_message(msg, from_addr=SUPPORT_EMAIL, to_addrs=[to_email]) -def notify_admin(subject, html_content=""): - send_by_postfix(ADMIN_EMAIL, subject, html_content, html_content) - - def get_email_name(email_from): """parse email from header and return the name part First Last -> First Last diff --git a/cron.py b/cron.py index 501e0ad2..ae4f8389 100644 --- a/cron.py +++ b/cron.py @@ -1,7 +1,7 @@ import arrow -from app import email_utils -from app.config import IGNORED_EMAILS +from app.config import IGNORED_EMAILS, ADMIN_EMAIL +from app.email_utils import send_email from app.extensions import db from app.log import LOG from app.models import ( @@ -18,6 +18,10 @@ from server import create_app def stats(): """send admin stats everyday""" + if not ADMIN_EMAIL: + # nothing to do + return + # nb user q = User.query for ie in IGNORED_EMAILS: @@ -68,9 +72,11 @@ def stats(): today = arrow.now().format() - email_utils.notify_admin( - f"SimpleLogin Stats for {today}", - f""" + send_email( + ADMIN_EMAIL, + subject=f"SimpleLogin Stats for {today}", + plaintext="", + html=f""" Stats for {today}
nb_user: {nb_user}
diff --git a/email_handler.py b/email_handler.py index 522b4ceb..9fe7f71d 100644 --- a/email_handler.py +++ b/email_handler.py @@ -39,7 +39,7 @@ from smtplib import SMTP from aiosmtpd.controller import Controller from app.config import EMAIL_DOMAIN, POSTFIX_SERVER, URL -from app.email_utils import get_email_name, get_email_part, send_by_postfix +from app.email_utils import get_email_name, get_email_part, send_email from app.extensions import db from app.log import LOG from app.models import GenEmail, ForwardEmail, ForwardEmailLog @@ -223,7 +223,7 @@ class MailHandler: user_email, ) - send_by_postfix( + send_email( envelope.mail_from, f"Your email ({envelope.mail_from}) is not allowed to send email to {reply_email}", "",