Using SMTP and Docker environment variables for configuration

This commit is contained in:
Heiko Joerg Schick
2023-09-08 09:42:52 +02:00
parent 696d81f49a
commit 4c5fbe4786
2 changed files with 45 additions and 20 deletions
+4
View File
@@ -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 \
+41 -20
View File
@@ -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 {
<body>
<br><br>
<div>Hi! Here are your <a href="https://arxiv-sanity-lite.com">arxiv-sanity-lite</a> recommendations. __STATS__</div>
<div>Hi! Here are your <a href="https://arxiv.h3132.de">arxiv-sanity-lite</a> recommendations. __STATS__</div>
<br><br>
<div>
@@ -69,7 +70,7 @@ body {
<br><br>
<div>
To stop these emails remove your email in your <a href="https://arxiv-sanity-lite.com/profile">account</a> settings. (your account is __ACCOUNT__).
To stop these emails remove your email in your <a href="https://arxiv.h3132.de/profile">account</a> settings. (your account is __ACCOUNT__).
</div>
<div> <3, arxiv-sanity-lite. </div>
@@ -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(
"""
<tr>
@@ -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)
# -----------------------------------------------------------------------------