Browse Source

add send_by_postfix, to distinct with send_by_sendgrid

Son NK 5 years ago
parent
commit
f366e1c383

+ 1 - 1
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"""

+ 1 - 1
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"""

+ 1 - 1
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"""

+ 1 - 1
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"""

+ 18 - 9
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><body>
-    {html_content}
-    </body></html>""",
-    )
+    send_by_postfix(SUPPORT_EMAIL, subject, html_content)