From 2e695a4331430050465c4e8b892768af5e18e934 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Sat, 17 Jun 2017 22:36:44 +0200 Subject: The Postmark team has chosen not to continue development of the Heroku Add-on as of June 30, 2017. Newspipe is now using SendGrid when deployed on Heroku. --- src/conf.py | 5 +++-- src/notifications/emails.py | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/conf.py b/src/conf.py index 9d4c363c..6299f601 100644 --- a/src/conf.py +++ b/src/conf.py @@ -27,7 +27,7 @@ DEFAULTS = {"platform_url": "https://www.newspipe.org/", "self_registration": "false", "cdn_address": "", "admin_email": "info@newspipe.org", - "postmark_api_key": "", + "sendgrid_api_key": "", "token_validity_period": "3600", "default_max_error": "3", "log_path": "newspipe.log", @@ -117,7 +117,8 @@ NOTIFICATION_TLS = config.getboolean('notification', 'tls') NOTIFICATION_SSL = config.getboolean('notification', 'ssl') NOTIFICATION_USERNAME = config.get('notification', 'username') NOTIFICATION_PASSWORD = config.get('notification', 'password') -POSTMARK_API_KEY = config.get('notification', 'postmark_api_key') +SENDGRID_API_KEY = config.get('notification', 'sendgrid_api_key') +POSTMARK_API_KEY = '' CSRF_ENABLED = True # slow database query threshold (in seconds) diff --git a/src/notifications/emails.py b/src/notifications/emails.py index 479d43cf..ebf301ef 100644 --- a/src/notifications/emails.py +++ b/src/notifications/emails.py @@ -24,7 +24,8 @@ import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText -from postmark import PMMail +import sendgrid +from sendgrid.helpers.mail import * import conf from web.decorators import async @@ -45,11 +46,11 @@ def send_async_email(mfrom, mto, msg): def send(*args, **kwargs): """ - This functions enables to send email through Postmark + This functions enables to send email through SendGrid or a SMTP server. """ if conf.ON_HEROKU: - send_postmark(**kwargs) + send_sendgrid(**kwargs) else: send_smtp(**kwargs) @@ -83,11 +84,16 @@ def send_smtp(to="", bcc="", subject="", plaintext="", html=""): s.sendmail(conf.NOTIFICATION_EMAIL, msg['To'] + ", " + msg['BCC'], msg.as_string()) s.quit() + def send_postmark(to="", bcc="", subject="", plaintext=""): """ Send an email via Postmark. Used when the application is deployed on Heroku. + Note: The Postmark team has chosen not to continue development of this + Heroku add-on as of June 30, 2017. Newspipe is now using SendGrid when + deployed on Heroku. """ + from postmark import PMMail try: message = PMMail(api_key = conf.POSTMARK_API_KEY, subject = subject, @@ -98,5 +104,26 @@ def send_postmark(to="", bcc="", subject="", plaintext=""): message.bcc = bcc message.send() except Exception as e: - logger.exception("send_postmark raised:") + logger.exception('send_postmark raised:') raise e + + +def send_sendgrid(to="", bcc="", subject="", plaintext=""): + """ + Send an email via SendGrid. Used when the application is deployed on + Heroku. + """ + sg = sendgrid.SendGridAPIClient(apikey=conf.SENDGRID_API_KEY) + from_email = Email(conf.NOTIFICATION_EMAIL) + subject = subject + to_email = Email(to) + content = Content('text/plain', plaintext) + mail = Mail(from_email, subject, to_email, content) + if bcc != "": + personalization = Personalization() + personalization.add_bcc(Email(bcc)) + mail.add_personalization(personalization) + response = sg.client.mail.send.post(request_body=mail.get()) + # print(response.status_code) + # print(response.body) + # print(response.headers) -- cgit