Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Diego Fernandez Fontana
Diego Fernandez Fontana
7,202 Points

Recive data from a form in Flask

Hello everyone. I am making a webpage and i am trying to make a contact page with a form. The form has 2 text field and 1 mail field, its like: Name, email and textarea. What i want to know is how could i get the information people post in the form to my email. Any ideas really what should i use WTForms flask-mail? i really have no idea i've been trying for a couple of hours and i couldnt find a soultion. Thanks

1 Answer

Hi Diego

I did this sort of thing for my protfolio website that has contact page which is built in flask.

# contact form

from flask_wtf import Form
from wtforms import TextAreaField,StringField,PasswordField
from wtforms.validators import DataRequired,Email,ValidationError


class ContactForm(Form):
  Message=TextAreaField('Type your message to me',
                    validators=[
                    DataRequired(),

    ]


  )
  email=StringField('Your Email Address',

           validators=[
              DataRequired(),
              Email(),
    ]

             )

# app.py

from flask.ext.mail import Mail,Message
from forms import *


app.config.update(dict(
    DEBUG = True,
    MAIL_SERVER = "smtp.gmail.com",
    MAIL_PORT = 465,
    MAIL_USE_SSL = True,
    MAIL_USERNAME = 'xxxxxxxx@gmail.com',
    MAIL_PASSWORD = 'xxxxxx',


))


mail = Mail(app)

def send_email(to_email,from_email,mydict):
  result_dict = {}
  try:
      msg = Message("Message from your website",sender=from_email,recipients=[to_email])
      msg.body="Email from your website"
      msg.html= render_template("email.html",contact_email=mydict['contact_email'],message=mydict['message'])
      mail.send(msg)
      flash("Thanks for contacting me. I will get back to you soon!","success")
  except Exception as e:
      flash("Error sending email".format(str(e)),"error")



@app.route('/contact',methods=['GET','POST'])
def contact():
  form = ContactForm()
  if form.validate_on_submit():
    result = send_email("example@hotmail.com",form.email.data,'message':form.Message.data,'contact_email':form.email.data})
    return render_template("contact.html",form=form)  
  return render_template("contact.html",form=form)


# watch out for indentation as i have just pasted code.
# I have used gmail smtp and flask mail

hope this helps you

Diego Fernandez Fontana
Diego Fernandez Fontana
7,202 Points

Thank you very much, i will try it in a couple of hours but now i understand how to do it!