Julian
Julian

Reputation: 15

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='[email protected]',
    MAIL_PASSWORD='mypassword'
)
mail = Mail(app)

@app.route('/')
def hello_world():
    msg=Message('hey hey hey', sender='[email protected]', recipients=['[email protected]'])
    print(msg.sender, msg.recipients)
    # ('[email protected]', ['[email protected]'])
    print(msg.send_to)
    # set(['[email protected]'])
    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

Upvotes: 1

Views: 1001

Answers (1)

davidism
davidism

Reputation: 127180

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.

Upvotes: 8

Related Questions