Home > Mobile >  Unable to send emails through Flask-mail
Unable to send emails through Flask-mail

Time:01-17

I have been trying to create a web app which takes email address as an input through HTML form and sends a one time pin for further access to website.

  1. I have 2 html files in my template folder (one for taking user's email address and other for OTP entering)

  2. i have config.json file which stores my accountid and password through which i intend to send the OTP.

.py file


from flask import Flask, render_template, request
from random import randint
import json
from flask_mail import *

with open('config.json','r') as f:
    params = json.load(f)['params']
mail = Mail(app)

otp = randint(100,999)      #otp production

app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = params['gmail-user']
app.config['MAIL_PASSWORD'] = params['gmail-password']


@app.route('/')
def home():
    return(render_template('otpgive.html'))

@app.route('/getOTP', methods = ['POST'])
def getOTP(): #OTP send and Verify here
    email_id = request.form['usermail']
    msg = Message('OTP for the APPLICATION', sender = 'my_email', recipients = [email_id]) 

  #my_email is the email through which i plan to send messages.

    msg.body = "Greetings! Your email has been verified successfully. Kindly note your OTP is "   str(otp)
    mail.send(msg)
    return render_template('otpconfirm.html')

@app.route('/validateOTP', methods = ['POST'])
def validateOTP():
    userotp = request.form['otp']
    if (otp == int(userotp)):
        return ("Email Verified Succcessfully ")
    else:
        return render_template('otpgive.html',msg = 'incorrect otp')

if __name__ == '__main__':
    app.run(debug = False)
    #app.run(host='0.0.0.0',port=5000, debug = True)

Solutions I tried but failed:

  1. Tried disabling the firewall.
  2. Tried setting the port number for 0.0.0.0
  3. Tried debug = False

I was expecting it to work. i.e send emails to the users but it shows ConnectionError or InternalServerError

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

ConnectionRefusedError:

[WinError 10061] No connection could be made because the target machine actively refused it

CodePudding user response:

I finally got the solution.

  1. Since I was using G-Mail, I had to enable 2FA (2-Factor Auth) first on my profile, and then generate a new password for my app.
  2. The password thus obtained was pasted in the config.json file instead of my main profile password.

Used directions from this thread Less Secure option in gmail unavailable


Now Changes I made in my code:

  1. Reverted back to ip: host='0.0.0.0', port=5000, debug = True
  2. I kept firewall disabled as a precaution.

I repositioned mail = Mail(app) line to after all app.configs

  • Related