简体   繁体   English

内部服务器错误:尝试使用烧瓶邮件时

[英]Internal Server Error: when trying to use flask-mail

I am trying to send mail to the user through flask-mail.我正在尝试通过烧瓶邮件向用户发送邮件。 This code shown below works fine on the localhost.下面显示的代码在 localhost 上运行良好。 But when I deployed my flask app to AWS Elastic Beanstalk and use the send_reset_email function, it throws me Internal Server Error.但是,当我将 flask 应用程序部署到 AWS Elastic Beanstalk 并使用 send_reset_email function 时,它会抛出内部服务器错误。 Where should I change my code?我应该在哪里更改我的代码? Any help will be appreciated.任何帮助将不胜感激。

My config code:我的配置代码:

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)

My function file:我的 function 文件:

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)

You can use mail.send_message() that takes in arguments title , sender , recipients and body .您可以使用mail.send_message()接收 arguments titlesenderrecipientsbody Here is a similar code that i used to send email activation token:这是我用来发送 email 激活令牌的类似代码:

code_act = "127.0.0.1:5000/confirm-mail/"+token
mail.send_message("Account activation Link", sender="bot", recipients=email.split(), body="The activation link is " + code_act)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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