![](/img/trans.png)
[英]flask-mail : ConnectionRefusedError when trying to send an email from flask server
[英]Internal Server Error: when trying to use flask-mail
我正在嘗試通過燒瓶郵件向用戶發送郵件。 下面顯示的代碼在 localhost 上運行良好。 但是,當我將 flask 應用程序部署到 AWS Elastic Beanstalk 並使用 send_reset_email function 時,它會拋出內部服務器錯誤。 我應該在哪里更改我的代碼? 任何幫助將不勝感激。
我的配置代碼:
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)
我的 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)
您可以使用mail.send_message()
接收 arguments title
、 sender
、 recipients
和body
。 這是我用來發送 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.