diff --git a/backend/restful/auth.py b/backend/restful/auth.py index 629e3d9..5a03489 100644 --- a/backend/restful/auth.py +++ b/backend/restful/auth.py @@ -79,7 +79,7 @@ class RegisterApi(flask_restful.Resource): email, secret_key=context.config["app_secret_key"] ) # send email - utils.send_verification_email(email, verification_code, email_config=context.config["email"]) + utils.send_verification_email(email, username, verification_code, email_config=context.config["email"]) pass else: database.update_email_confirm_time(email=email) @@ -104,8 +104,12 @@ class ResendEmailConfirmApi(flask_restful.Resource): return {"code": 21, "msg": "Request parameters error."}, 200 email = body["email"] + user_info = database.get_user_info(by="email", value=email) + if user_info is None: + return {"code": 51, "msg": "Your email not exist. Please re-register"}, 200 + username = user_info["username"] verification_code = utils.generate_email_verification_code(email, secret_key=context.config["app_secret_key"]) - utils.send_verification_email(email, verification_code, email_config=context.config["email"]) + utils.send_verification_email(email, username, verification_code, email_config=context.config["email"]) result = {"code": 0, "msg": "success", "data": {}} @@ -151,8 +155,11 @@ class SendResetPasswordEmailApi(flask_restful.Resource): if user_info is None: return {"code": 51, "msg": "Your email not exist. Please register first"}, 200 user_id = user_info["id"] + username = user_info["username"] verification_code = utils.generate_email_verification_code(email, secret_key=context.config["app_secret_key"]) - utils.send_reset_password_email(email, verification_code, str(user_id), email_config=context.config["email"]) + utils.send_reset_password_email( + email, username, verification_code, str(user_id), email_config=context.config["email"] + ) result = {"code": 0, "msg": "success", "data": {}} diff --git a/backend/restful/utils.py b/backend/restful/utils.py index 38d32bd..9a4adbc 100644 --- a/backend/restful/utils.py +++ b/backend/restful/utils.py @@ -4,6 +4,7 @@ from learnware import learnware import lib.database_operations as dbops import itsdangerous import smtplib +from email.message import EmailMessage import ssl import multiprocessing.dummy as mp import socks @@ -81,7 +82,8 @@ def send_email_worker(sender_email, password, receiver_email, message, smtp_serv with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(sender_email, password) - server.sendmail(sender_email, receiver_email, message) + # server.sendmail(sender_email, receiver_email, message) + server.send_message(message) pass if len(proxy_host) > 0 and proxy_port > 0: @@ -92,7 +94,7 @@ def send_email_worker(sender_email, password, receiver_email, message, smtp_serv pass -def send_verification_email(email: str, verification_code: str, email_config: dict) -> bool: +def send_verification_email(email: str, username: str, verification_code: str, email_config: dict) -> bool: port = email_config["smtp_port"] smtp_server = email_config["smtp_server"] sender_email = email_config["sender_email"] @@ -102,58 +104,114 @@ def send_verification_email(email: str, verification_code: str, email_config: di proxy_host = email_config.get("proxy_host", "") proxy_port = email_config.get("proxy_port", 0) - message = f"""\ -From: {sender_email}\r\n\ -Subject: Please activate your account\r\n\ -\r\n - Welcome! Thanks for signing up. Please follow this link to activate your account: + message = EmailMessage() + message["From"] = sender_email + message["To"] = receiver_email + message["Subject"] = "[bmwu]Please activate your account" + message.set_content( + f"""\ + +
+