Browse Source

send by postfix only

Son NK 5 years ago
parent
commit
6f93f419c2
5 changed files with 57 additions and 111 deletions
  1. 1 22
      app/auth/views/register.py
  2. 0 1
      app/config.py
  3. 3 21
      app/dashboard/views/setting.py
  4. 1 40
      app/developer/views/new_client.py
  5. 52 27
      app/email_utils.py

+ 1 - 22
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! <br><br>
-
-Our mission is to make the login process as smooth and as secure as possible. This should be easy. <br><br>
-
-To get started, we need to confirm your email address, so please click this <a href="{activation_link}">link</a> 
-to finish creating your account. Or you can paste this link into your browser: <br><br>
-
-{activation_link} <br><br>
-
-Your feedbacks are very important to us. Please feel free to reply to this email to let us know any 
-of your suggestion! <br><br>
-
-Thanks! <br><br>
-
-SimpleLogin team.
-            
-            """,
-    )
+    email_utils.send_activation_email(user.email, user.name, activation_link)

+ 0 - 1
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"])
 
 

+ 3 - 21
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}! <br><br>
-
-    To reset or change your password, please follow this link <a href="{reset_password_link}">reset password</a>. 
-    Or you can paste this link into your browser: <br><br>
-
-    {reset_password_link} <br><br>
-
-    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",

+ 1 - 40
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}! <br><br>
-
-This is Son, SimpleLogin CEO & Founder :) <br><br>
-
-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. <br><br>
-
-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.
-<br><br>
-
-And ok, this is an automated email, but if you reply it comes directly to me and will be answered by me.
-<br><br>
-
-Best regards, <br>
-Son. <br>
-<br>
-----------------------------------<br>
-Son NK <br>
-SimpleLogin founder. <br>
-https://simplelogin.io <br>
-https://twitter.com/nguyenkims <br>
-                             """,
-                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)

+ 52 - 27
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),
+    )
+
+
+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),
     )
 
-    sg = SendGridAPIClient(SENDGRID_API_KEY)
-    response = sg.send(message)
-    LOG.d("sendgrid res:%s, email:%s", response.status_code, to_email)
 
+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_by_postfix(to_email, subject, content):
+
+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)