flask-mail
flask-mail copied to clipboard
Delete spaces in the subject
from flask_mail import Message from openbps import mail
msg = Message('Тест длинногоочен письма', html='1231231 3123123', recipients=['[email protected]'])
mail.send(msg)
and get email with subject "Тестдлинногооченписьма"
Subject: =?utf-8?b?0KLQtdGB0YI=?= =?utf-8?b?0LTQu9C4?= =?utf-8?b?0L3QvdC+0LM=?= =?utf-8?b?0L7QvtGH0LXQvQ==?= =?utf-8?b?0L/QuNGB0YzQvNCw?=
if send short subject, get "ok" if set charset as cp1251 (for example), get "ok"
but need long subjects with utf-8
Python 3.5
I have the same problem. Does anyone know how to fix it?
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.multipart import MIMEBase
from email.header import Header
from email import encoders
from email.utils import make_msgid
from werkzeug.datastructures import FileStorage
from io import BytesIO
from sphere import conf # wrapped flask config
from sphere.email.models import Log
from sphere.lib.uploader import save_file
from sphere.email import signals
def send_email(subject, message, recipients, message_type=None, attachments=None,
author=None, data=None, log_success=True, fail_quiet=False, html=True):
""" Отправить сообщение. """
try:
msg = MIMEMultipart()
message_id = make_msgid()
msg['Subject'] = Header(subject, "utf-8")
msg['From'] = conf.MAIL_USERNAME
msg['To'] = ', '.join(recipients)
msg['Message-Id'] = message_id
# Кусок с телом сообщения
content_type = 'html' if html else 'text'
html_part = MIMEText(message, content_type)
html_part.add_header('Content-Disposition', 'inline', filename='body')
msg.attach(html_part)
# Вложения, если есть
if attachments:
for filename, file_content in attachments:
file_part = MIMEBase('application', 'octet-stream')
file_part.set_payload(file_content)
encoders.encode_base64(file_part)
file_part.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(file_part)
# Логгируем сообщение в БД
if log_success:
eml_file_obj = FileStorage(stream=BytesIO(msg.as_bytes()), filename='message.eml')
obj = Log(message_id=message_id,
message_type=message_type,
author=author,
direction='out',
sender=conf.MAIL_USERNAME,
subject=subject,
message=message,
eml=save_file(eml_file_obj, 'email_send'),
data=data)
obj.recipients.extend(recipients)
obj.save()
# Отправляем, если сервер не в режиме отладки
if not conf.MAIL_SUPPRESS_SENDING:
with smtplib.SMTP_SSL(conf.MAIL_HOST, conf.MAIL_PORT) as server:
server.login(conf.MAIL_USERNAME, conf.MAIL_PASSWORD)
server.send_message(msg)
signals.email_sended.send(conf.MAIL_USERNAME, obj=obj, msg=msg)
except Exception as exc:
if not fail_quiet:
raise exc
I have the same problem too with cyrillic subject
I solved it by changing default MAXLINELEN value in email.header module. Seems like nonascii header splits by MAXLINELEN parts when encoding, and it somehow reflecting on spaces between words.
I've just put the next code beside my flask-mail app initialization:
from email import header
header.MAXLINELEN = 32