Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 30010

Flask Mail - unable to send message with attachment

$
0
0

I have some working code to send emails out asynchronously (courtesy of the Flask Megatutorial) but when I try to add an attachment I get the following message:

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'

The relevant code is below, the first def (send_async_email) is called by the second and third defs, these are identical other than the addition of an attachment in the second. I can't seem to find any online help with this hence asking the community.

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()

Viewing all articles
Browse latest Browse all 30010

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>