forked from beimingwu/beimingwu
Merge pull request #356 from Learnware-LAMDA/email_html
fix(backend): send email using html format
This commit is contained in:
commit
2501d0a239
|
|
@ -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": {}}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"""\
|
||||
<html>
|
||||
<body>
|
||||
<div style="margin:0;">
|
||||
Dear {username},
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
<br />
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
Welcome to Beimingwu! Thanks for signing up. Please follow this link to activate your account:
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
<br />
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
<div style="margin:0;">
|
||||
{ confirm_url }
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
Cheers!
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
Beimingwu Group
|
||||
</div>
|
||||
</div>
|
||||
<br />
|
||||
</body>
|
||||
</html>
|
||||
""",
|
||||
subtype="html",
|
||||
)
|
||||
|
||||
{ confirm_url }
|
||||
thread = mp.Process(
|
||||
target=send_email_worker,
|
||||
args=(sender_email, password, receiver_email, message, smtp_server, port, proxy_host, proxy_port),
|
||||
)
|
||||
thread.start()
|
||||
|
||||
Cheers!
|
||||
Beimingwu Group
|
||||
"""
|
||||
|
||||
# thread = mp.Process(
|
||||
# target=send_email_worker,
|
||||
# args=(sender_email, password, receiver_email, message, smtp_server, port, proxy_host, proxy_port),
|
||||
# )
|
||||
# thread.start()
|
||||
|
||||
# return thread
|
||||
send_email_worker(sender_email, password, receiver_email, message, smtp_server, port, proxy_host, proxy_port)
|
||||
return None
|
||||
return thread
|
||||
# send_email_worker(sender_email, password, receiver_email, message, smtp_server, port, proxy_host, proxy_port)
|
||||
# return None
|
||||
|
||||
|
||||
def send_reset_password_email(email: str, verification_code: str, user_id: str, email_config: dict) -> bool:
|
||||
def send_reset_password_email(
|
||||
email: str, username: str, verification_code: str, user_id: str, email_config: dict
|
||||
) -> bool:
|
||||
port = email_config["smtp_port"]
|
||||
smtp_server = email_config["smtp_server"]
|
||||
sender_email = email_config["sender_email"]
|
||||
receiver_email = email
|
||||
password = email_config["smtp_password"]
|
||||
confirm_url = email_config["reset_password_url"] + "?code=" + verification_code + "&user_id=" + user_id
|
||||
reset_url = email_config["reset_password_url"] + "?code=" + verification_code + "&user_id=" + user_id
|
||||
proxy_host = email_config.get("proxy_host", "")
|
||||
proxy_port = email_config.get("proxy_port", 0)
|
||||
|
||||
message = f"""\
|
||||
From: {sender_email}\r\n\
|
||||
Subject: Reset your password\r\n\
|
||||
\r\n
|
||||
Please follow this link to reset your password:
|
||||
message = EmailMessage()
|
||||
message["From"] = sender_email
|
||||
message["To"] = receiver_email
|
||||
message["Subject"] = "[bmwu]Reset your password"
|
||||
message.set_content(
|
||||
f"""\
|
||||
<html>
|
||||
<body>
|
||||
<div style="margin:0;">
|
||||
Dear {username},
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
<br />
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
Please follow this link to reset your password:
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
<br />
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
<div style="margin:0;">
|
||||
{ reset_url }
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
Cheers!
|
||||
</div>
|
||||
<div style="margin:0;">
|
||||
Beimingwu Group
|
||||
</div>
|
||||
</div>
|
||||
<br />
|
||||
</body>
|
||||
</html>
|
||||
""",
|
||||
subtype="html",
|
||||
)
|
||||
|
||||
{ confirm_url }
|
||||
|
||||
Cheers!
|
||||
|
||||
Beimingwu Group
|
||||
"""
|
||||
|
||||
# thread = mp.Process(
|
||||
# target=send_email_worker,
|
||||
# args=(sender_email, password, receiver_email, message, smtp_server, port, proxy_host, proxy_port),
|
||||
# )
|
||||
# thread.start()
|
||||
# return thread
|
||||
|
||||
send_email_worker(sender_email, password, receiver_email, message, smtp_server, port, proxy_host, proxy_port)
|
||||
return None
|
||||
thread = mp.Process(
|
||||
target=send_email_worker,
|
||||
args=(sender_email, password, receiver_email, message, smtp_server, port, proxy_host, proxy_port),
|
||||
)
|
||||
thread.start()
|
||||
return thread
|
||||
|
|
|
|||
Loading…
Reference in New Issue