Using SMTP and Docker environment variables for configuration
This commit is contained in:
@@ -15,6 +15,10 @@ ENV PYTHONDONTWRITEBYTECODE=1
|
|||||||
ENV PYTHONUNBUFFERED=1
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
ENV ARXIV_QUERY=""
|
ENV ARXIV_QUERY=""
|
||||||
|
ENV SMTP_ADDRESS=""
|
||||||
|
ENV SMTP_PORT=""
|
||||||
|
ENV SMTP_USER_NAME=""
|
||||||
|
ENV SMTP_PASSWORD=""
|
||||||
|
|
||||||
# Install required binary packages.
|
# Install required binary packages.
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
|
|||||||
+41
-20
@@ -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
|
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.
|
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
|
You'll notice that the smtp password is not in the repo, you'd have
|
||||||
to manually register with sendgrid yourself, get an API key and put it in the file.
|
to manually register with smtp yourself.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@@ -17,8 +17,9 @@ import argparse
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn import svm
|
from sklearn import svm
|
||||||
|
|
||||||
import sendgrid
|
import smtplib
|
||||||
from sendgrid.helpers.mail import Email, To, Content, Mail
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
|
||||||
from aslite.db import load_features
|
from aslite.db import load_features
|
||||||
from aslite.db import get_tags_db
|
from aslite.db import get_tags_db
|
||||||
@@ -60,7 +61,7 @@ body {
|
|||||||
<body>
|
<body>
|
||||||
|
|
||||||
<br><br>
|
<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>
|
<br><br>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
@@ -69,7 +70,7 @@ body {
|
|||||||
|
|
||||||
<br><br>
|
<br><br>
|
||||||
<div>
|
<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>
|
||||||
<div> <3, arxiv-sanity-lite. </div>
|
<div> <3, arxiv-sanity-lite. </div>
|
||||||
|
|
||||||
@@ -159,7 +160,7 @@ def render_recommendations(user, tags, tag_pids, tag_scores):
|
|||||||
if len(summary) == 500:
|
if len(summary) == 500:
|
||||||
summary += '...'
|
summary += '...'
|
||||||
# create the url that will feature this paper on top and also show the most similar papers
|
# 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(
|
parts.append(
|
||||||
"""
|
"""
|
||||||
<tr>
|
<tr>
|
||||||
@@ -196,27 +197,47 @@ def render_recommendations(user, tags, tag_pids, tag_scores):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# send the actual html via sendgrid
|
# send the actual html via smtp
|
||||||
|
|
||||||
def send_email(to, html):
|
def send_email(to, html):
|
||||||
|
|
||||||
# init the api
|
# init the api
|
||||||
assert os.path.isfile('sendgrid_api_key.txt')
|
# assert os.path.isfile('sendgrid_api_key.txt')
|
||||||
api_key = open('sendgrid_api_key.txt', 'r').read().strip()
|
# api_key = open('sendgrid_api_key.txt', 'r').read().strip()
|
||||||
sg = sendgrid.SendGridAPIClient(api_key=api_key)
|
# 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 :)
|
# hope for the best :)
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
response = sg.client.mail.send.post(request_body=mail.get())
|
try:
|
||||||
print(response.status_code)
|
# Connect to the server
|
||||||
pass
|
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)
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user