У меня есть некоторый рабочий код для асинхронной отправки электронных писем (любезно предоставлено Flask Megatutorial), но когда я пытаюсь добавить вложение, я получаю следующее сообщение:
Exception in thread Thread-5:
Traceback (most recent call last):
File "C:\Users\tribl\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\tribl\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\tribl\PycharmProjects\CM2020\app\email.py", line 11, in send_async_email
mail.send(msg)
File "C:\Users\tribl\PycharmProjects\CM2020\venv\lib\site-packages\flask_mail.py", line 492, in send
message.send(connection)
File "C:\Users\tribl\PycharmProjects\CM2020\venv\lib\site-packages\flask_mail.py", line 427, in send
connection.send(self)
File "C:\Users\tribl\PycharmProjects\CM2020\venv\lib\site-packages\flask_mail.py", line 190, in send
message.as_bytes() if PY3 else message.as_string(),
File "C:\Users\tribl\PycharmProjects\CM2020\venv\lib\site-packages\flask_mail.py", line 385, in as_bytes
return self._message().as_bytes()
File "C:\Users\tribl\PycharmProjects\CM2020\venv\lib\site-packages\flask_mail.py", line 349, in _message
f = MIMEBase(*attachment.content_type.split('/'))
TypeError: __init__() missing 1 required positional argument: '_subtype'
Соответствующий код ниже первое def (send_async_email) вызывается вторым и третьим def, они идентичны, кроме добавления вложения во втором. Кажется, я не могу найти онлайн-справку по этому вопросу, поэтому спрашиваю сообщество.
def send_async_email(app,msg):
with app.app_context():
mail.send(msg)
def send_email(subject, sender, recipients, text_body, html_body):
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
msg.html = html_body
Thread(target=send_async_email,
args=(current_app._get_current_object(), msg)).start()
def send_email_with_attachment(subject, sender, recipients, text_body, html_body):
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
msg.html = html_body
with open("welcome.txt") as fp:
msg.attach("welcome.txt", "welcome.txt", fp.read())
Thread(target=send_async_email,
args=(current_app._get_current_object(), msg)).start()