I'm developing a service to send email to user to recover password. But, when I make a request to a given endpoint to call this function, I receive ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997).
I'm using MailTrap in development and my environment variables are written in .env file like this:
MAIL_SERVER="sandbox.smtp.mailtrap.io"
MAIL_PORT=2525
MAIL_USERNAME="my_username"
MAIL_PASSWORD="my_password"
MAIL_USE_SSL=false
MAIL_USE_TLS=true
This is a default configuration provided by MailTrap to be used in Flask-Mail.
I tried use other values for MAIL_PORT, MAIL_USE_SSL, MAIL_USE_TLS, like:
MAIL_PORT=587
MAIL_USE_SSL=false
MAIL_USE_TLS=true
and
MAIL_PORT=465
MAIL_USE_SSL=true
MAIL_USE_TLS=false
but nothing was enough.
Code used to send email is:
from flask import current_app, render_template
from flask_mail import Message
from gymanager.extensions.mailer import mail
def send_mail(subject, to, template, **kwargs):
msg = Message(
subject=subject,
recipients=[to],
sender=current_app.config["MAIL_SENDER"]
)
msg.body = render_template(f"mails/{template}.txt", **kwargs)
msg.html = render_template(f"mails/{template}.html", **kwargs)
mail.send(msg)
Resource that calls the function above:
import secrets
from flask_restful import Resource, reqparse
from gymanager.models import User
from gymanager.extensions.database import db
from gymanager.services.mail import send_mail
from werkzeug.security import generate_password_hash
class ForgetPassword(Resource):
def post(self):
parser = reqparse.RequestParser(trim=True)
parser.add_argument("email", required=True, help="required field")
args = parser.parse_args()
user = User.query.filter_by(email=args.email).first()
if not user:
return {"error": "user not found"}, 400
password_temp = secrets.token_hex(8)
user.password = generate_password_hash(password_temp)
db.session.add(user)
try:
db.session.commit()
except Exception as e:
db.session.rollback()
return {"error": "could not possible reset password"}, 500
send_mail(
subject="recuperação da conta",
to=user.email,
template="forget-password",
password_temp=password_temp
)
return {"msg": "email sended successfully"}, 201
This resource is called by an endpoint.
How I solve this issue?