我希望有一个邮件功能,在用户注册后发送确认邮件。我首先通过POST方法从用户那里获取输入,然后将其传递给函数。以下是我的代码:
reg_args = reqparse.RequestParser()
reg_args.add_argument("name", type=str, help="Name is missing", required=True)
reg_args.add_argument(
"email", type=str, help="Email is missing", required=True)
reg_args.add_argument("password", type=str,
help="Password is missing", required=True)
reg_args.add_argument("cpassword", type=str,
help="Confirm Password is missing", required=True)
class Register(Resource):
def post(self):
try:
args = reg_args.parse_args()
name = args.name.strip()
email = args.email.strip()
password = args.password.strip()
cpassword = args.cpassword.strip()
if password == cpassword:
pass_hashed = pass_salt(str(password))
check_email = mongo.db.tw_user_details.find_one(
{'email': email})
if check_email == None:
query_result = mongo.db.tw_user_details.insert(
{'name': name, 'email': email, 'password': pass_hashed, 'status': 'Active', 'verification': 'Pending', 'token': '', 'registered_date': datetime.now()})
if len(str(query_result)) >= 24:
return jsonify({'status': True, 'uid': str(query_result), 'message': "You have successfully registered!!!"})
else:
return jsonify({'status': False, 'message': "Registration Failed. Please try again!!!"})
else:
return jsonify({'status': False, 'message': "Your Email is already exists. Please Reset Password to Login!!!"})
else:
return jsonify({'status': False, 'message': "Password and Confirm Password don't match!!!"})
except:
return jsonify({'status': False, 'message': "Error:: Something went wrong. Please try again!!!"})
发送邮件类
class sendMail(Resource):
def post(self):
try:
args = reg_args.parse_args()
name = args.name.strip()
email = args.email.strip()
check_details = mongo.db.tw_user_details.find_one({'email': email,'name': name})
if (check_details == None):
return jsonify({'status': False, 'message': "Credential not found!!!"})
else:
body = f'''<!doctype html>
some body
</html>'''
msg = send_html_mail("Sub:New Registration",body,email)
if (msg==True):
return jsonify({'status': True, 'message': "Email Send.!!!"})
else:
return jsonify({'status': False, 'message': "Invalid Mail ID!!!"})
except:
return jsonify({'status': False, 'message': "Email not Send.!!!"})
然而,邮件无法发送,如何连接两个类以便它能自动发送邮件?
send_html_mail
函数是正常的,没有问题。