我正在使用Flask-mail,但出现此错误:smtplib.SMTPRecipientsRefused

时间:2019-03-28 03:15:38

标签: flask flask-mail

我正在使用Flask邮件制作此联系表单,但出现此错误: “ smtplib.SMTPRecipientsRefused

smtplib.SMTPRecipientsRefused:{'':(555,b'5.5.2语法错误。q18sm30917485pgv.9-gsmtp'“

对于此联系表,我具有应用专用密码,但仍然出现错误。请帮忙!

代码如下:

import os
from flask import Flask, render_template, url_for, redirect, request, flash
from flask_mail import Mail, Message 
from forms import ContactForm
from flask_debugtoolbar import DebugToolbarExtension

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'thisissecrtet'
toolbar = DebugToolbarExtension(app)
app.config.update(
    #email settings
    MAIL_SERVER = 'smtp.gmail.com',
    MAIL_PORT = 465,
    MAIL_USE_SSL = True,
    MAIL_USERNAME = 'myusername',
    MAIL_PASSWORD = 'mypassword'
    )

mail  = Mail(app)


@app.route('/form', methods=['GET', 'POST'])

def Contact():
    form = ContactForm()
    if request.method == 'POST':
        if form.validate() == False:
            flash('You must enter something in all that fields !')
            return render_template('contact.html', form=form)
        else:
            msg = Message(form.subject.data, sender='[SENDER EMAIL]', recipients=['[RECIPIENT EMAIL]'])
            msg.body =  """
            From: %s %s <%s>
            %s
            """% (form.firstName.data, form.lastName.data, form.email.data, form.message.data)
            mail.send(msg)
            return render_template('contact.html', success=True)
    elif request.method == 'GET':
            return render_template('contact.html', title='Contact Form', form=form)
    return render_template('contact.html', title='Flask Contact')



@app.route('/name/<string>')
def index(string):
    return '<h1>Contact Form Using Flask !'+string+'</h1>'



if __name__ == '__main__':
    app.run()

0 个答案:

没有答案