Send email automatically in Flask framework

Viewed 154

I want an email feature where after user registration a confirmation mail will be send.I first take input from users as post method,then pass it to function. Here is my code:

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!!!"})

and send mail class

  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.!!!"}) 

However, mail is not going, how to connect both class so that it will send mail automatically?

send_html_mail fuction is ok,there is no problem.

1 Answers

That is easy to call send_html_mail in post of Register. On the contrary, Exposing post of sendMail is dangerous. Anyone who knows address can send mails.

Related