0

I'm trying to follow an online tutorial to use flask email in sending emails. But I get a 405 Not Allowed message when I submit the form. Unfortunately the author doesn't exlain anything about these error messages. I'm using google as the mail server. Can someone see where I'm going wrong? Thanks in advance. here's the code:

import os
from flask import Flask, render_template, request
from flask_mail import Mail, Message

app = Flask(__name__)


app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'milestone2020.projects@gmail.com'
app.config['MAIL_PASSWORD'] = '******'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

mail = Mail(app)


@app.route('/')

def index():
    return render_template('home.html')


@app.route('/send_message', methods=['GET', 'POST'])
def send_message():
    if request.method == 'POST':
        email = request.form['email']
        subject = request.form['subject']
        msg = request.form['message']

        message = Message(subject, sender='milestone2020.projects@gmail.com',
                          recipients=[email])

        message.body = msg

        mail.send(message)

        success = "Message sent"

        return render_template('result.html', success=success)


if __name__ == '__main__':
   app.run(debug = True)er code here


This is the form at **home.html**.

<form action="{{url_for('send_message')}}" method="POST">    
    <input type="email" id="email" name="email" placeholder="Email" required><br><br>  
    <input type="text" id="subject" name="subject" placeholder="subject" required><br><br>
    <textarea name="message" id="message" cols="30" rows="10" placeholder="Message body"></textarea>
    <br><br>
    <input type="submit" value="Submit">
  </form>


**result.html**

{{success}}

go to home page
new_user
  • 11
  • 1
  • The error message means that your email configuration is not correct. also see https://stackoverflow.com/questions/37058567/configure-flask-mail-to-use-gmail – Jürgen Gmach Aug 30 '20 at 05:20
  • Does this answer your question? [Configure Flask-Mail to use GMail](https://stackoverflow.com/questions/37058567/configure-flask-mail-to-use-gmail) – Jürgen Gmach Aug 30 '20 at 05:21
  • Thanks a lot. But the issue still persists. I 've changed the gmail password to the new generated app password. I chose the device to logon as my windows computer because that's where I'm running the python script from. Is that the correct way to go about this? here's the error: 127.0.0.1 - - [31/Aug/2020 18:21:25] "POST /send_message HTTP/1.1" 500 - 127.0.0.1 - - [31/Aug/2020 18:22:23] "POST /send_message HTTP/1.1" 500 - – new_user Aug 31 '20 at 18:31

1 Answers1

0

Disabling CAPTCHA for clients If you are not using 2-factor authentication and you have verified the credentials in your Python source are correct, follow these steps:

1.) Login to gmail in your browser

2.) Navigate to the DisplayUnclockCaptcha page.

3.) Click the continue button, and you will see the message 'Account access enabled Please try signing in to your Google account again from your new device or application.'

4.)Run your Python script - your login attempt should be successful.

  • Many thanks for all the help. I'm still getting a 405 error message even after having enabled 2-factor authentication and replaced the gmail password in the flask app with the newly generated app password. – new_user Aug 31 '20 at 18:27