gpt4 book ai didi

python - 内部服务器错误 : when trying to use flask-mail

转载 作者:行者123 更新时间:2023-12-05 07:05:14 26 4
gpt4 key购买 nike

我正在尝试通过 flask-mail 向用户发送邮件。下面显示的代码在本地主机上运行良好。但是,当我将 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)

最佳答案

您可以使用 mail.send_message() 接受参数 titlesenderrecipients正文。这是我用来发送电子邮件激活 token 的类似代码:

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)

关于python - 内部服务器错误 : when trying to use flask-mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62791817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com