3

I am trying to send mail to the user through flask-mail. This code shown below works fine on the 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. 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:

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)
Heisenberg
  • 31
  • 5
  • What's `mail`? Why is it indented? And where could you add the full error traceback to your question? – Klaus D. Jul 08 '20 at 09:39
  • 3
    Could you check the logs and show the traceback for the error? – mtshaikh Jul 08 '20 at 09:39
  • @mtshaikh I am beginner. I couldn't find traceback of this error in log\nginx\error. this is what this file looks like 2020/07/08 08:01:33 [warn] 32194#0: *39 a client request body is buffered to a temporary file /var/lib/nginx/tmp/client_body/0000000002, client: 1.38.76.34, server: , request: "POST /account HTTP/1.1", host: "blogbydarshan-env.eba-5yxwxghg.ap-south-1.elasticbeanstalk.com", referrer: "http://blogbydarshan-env.eba-5yxwxghg.ap-south-1.elasticbeanstalk.com/account" – Heisenberg Jul 08 '20 at 10:06
  • 1
    You need to provide the traceback. – Jürgen Gmach Jul 08 '20 at 11:44

1 Answers1

0

You can use mail.send_message() that takes in arguments title, sender, recipients and body. Here is a similar code that i used to send email activation 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)