【问题标题】:Internal Server Error: when trying to use flask-mail内部服务器错误:尝试使用烧瓶邮件时
【发布时间】:2020-07-08 09:36:00
【问题描述】:

我正在尝试通过烧瓶邮件向用户发送邮件。下面显示的代码在 localhost 上运行良好。但是,当我将烧瓶应用程序部署到 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 是什么?为什么是缩进的?您可以在哪里添加完整的错误回溯到您的问题?
  • 你能检查日志并显示错误的回溯吗?
  • @mtshaikh 我是初学者。我在 log\nginx\error 中找不到此错误的回溯。这就是这个文件的样子 2020/07/08 08:01:33 [warn] 32194#0: *39 客户端请求正文被缓冲到临时文件 /var/lib/nginx/tmp/client_body/0000000002,客户端:1.38.76.34,服务器:,请求:“POST /account HTTP/1.1”,主机:“blogbydarshan-env.eba-5yxwxghg.ap-south-1.elasticbeanstalk.com”,引用者:“blogbydarshan-env.eba-5yxwxghg.ap-south-1.elasticbeanstalk.com/…”跨度>
  • 您需要提供回溯。

标签: python flask flask-mail


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2014-08-06
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2023-03-27
    • 2018-08-29
    相关资源
    最近更新 更多