diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-11-19 21:08:47 +0100 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-11-19 21:08:47 +0100 |
commit | da3db1fc0a84801a927ea3223923ee3d45a24d6a (patch) | |
tree | 2da5b915163f940f6db3a51ed0cdd9d1d709cd13 /pyaggr3g470r | |
parent | When the title is not found. (diff) | |
download | newspipe-da3db1fc0a84801a927ea3223923ee3d45a24d6a.tar.gz newspipe-da3db1fc0a84801a927ea3223923ee3d45a24d6a.tar.bz2 newspipe-da3db1fc0a84801a927ea3223923ee3d45a24d6a.zip |
Emails via Postmark and traditional SMTP server.
Diffstat (limited to 'pyaggr3g470r')
-rw-r--r-- | pyaggr3g470r/emails.py | 104 |
1 files changed, 59 insertions, 45 deletions
diff --git a/pyaggr3g470r/emails.py b/pyaggr3g470r/emails.py index 6ec4e74d..f55676c2 100644 --- a/pyaggr3g470r/emails.py +++ b/pyaggr3g470r/emails.py @@ -43,23 +43,42 @@ def send_async_email(mfrom, mto, msg): s.sendmail(mfrom, mto, msg.as_string()) s.quit() -def send_email(mfrom, mto, feed, article): +def send(*args, **kwargs): """ - Send the article via mail. + This decorator enables to send email through Postmark + or a SMTP server. + """ + if conf.ON_HEROKU: + send_postmark(to=kwargs.get("to"), + bcc=kwargs.get("bcc"), + subject=kwargs.get("subject"), + plaintext=kwargs.get("plaintext")) + else: + send_email(to=kwargs.get("to"), + bcc=kwargs.get("bcc"), + subject=kwargs.get("subject"), + plaintext=kwargs.get("plaintext"), + html=kwargs.get("html")) + +def send_email(to="", bcc="", subject="", plaintext="", html=""): + """ + Send an email. """ # Create the body of the message (a plain-text and an HTML version). html = """<html>\n<head>\n<title>%s</title>\n</head>\n<body>\n%s\n</body>\n</html>""" % \ (feed.title + ": " + article.title, article.content) - text = utils.clear_string(article.content) + if plaintext == "": + plaintext = utils.clear_string(html) # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') - msg['Subject'] = '[pyAggr3g470r] ' + feed.title + ": " + article.title - msg['From'] = mfrom - msg['To'] = mto + msg['Subject'] = subject + msg['From'] = conf.NOTIFICATION_EMAIL + msg['To'] = to + msg['BCC'] = bcc # Record the MIME types of both parts - text/plain and text/html. - part1 = MIMEText(text, 'plain', 'utf-8') + part1 = MIMEText(plaintext, 'plain', 'utf-8') part2 = MIMEText(html, 'html', 'utf-8') # Attach parts into message container. @@ -74,16 +93,13 @@ def send_email(mfrom, mto, feed, article): except Exception: logger.exception("send_email raised:") else: - s.sendmail(mfrom, mto, msg.as_string()) + s.sendmail(conf.NOTIFICATION_EMAIL, msg['To'] + ", " + msg['BCC'], msg.as_string()) s.quit() - -# -# Notifications -# -def send_heroku(user=None, bcc="", subject="", plaintext=""): +def send_postmark(to="", bcc="", subject="", plaintext=""): """ - Send an email on Heroku via Postmark. + Send an email via Postmark. Used when the application is deployed on + Heroku. """ try: message = PMMail(api_key = conf.POSTMARK_API_KEY, @@ -94,30 +110,33 @@ def send_heroku(user=None, bcc="", subject="", plaintext=""): message.to = conf.NOTIFICATION_EMAIL message.bcc = bcc elif bcc == "": - message.to = user.email + message.to = recipients message.send() except Exception as e: - logger.exception("send_heroku raised:") + logger.exception("send_postmark raised:") raise e + +# +# Notifications +# def information_message(subject, plaintext): """ Send an information message to the users of the platform. """ from pyaggr3g470r.models import User users = User.query.all() - emails = [user.email for user in users] - if conf.ON_HEROKU: - # Postmark has a limit of twenty recipients per message in total. - #for i in xrange(0, len(emails), 20): - #send_heroku(bcc=", ".join(emails[i:i+20]), subject=subject, plaintext=plaintext) - for user in users: - try: - send_heroku(user=user, subject=subject, plaintext=plaintext) - except: - continue - else: - pass + # Only send email for activated accounts. + emails = [user.email for user in users if user.activation_key == ""] + # Postmark has a limit of twenty recipients per message in total. + for i in xrange(0, len(emails), 19): + send(to=conf.NOTIFICATION_EMAIL, bcc=", ".join(emails[i:i+19]), subject=subject, plaintext=plaintext) + + #for user in users: + #try: + #send(user=user, subject=subject, plaintext=plaintext) + #except: + #continue def new_account_notification(user): """ @@ -125,10 +144,7 @@ def new_account_notification(user): """ plaintext = """Hello,\n\nYour account has been created. Click on the following link to confirm it:\n%s\n\nSee you,""" % \ (conf.PLATFORM_URL + 'confirm_account/' + user.activation_key) - if conf.ON_HEROKU: - send_heroku(user=user, subject="[pyAggr3g470r] Account creation", plaintext=plaintext) - else: - pass + send(to=conf.NOTIFICATION_EMAIL, bcc=user.email, subject="[pyAggr3g470r] Account creation", plaintext=plaintext) def new_account_activation(user): """ @@ -136,25 +152,23 @@ def new_account_activation(user): """ plaintext = """Hello,\n\nYour account has been activated. You can now connect to the platform:\n%s\n\nSee you,""" % \ (conf.PLATFORM_URL) - if conf.ON_HEROKU: - send_heroku(user=user, subject="[pyAggr3g470r] Account activated", plaintext=plaintext) - else: - pass + send(to=conf.NOTIFICATION_EMAIL, bcc=user.email, subject="[pyAggr3g470r] Account activated", plaintext=plaintext) def new_password_notification(user, password): """ + New password notification. """ plaintext = """Hello,\n\nA new password has been generated at your request:\n\n%s""" % \ (password, ) plaintext += "\n\nIt is advised to replace it as soon as connected to pyAggr3g470r.\n\nSee you," - - if conf.ON_HEROKU: - send_heroku(user=user, subject="[pyAggr3g470r] New password", plaintext=plaintext) - else: - pass + send(to=conf.NOTIFICATION_EMAIL, bcc=user.email, subject="[pyAggr3g470r] New password", plaintext=plaintext) def new_article_notification(user, feed, article): - if conf.ON_HEROKU: - pass - else: - send_email(conf.NOTIFICATION_EMAIL, user.email, feed, article) + """ + New article notification. + """ + subject = '[pyAggr3g470r] ' + feed.title + ": " + article.title + html = """<html>\n<head>\n<title>%s</title>\n</head>\n<body>\n%s\n</body>\n</html>""" % \ + (feed.title + ": " + article.title, article.content) + plaintext = utils.clear_string(html) + send(to=conf.NOTIFICATION_EMAIL, bcc=user.email, subject=subject, plaintext=plaintext, html=html) |