在Ubuntu服务器上使用Flask与FlaskMail和Postfix的BadHeaderError

时间:2015-12-23 22:14:05

标签: python email flask

我正在使用Flask和SQLite运行一个fullstack网站。在页面上,我有一个联系部分,让访问者给我发电子邮件。联系部分包含一个HTML表单,它通过HTTP POST将信息发送到Web服务器。服务器本身使用FlaskMail插件创建消息并将其发送到邮件服务器(在这种情况下是我在同一服务器上设置的后缀服务器)。 但是,每次单击“提交”时,我都会收到BadHeaderError。 我已经查找了现有的修补程序并找到了现有的Stackoverflow问题here。但是,解决方案似乎与我的问题无关。

来自webserver.py(Flask python文件)的相关代码:

app = Flask(__name__)
app.debug = True

app.config['MAIL_USERNAME'] = 'admin'
app.config['MAIL_PASSWORD'] = 'password'
app.config['MAIL_USE_TLS'] = True

mail = Mail(app)

@app.route('/contact', methods=['GET', 'POST'])
def contact():
    lang = get_lang(request.headers)

    if request.method == 'GET':
            if lang == 'de':
                    return render_template('contact.html', lang=lang, title=title_de, descriptio$
            else:
                    return render_template('contact.html', lang=lang, title=title_en, descriptio$
    elif request.method == 'POST':
            name = request.form['name']
            email = request.form['email']
            company = request.form['company']
            message = request.form['message']

            if not name:
                    flash('Please enter your name')
                    return redirect(url_for('contact'))
            if not email:
                    flash('Please enter your e-mail address')
                    return redirect(url_for('contact'))
            if not message:
                    flash('Please enter a message')
                    return redirect(url_for('contact'))

            message += '\n\n'
            message += ('Name: ' + name)
            message += '\n\n'
            message += ('Company: ' + company)

            msg = Message(message, sender=email, recipients=['pawelczyk.johannes@gmail.com'])

            mail.send(msg)

            flash('You will receive an automatic confirmation mail')
            return redirect(url_for('index'))
    else:
            flash('Bad request')
            return redirect(url_for('index'))

Apache错误日志:pastebin

HTML:

<div id="contact-form-container">
    <form id="contact-form" action="{{ url_for('contact') }}" method="post">
        <div>
            <div class="contact-form-row">
                <h3 class="to-left">Name:</h3>
                <input class="to-right" type="text" name="name" placeholder="Name">
            </div>
            <div class="contact-form-row">
                <h3 class="to-left">E-Mail:</h3>
                <input class="to-right" type="text" name="email" placeholder="E-Mail">
            </div>
            <div class="contact-form-row">
                <h3 class="to-left">Firma (optional):</h3>
                <input class="to-right" type="text" name="company" placeholder="Firma">
            </div>
            <h3 class="gap-over">Ihre Nachricht:</h3>
            <textarea cols="40" rows="10" name="message"></textarea></br>
        </div>
        <div class="middle">
            <input class="button" type="submit" value="Senden">
        </div>
    </form>
</div>

0 个答案:

没有答案