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

标签 python flask flask-mail

我正在尝试通过 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/

相关文章:

python - Flask-mongoengine 文档 id

docker - 尝试使用 Flask-mail 发送电子邮件时出现 SSL 错误

python - 我怎样才能避免出现 OSError : [Errno 9] Bad file descriptor using ibapi?

python - 使用 PIL.Image._getexif() 访问 exif 信息时遇到问题

python - 我的 openssl 和 ssl 默认 CA 证书路径是什么?

python - 删除不用作外键的记录的通用解决方案

python - 使用 Postgresql 在 Google App Engine 上使用 SQLAlchemy 和 pg8000 KeyError/Broken Pipe

python - Firefox 断开连接后 Flask sse-stream 未终止

python - 错误: [Errno 111] Connection refused in flask -mail

python - Flask 邮件安全不符合 Microsoft Outlook 的安全要求?