Issue
I am building a website with Python and Flask and I built a Contact Form Modal. The Modal does open up and i can fill it (thanks to the community <3), but i get 500 Internal Server Error once i submit.
from flask import Flask, render_template, send_from_directory, request
from flask_mail import Mail, Message
from config import sender_mail_username, sender_mail_password, recipient_mail_username
app = Flask(__name__)
mail = Mail(app)
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = sender_mail_username
app.config["MAIL_PASSWORD"] = sender_mail_password
@app.route("/")
def home():
return render_template("home.html")
@app.route("/", methods=["GET", "POST"])
def contact():
if request.method == "POST":
email = request.form.get("email")
subject = request.form.get("subject")
message = request.form.get("message")
msg = Message(sender=sender_mail_username, recipients=recipient_mail_username,
subject="UNIFINITY Contact Form submission", body=f"Email {email}\nSubject: {subject}"
f"\n\n{message}")
mail.send(msg)
return render_template("sent.html")
if __name__ == "__main__":
app.run()
I do use the method "POST"
<form name="contact" role="form" method="POST">
And the send-button type is on submit
<button type="submit" class="btn btn-dark">Send</button>
EDIT: This is the Traceback:
Traceback (most recent call last):
File "/Users/Jules/Documents/IT/Pycharm/UNIFINITY Webpage/webpage.py", line 35, in contact
mail.send(msg)
File "/Users/Jules/Library/Python/3.9/lib/python/site-packages/flask_mail.py", line 491, in send
with self.connect() as connection:
File "/Users/Jules/Library/Python/3.9/lib/python/site-packages/flask_mail.py", line 144, in __enter__
self.host = self.configure_host()
File "/Users/Jules/Library/Python/3.9/lib/python/site-packages/flask_mail.py", line 158, in configure_host
host = smtplib.SMTP(self.mail.server, self.mail.port)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/socket.py", line 843, in create_connection
raise err
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/socket.py", line 831, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
[2023-12-05 09:31:58,550] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "/Library/Python/3.9/site-packages/flask/app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/3.9/site-packages/flask/app.py", line 870, in full_dispatch_request
return self.finalize_request(rv)
File "/Library/Python/3.9/site-packages/flask/app.py", line 889, in finalize_request
response = self.make_response(rv)
File "/Library/Python/3.9/site-packages/flask/app.py", line 1161, in make_response
raise TypeError(
TypeError: The view function for 'contact' did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [05/Dec/2023 09:31:58] "POST / HTTP/1.1" 500 -
I've tried the SMTP Test through https://www.gmass.co/smtp-test and I can send e-mails. So i figure it must be an coding mistake somewhere. As seen on the flask_mail documentation i figured the sender= parameter has to be a list, is that correct?
Thank you for your inputs <3
Solution
So the issue was already dealt with here
mail = Mail(app)
should come after
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = sender_mail_username
app.config["MAIL_PASSWORD"] = sender_mail_password
Answered By - codian40V3
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.