Fix UTF-8 bug in email sending code

This commit is contained in:
Noah 2015-03-20 00:08:33 +00:00
parent c665db20b8
commit ca9e34f117

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import (g, session, request, render_template, flash, redirect, from flask import (g, session, request, render_template, flash, redirect,
url_for, current_app) url_for, current_app)
from functools import wraps from functools import wraps
@ -15,6 +17,8 @@ import json
import urlparse import urlparse
import traceback import traceback
import socket import socket
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from rophako.log import logger from rophako.log import logger
from rophako.settings import Config from rophako.settings import Config
@ -181,22 +185,23 @@ def send_email(to, subject, message, sender=None, reply_to=None):
if Config.mail.method == "smtp": if Config.mail.method == "smtp":
# Send mail with SMTP. # Send mail with SMTP.
for email in to: for email in to:
# Construct the mail headers. msg = MIMEMultipart("alternative")
headers = [ msg.set_charset("utf-8")
"From: {}".format(sender),
"To: {}".format(email),
]
if reply_to is not None:
headers.append("Reply-To: {}".format(reply_to))
headers.append("Subject: {}".format(subject))
# Prepare the mail for transport. msg["Subject"] = subject
msg["From"] = sender
msg["To"] = email
if reply_to is not None:
msg["Reply-To"] = reply_to
text = MIMEText(message, "plain", "utf-8")
msg.attach(text)
# Send the e-mail.
try: try:
server = smtplib.SMTP(Config.mail.server, Config.mail.port) server = smtplib.SMTP(Config.mail.server, Config.mail.port)
msg = "\n".join(headers) + "\n\n" + message server.sendmail(sender, [email], msg.as_string())
server.sendmail(sender, email, msg) except:
server.quit()
except socket.error:
pass pass