Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

python - flask email not sending

I using flask and need send email

my code for it

from flask.ext.mail import Mail, Message
#mail config
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'mymail@gmail.com'
app.config['MAIL_PASSWORD'] = 'mypassword'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)

"send" route

@app.route('/send/')
def send():
    msg = Message('Hi', sender = 'mymail@gmail.com', recipients = ['recipient@gmail.com'])
    msg.body = "This is the email body sending with flask!"
    mail.send(msg)
    #msg.html = '<b>HTML</b> body'
    return "Sent"

But after clicking on the above company I get the error

TypeError: __init__() takes 1 positional argument but 2 were given

File "I:pythonlibsite-packagesflaskapp.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "I:pythonlibsite-packagesflaskapp.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "I:pythonlibsite-packagesflaskapp.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "I:pythonlibsite-packagesflask\_compat.py", line 33, in reraise
raise value
File "I:pythonlibsite-packagesflaskapp.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "I:pythonlibsite-packagesflaskapp.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "I:pythonlibsite-packagesflaskapp.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "I:pythonlibsite-packagesflask\_compat.py", line 33, in reraise
raise value
File "I:pythonlibsite-packagesflaskapp.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "I:pythonlibsite-packagesflaskapp.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "G:flaskfopsterhello.py", line 56, in send
msg = Message('Hi', sender = 'mymail@gmail.com', recipients = ['recipient@gmail.com'])

What could be the problem? The name of the mail addresses changed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're instantiating your model Message class and not the flask mail Message class.

Do something like :

from flask.ext.mail import Mail, Message as MailMessage

@app.route('/send/')
def send():
    msg = MailMessage('Hi', sender = 'mymail@gmail.com', recipients = ['recipient@gmail.com'])
    msg.body = "This is the email body sending with flask!"
    mail.send(msg)
    #msg.html = '<b>HTML</b> body'
    return "Sent"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...