简体   繁体   中英

Bad file descriptor error with flask-mail

I'm using flask-mail to send the user an email with their new password. I keep getting IOError bad file descriptor when I run the code

app.config.update(
    DEBUG=True,
    #EMAIL SETTINGS
    MAIL_SERVER='smtp.gmail.com',
    MAIL_PORT=465,
    MAIL_USE_SSL=True,
    MAIL_USE_TLS= False,
    MAIL_USERNAME = 'myemail@gmail.com',
    MAIL_PASSWORD = 'mypass'
)

mail = Mail(app)
@app.route('/api/account', methods= ['POST'])
def login():
    e= request.json ['email']
    u= request.json ['user']
    p= request.json ['pass']
    msg= Message("Your password", sender= 'myemail@gmail.com',recipients = ['destinationemail'])
    msg.body= 'Your password is ' + hashedp
    #msg.html ='<b> password </b>'
    mail.send(msg)

    return 'Your new password has been sent through email' 

Does this have something to do with sockets and connection? I can post the full traceback if needed

EDIT full traceback:

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\mpelixp\Documents\joanna\app2api.py", line 95, in login
mail.send(msg)
File "C:\Python27\lib\site-packages\flask_mail.py", line 491, in send
with self.connect() as connection:
File "C:\Python27\lib\site-packages\flask_mail.py", line 144, in __enter__
self.host = self.configure_host()
File "C:\Python27\lib\site-packages\flask_mail.py", line 165, in configure_host
host.login(self.mail.username, self.mail.password)
File "C:\Python27\lib\smtplib.py", line 582, in login
self.ehlo_or_helo_if_needed()
File "C:\Python27\lib\smtplib.py", line 542, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
File "C:\Python27\lib\smtplib.py", line 414, in ehlo
(code, msg) = self.getreply()
File "C:\Python27\lib\smtplib.py", line 370, in getreply
print>>stderr, 'reply:', repr(line)
IOError: [Errno 9] Bad file descriptor

If it doesn't work you can always use yagmail :

import yagmail
yag = yagmail.SMTP(MAIL_USERNAME, MAIL_PASSWORD)
yag.send(to = e, contents = 'Your password is ' + hashedp)

First install it with pip install yagmail (or pip3 for python 3).

It has a lot of functionality, including easily sending HTML emails (with fallback), attaching by pointing to a file and passwordless scripts.

See more on github .

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