From 3442b83dbc1b67b01cd43c6fec122eebea8d03cf Mon Sep 17 00:00:00 2001 From: Alon Levy Date: Tue, 13 Jan 2015 15:12:37 +0200 Subject: server/feedback: get rid of flask-mail; left todo: filenames for attachments --- src/server/rz_mail.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'src/server/rz_mail.py') diff --git a/src/server/rz_mail.py b/src/server/rz_mail.py index e512ed91..4addc9f2 100644 --- a/src/server/rz_mail.py +++ b/src/server/rz_mail.py @@ -1,19 +1,35 @@ -from flask_mail import Mail, Message +import smtplib +from os.path import basename +from email.mime.base import MIMEBase +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.utils import COMMASPACE, formatdate +from email import Encoders -mail = None - -def init_mail(webapp): - global mail - mail = Mail(webapp) +from flask import current_app def send_message(recipients, subject, attachments, body): """ - attachments is a list of tuples (filename, data) - libmagic is used to guess the mimetype + attachments is a list of tuples (filename, mimetype, data) """ - msg = Message(subject, recipients=recipients) - msg.body = body + assert isinstance(recipients, list) + send_from = current_app.rz_config.mail_default_sender + msg = MIMEMultipart( + From=send_from, + To=COMMASPACE.join(recipients), + Date=formatdate(localtime=True), + Subject=subject + ) + msg.attach(MIMEText(body)) + for filename, mimetype, data in attachments: - msg.attach(filename=filename, content_type=mimetype, data=data) - mail.send(msg) + maintype, subtype = mimetype.split('/') + part = MIMEBase(maintype, subtype) + part.set_payload(data) + Encoders.encode_base64(part) + part.add_header('Content_Disposition', 'attachment; filename="%s"' % filename) + msg.attach(part) + smtp = smtplib.SMTP(current_app.rz_config.mail_hostname) + smtp.sendmail(send_from, recipients, msg.as_string()) + smtp.close() -- cgit v1.3.1