Enrico
Enrico

Reputation: 39

Python/Flask HTML form sending email

I am pretty new to programming and especially to Python/Flask. I am trying to have a HTML form submitting to a designate email address. Been 2 days that I have been trying everything, but have been unsuccessful. When I deploy to heroku everything is fine, but the form doesn't work.

this is my script.py

from flask import Flask, render_template, request, flash
from form import ContactForm
from flask_mail import Mail, Message
import smtplib
from dotenv import load_dotenv   #for python-dotenv method
load_dotenv()
import os

OWN_EMAIL = os.environ.get("OWN_EMAIL")
OWN_PASSWORD = os.environ.get("OWN_PASSWORD")

print(os.environ.get("OWN_EMAIL"))
print(os.environ.get("OWN_PASSWORD"))

app = Flask(__name__)

@app.route('/')
@app.route('/home', methods=["GET", "POST"])
def home():
    if request.method == "POST":
        data = request.form
        data = request.form
        send_email(data["name"], data["email"], data["subject"], data["message"])
        return render_template("landing.html", msg_sent=True)
    return render_template("landing.html", msg_sent=False)


def send_email(name, email, phone, message):
    email_message = f"Subject:New Message\n\nName: {name}\nEmail: {email}\nSubject: {subject}\nMessage:{message}"
    with smtplib.SMTP("smtp.zoho.eu") as connection:
        connection.starttls()
        connection.login(OWN_EMAIL, OWN_PASSWORD)
        connection.sendmail(OWN_EMAIL, OWN_EMAIL, email_message)

if __name__=="__main__":
    app.run(debug=True)

and this is my landing.html

  <div class="col-lg-6">
    <form action="{{ url_for('home') }}" name="sentMessage" method="post" class="contactForm" novalidate>
      <div class="row">

        <div id="sendmessage">Your message has been sent. Thank you!</div>
        <div id="errormessage"></div>

        <div class="col-lg-6">
          <div class="form-group contact-block1">
            <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
            <div class="validation"></div>
          </div>
        </div>

        <div class="col-lg-6">
          <div class="form-group">
            <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
            <div class="validation"></div>
          </div>
        </div>

        <div class="col-lg-12">
          <div class="form-group">
            <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
            <div class="validation"></div>
          </div>
        </div>

        <div class="col-lg-12">
          <div class="form-group">
            <textarea class="form-control" name="message" rows="8" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
            <div class="validation"></div>
          </div>
        </div>

        <div class="col-lg-12">
          <input type="submit" class="btn btn-defeault btn-send" value="Send message">
        </div>

      </div>
    </form>
  </div>

Would really appreciate any input.

Thanks

Upvotes: 2

Views: 2114

Answers (1)

IoaTzimas
IoaTzimas

Reputation: 10624

Remove the first function from your route. It should be:

@app.route('/home')
def home():
    if request.method == "POST":
        data = request.form
        data = request.form
        send_email(data["name"], data["email"], data["subject"], data["message"])
        return render_template("landing.html", msg_sent=True)
    return render_template("landing.html", msg_sent=False)

Also change this line:

<form action="{{ url_for('home') }}"

to this:

<form action="/home" 

inside your html file

Upvotes: 2

Related Questions