内部服务器错误:尝试使用Flask-mail时

时间:2020-07-08 09:36:00

标签: python flask flask-mail

我正在尝试通过烧瓶邮件向用户发送邮件。下面显示的这段代码在localhost上运行良好。但是,当我将flask应用程序部署到AWS Elastic Beanstalk并使用send_reset_email函数时,它引发了我内部服务器错误。我应该在哪里更改代码?任何帮助将不胜感激。

我的配置代码:

application = Flask(__name__)
application.config['SECRET_KEY'] = '-------'
application.config["MONGO_URI"] = "mongodb+srv://------"
mongo = PyMongo(application)
db = mongo.db
bcrypt = Bcrypt(application)
application.config['MAIL_SERVER'] = 'smtp.googlemail.com'
application.config['MAIL_PORT'] = 587
application.config['MAIL_USE_TLS'] = True
application.config['MAIL_USERNAME'] = '----'
application.config['MAIL_PASSWORD'] = '----'
mail = Mail(application)

我的功能文件:

def get_reset_token(username, expires_sec=1800):
    s = Serializer(application.config['SECRET_KEY'], expires_sec)
    return s.dumps({'user': username}).decode('utf-8')

def verify_reset_token(token):
    s = Serializer(application.config['SECRET_KEY'])
    try:
        username = s.loads(token)['user']
    except:
        return None
    user = db.user.find_one({ 'username' : username })
    return user

def send_reset_email(user):
    token = get_reset_token(username=user['username'])
    msg = Message('Password Reset Request',sender='----',recipients=[user['email']])
    msg.body = f'''To reset your password, visit the following link:
{url_for('reset_token', token=token, _external=True)}
If you did not make this request then simply ignore this email and no changes will be made.
            '''
    mail.send(msg)

1 个答案:

答案 0 :(得分:0)

您可以使用带有参数mail.send_message()titlesenderrecipients的{​​{1}}。这是我用来发送电子邮件激活令牌的类似代码:

body