简体   繁体   中英

No recipients have been added when trying to send message with Flask-Mail

I am trying to send email with Flask-Mail. I create the message with recipients, but I get AssertionError: No recipients have been added when I try to send it. In the following code, I print out the message recipients and they are correct. How do I fix this error?

from flask import Flask
from flask_mail import Message, Mail

app = Flask(__name__)
app.config.update(
    DEBUG=True,
    MAIL_SERVER='smtp.gmail.com',
    MAIL_PORT=465,
    MAIL_USE_SSL=True,
    MAIL_USERNAME='socialcreditsystem@gmail.com',
    MAIL_PASSWORD='mypassword'
)
mail = Mail(app)

@app.route('/')
def hello_world():
    msg=Message('hey hey hey', sender='socialcreditsystem@gmail.com', recipients=['julian.fink1000@gmail.com'])
    print(msg.sender, msg.recipients)
    # ('socialcreditsystem@googlemail.com', ['julian.fink1000@googlemail.com'])
    print(msg.send_to)
    # set(['julian.fink1000@googlemail.com'])
    mail.send_message(msg)
    return 'Hello World!'

if __name__ == '__main__':
    app.run()
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Julian\PycharmProjects\flask_mail_test\flask_mail_test.py", line 26, in hello_world
    mail.send_message(msg)
  File "C:\Python27\lib\site-packages\flask_mail.py", line 503, in send_message
    self.send(Message(*args, **kwargs))
  File "C:\Python27\lib\site-packages\flask_mail.py", line 493, in send
    message.send(connection)
  File "C:\Python27\lib\site-packages\flask_mail.py", line 428, in send
    connection.send(self)
  File "C:\Python27\lib\site-packages\flask_mail.py", line 176, in send
    assert message.send_to, "No recipients have been added"
AssertionError: No recipients have been added

You're using the wrong function. mail.send_message is a shortcut to build and send a message, it takes the same args as Message . Use mail.send(msg) to send an existing Message instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM