From ecb73e376e4807087a87d38f73d7699b69c241f1 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Sat, 21 Mar 2020 10:30:23 +0100 Subject: fixed email encoding issue --- newspipe/notifications/emails.py | 11 +++++++---- newspipe/notifications/notifications.py | 8 ++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/newspipe/notifications/emails.py b/newspipe/notifications/emails.py index 296e793d..44b1d8b3 100644 --- a/newspipe/notifications/emails.py +++ b/newspipe/notifications/emails.py @@ -23,6 +23,7 @@ import logging import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from email import charset from newspipe.bootstrap import application from newspipe.web.decorators import async_maker @@ -41,7 +42,7 @@ def send_async_email(mfrom, mto, msg): except Exception: logger.exception("send_async_email raised:") else: - s.sendmail(mfrom, mto, msg.as_string()) + s.sendmail(mfrom, mto, msg.as_bytes().decode(encoding="UTF-8")) s.quit() @@ -58,19 +59,21 @@ def send_smtp(to="", subject="", plaintext="", html=""): """ # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart("alternative") + ch = charset.add_charset("utf-8", charset.QP) + msg.set_charset(ch) msg["Subject"] = subject msg["From"] = application.config["MAIL_DEFAULT_SENDER"] msg["To"] = to # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(plaintext, "plain", "utf-8") - part2 = MIMEText(html, "html", "utf-8") + # part2 = MIMEText(html, "html", "utf-8") # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) - msg.attach(part2) + # msg.attach(part2) try: s = smtplib.SMTP(application.config["MAIL_SERVER"]) @@ -85,6 +88,6 @@ def send_smtp(to="", subject="", plaintext="", html=""): s.sendmail( application.config["MAIL_DEFAULT_SENDER"], msg["To"], - msg.as_bytes().decode(encoding='UTF-8'), + msg.as_bytes().decode(encoding="UTF-8"), ) s.quit() diff --git a/newspipe/notifications/notifications.py b/newspipe/notifications/notifications.py index 633af4c3..debbee61 100644 --- a/newspipe/notifications/notifications.py +++ b/newspipe/notifications/notifications.py @@ -46,9 +46,7 @@ def new_account_notification(user, email): ) emails.send( - to=email, - subject="[Newspipe] Account creation", - plaintext=plaintext, + to=email, subject="[Newspipe] Account creation", plaintext=plaintext, ) @@ -58,7 +56,5 @@ def new_password_notification(user, password): """ plaintext = render_template("emails/new_password.txt", user=user, password=password) emails.send( - to=user.email, - subject="[Newspipe] New password", - plaintext=plaintext, + to=user.email, subject="[Newspipe] New password", plaintext=plaintext, ) -- cgit