2

I'm running a fullstack website with Flask and SQLite. On the page I have a contact-section to let visitors send me an email. The contact-section consists of an HTML form which sends the information to the webserver via HTTP POST. The server itself uses the FlaskMail plugin to create the message and send it to the mail-server (in this case a postfix server I set up on the same server). However, I'm getting a BadHeaderError every time I click submit. I already looked for existing fixes and found an existing Stackoverflow question here. However, the solution there seems to be unrelated to my issue.

Relevant code from webserver.py (Flask python file):

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 error log: 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>
Community
  • 1
  • 1
J. Pawelczyk
  • 383
  • 1
  • 3
  • 8
  • Do you get this error when you try to send email without Flask/through your form, or whenever you try to send any emails? Also, can you enable debug mode and get a traceback from the app? In general I'd suggest against using your own mail server as well, it is very hard to manage and ensure you're not getting caught by spam. – Ryan O'Donnell Dec 23 '15 at 23:37
  • You got a fair point. I'm gonna try set it up using a gmail server (or similar). – J. Pawelczyk Dec 23 '15 at 23:58
  • Using the Gmail SMTP server, I still get the same BadHeaderError ... – J. Pawelczyk Dec 24 '15 at 00:16
  • 4
    Fixed it. Derp. Got confused with the Message class. The first argument to the Message constructor is the subject, while one has to specify the actual content of the message by changing the value of the msg.body variable. – J. Pawelczyk Dec 24 '15 at 00:28
  • 1
    Good catch! Yeah you're not allowed newlines in subjects. Something to remember! – Ryan O'Donnell Dec 24 '15 at 04:03

0 Answers0