From 4c5fbe4786535429b839db34d647fbb647024662 Mon Sep 17 00:00:00 2001 From: Heiko Joerg Schick Date: Fri, 8 Sep 2023 09:42:52 +0200 Subject: [PATCH] Using SMTP and Docker environment variables for configuration --- Dockerfile | 4 ++++ send_emails.py | 61 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6ec9cee..9f28d9f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,10 @@ ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 ENV ARXIV_QUERY="" +ENV SMTP_ADDRESS="" +ENV SMTP_PORT="" +ENV SMTP_USER_NAME="" +ENV SMTP_PASSWORD="" # Install required binary packages. RUN apt-get update && apt-get install -y \ diff --git a/send_emails.py b/send_emails.py index b0d8ee2..aae535a 100644 --- a/send_emails.py +++ b/send_emails.py @@ -5,8 +5,8 @@ I run this script in a cron job to send out emails to the users with their recommendations. There's a bit of copy paste code here but I expect that the recommendations may become more complex in the future, so this is ok for now. -You'll notice that the file sendgrid_api_key.txt is not in the repo, you'd have -to manually register with sendgrid yourself, get an API key and put it in the file. +You'll notice that the smtp password is not in the repo, you'd have +to manually register with smtp yourself. """ import os @@ -17,8 +17,9 @@ import argparse import numpy as np from sklearn import svm -import sendgrid -from sendgrid.helpers.mail import Email, To, Content, Mail +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText from aslite.db import load_features from aslite.db import get_tags_db @@ -60,7 +61,7 @@ body {

-
Hi! Here are your arxiv-sanity-lite recommendations. __STATS__
+
Hi! Here are your arxiv-sanity-lite recommendations. __STATS__


@@ -69,7 +70,7 @@ body {

-To stop these emails remove your email in your account settings. (your account is __ACCOUNT__). +To stop these emails remove your email in your account settings. (your account is __ACCOUNT__).
<3, arxiv-sanity-lite.
@@ -159,7 +160,7 @@ def render_recommendations(user, tags, tag_pids, tag_scores): if len(summary) == 500: summary += '...' # create the url that will feature this paper on top and also show the most similar papers - url = 'https://arxiv-sanity-lite.com/?rank=pid&pid=' + pid + url = 'https://arxiv.h3132.de/?rank=pid&pid=' + pid parts.append( """ @@ -196,27 +197,47 @@ def render_recommendations(user, tags, tag_pids, tag_scores): return out # ----------------------------------------------------------------------------- -# send the actual html via sendgrid +# send the actual html via smtp def send_email(to, html): # init the api - assert os.path.isfile('sendgrid_api_key.txt') - api_key = open('sendgrid_api_key.txt', 'r').read().strip() - sg = sendgrid.SendGridAPIClient(api_key=api_key) + # assert os.path.isfile('sendgrid_api_key.txt') + # api_key = open('sendgrid_api_key.txt', 'r').read().strip() + # sg = sendgrid.SendGridAPIClient(api_key=api_key) + + # Setup the necessary details from environment variables + smtp_server = os.environ.get('SMTP_ADDRESS', '') + port = os.environ.get('SMTP_PORT', '') + sender = os.environ.get('SMTP_USER_NAME', '') + password = os.environ.get('SMTP_PASSWORD', '') + receiver = to + + # Create the message + msg = MIMEMultipart() + msg['Subject'] = tnow_str + " Arxiv Sanity Lite recommendations" + msg['From'] = sender + msg['To'] = to + msg_body = html + msg.attach(MIMEText(msg_body, 'html')) - # construct the email - from_email = Email("admin@arxiv-sanity-lite.com") - to_email = To(to) - subject = tnow_str + " Arxiv Sanity Lite recommendations" - content = Content("text/html", html) - mail = Mail(from_email, to_email, subject, content) # hope for the best :) if not args.dry_run: - response = sg.client.mail.send.post(request_body=mail.get()) - print(response.status_code) - pass + try: + # Connect to the server + server = smtplib.SMTP_SSL(smtp_server, port) + + # Login to the email server + server.login(sender, password) + + # Send the email + server.sendmail(sender, receiver, msg.as_string()) + + # Close the connection to the server + server.quit() + except Exception as e: + print('Something went wrong.', e) # -----------------------------------------------------------------------------