Python Flask邮件-无法发送带有附件的邮件
我有一些工作代码可以异步发送电子邮件(教程提供),但当我尝试添加附件时,我收到以下消息:Python Flask邮件-无法发送带有附件的邮件,python,email,flask,Python,Email,Flask,我有一些工作代码可以异步发送电子邮件(教程提供),但当我尝试添加附件时,我收到以下消息: 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:\Use
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(发送异步电子邮件)由第二个和第三个def调用,除了在第二个def中添加附件外,其他两个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()
您正在使用Python3。下面是
MIMEBase
(Python 3.7.3)的定义,请注意\uuuu init\uuuuu
签名,\umaintype
,\usubtype
是必需的。您需要修改flask\u mail.py
以发送附件
class MIMEBase(message.Message):
"""Base class for MIME specializations."""
def __init__(self, _maintype, _subtype, *, policy=None, **_params):
"""This constructor adds a Content-Type: and a MIME-Version: header.
The Content-Type: header is taken from the _maintype and _subtype
arguments. Additional parameters for this header are taken from the
keyword arguments.
"""
if policy is None:
policy = email.policy.compat32
message.Message.__init__(self, policy=policy)
ctype = '%s/%s' % (_maintype, _subtype)
self.add_header('Content-Type', ctype, **_params)
self['MIME-Version'] = '1.0'
在flask_mail.py
第349行中,我们将MIMEBase
实例化如下:
for attachment in attachments:
f = MIMEBase(*attachment.content_type.split('/'))
要使用Python 3,应将其更改为:
for attachment in attachments:
maintype, subtype = attachment.content_type.split('/')
f = MIMEBase(maintype, subtype)