diff options
-rw-r--r-- | app.json | 2 | ||||
-rw-r--r-- | requirements.txt | 2 | ||||
-rw-r--r-- | src/conf.py | 5 | ||||
-rw-r--r-- | src/notifications/emails.py | 35 |
4 files changed, 36 insertions, 8 deletions
@@ -66,7 +66,7 @@ "HEROKU": "1", "CDN_ADDRESS": "https://cdn.cedricbonhomme.org/", "NOTIFICATION_EMAIL": "newspipe@no-reply.com", - "POSTMARK_API_KEY": "REDACTED", + "SENDGRID_API_KEY": "REDACTED", "CRAWLER_RESOLV": { "description": "Specify if the crawler should try to resolve link of articles behind proxies.", "value": "false" diff --git a/requirements.txt b/requirements.txt index 0f43eec2..12a4bfc9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,5 +22,5 @@ Flask-SSLify==0.1.5 Flask-Migrate==2.0.4 Flask-Script==2.0.5 WTForms==2.1 -python-postmark==0.4.12 +sendgrid==4.2.0 python-dateutil==2.6.0 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) |