フォームの内容確認をGmailで送ろうとしているのですが
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
このような文面が実行すると出ます
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- ▲▲機能を動作するようにする
発生している問題・エラーメッセージ
PowerShell
1[2023-01-06 19:30:51,183] ERROR in app: Exception on /contact/complete [POST] 2Traceback (most recent call last): 3 File "C:\Users\tsuch\venv\Lib\site-packages\flask_mail.py", line 492, in send 4 message.send(connection) 5 File "C:\Users\tsuch\venv\Lib\site-packages\flask_mail.py", line 427, in send 6 connection.send(self) 7 File "C:\Users\tsuch\venv\Lib\site-packages\flask_mail.py", line 177, in send 8 assert message.sender, ( 9AssertionError: The message does not specify a sender and a default sender has not been configured 10 11During handling of the above exception, another exception occurred: 12 13Traceback (most recent call last): 14 File "C:\Users\tsuch\venv\Lib\site-packages\flask\app.py", line 2525, in wsgi_app 15 response = self.full_dispatch_request() 16 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 17 File "C:\Users\tsuch\venv\Lib\site-packages\flask\app.py", line 1822, in full_dispatch_request 18 rv = self.handle_user_exception(e) 19 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 File "C:\Users\tsuch\venv\Lib\site-packages\flask\app.py", line 1820, in full_dispatch_request 21 rv = self.dispatch_request() 22 ^^^^^^^^^^^^^^^^^^^^^^^ 23 File "C:\Users\tsuch\venv\Lib\site-packages\flask\app.py", line 1796, in dispatch_request 24 return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) 25 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 26 File "C:\Users\tsuch\flaskbook\apps\minimalapp\app.py", line 109, in contact_complete 27 send_email( 28 File "C:\Users\tsuch\flaskbook\apps\minimalapp\app.py", line 129, in send_email 29 mail.send(msg) 30 File "C:\Users\tsuch\venv\Lib\site-packages\flask_mail.py", line 491, in send 31 with self.connect() as connection: 32 File "C:\Users\tsuch\venv\Lib\site-packages\flask_mail.py", line 152, in __exit__ 33 self.host.quit() 34 File "C:\Users\tsuch\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", line 1004, in quit 35 res = self.docmd("quit") 36 ^^^^^^^^^^^^^^^^^^ 37 File "C:\Users\tsuch\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", line 431, in docmd 38 self.putcmd(cmd, args) 39 File "C:\Users\tsuch\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", line 378, in putcmd 40 self.send(f'{s}{CRLF}') 41 File "C:\Users\tsuch\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", line 365, in send 42 raise SMTPServerDisconnected('please run connect() first') 43smtplib.SMTPServerDisconnected: please run connect() first 44127.0.0.1 - - [06/Jan/2023 19:30:51] "POST /contact/complete HTTP/1.1" 500 - 45
該当のソースコード
python
1# loggingをインポート 2import logging 3import os 4 5from email_validator import EmailNotValidError, validate_email 6 7# Flaskクラスをインポート 8from flask import ( 9 Flask, 10 current_app, 11 flash, 12 g, 13 redirect, 14 render_template, 15 request, 16 url_for, 17) 18from flask_debugtoolbar import DebugToolbarExtension 19from flask_mail import Mail, Message 20 21# Flaskクラスをインスタンス化 22app = Flask(__name__) 23# secret_keyの追加 24app.config["SECRET_KEY"] = "2AZSMss3p5QPbcY2hBsJ" 25# ログレベルの設定 26app.logger.setLevel(logging.DEBUG) 27 28app.logger.debug("DEBUG") 29app.logger.info("INFO") 30app.logger.warning("WARNING") 31app.logger.error("ERROR") 32app.logger.critical("CRITICAL") 33 34 35app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = False 36toolbor = DebugToolbarExtension(app) 37 38# URLと実行する関数をマッピング 39@app.route("/") 40def index(): 41 return "Hello,Flaskbook!" 42 43 44@app.route("/hello/<name>", methods=["GET", "POST"], endpoint="hello-endpoint") 45def hello(name): 46 return f"Hello, {name}!" 47 48 49@app.route("/name/<name>") 50def show_name(name): 51 return render_template("index.html", name=name) 52 53 54with app.test_request_context(): 55 # / 56 print(url_for("index")) 57 # /hello/world 58 print(url_for("hello-endpoint", name="world")) 59 # /name/Rikuo?page=1 60 print(url_for("show_name", name="Rikuo", page="1")) 61 62ctx = app.app_context() 63ctx.push() 64 65print(current_app.name) 66 67g.connection = "connection" 68print(g.connection) 69 70with app.test_request_context("/users?update=ture"): 71 print(request.args.get("updated")) 72 73 74@app.route("/contact") 75def contact(): 76 return render_template("contact.html") 77 78 79@app.route("/contact/complete", methods=["GET", "POST"]) 80def contact_complete(): 81 if request.method == "POST": 82 username = request.form["username"] 83 email = request.form["email"] 84 description = request.form["description"] 85 is_valid = True 86 87 if not username: 88 flash("ユーザ名は必須です") 89 is_valid = False 90 91 if not email: 92 flash("メールアドレスは必須です") 93 is_valid = False 94 95 try: 96 validate_email(email) 97 except EmailNotValidError: 98 flash("メールアドレスの形式で入力してください") 99 is_valid = False 100 101 if not description: 102 flash("問い合わせ内容は必須です") 103 is_valid = False 104 105 if not is_valid: 106 return redirect(url_for("contact")) 107 108 # メールを送る 109 send_email( 110 email, 111 "お問い合わせありがとうございました。", 112 "contact_mail", 113 username=username, 114 description=description, 115 ) 116 117 flash("問い合わせ内容はメールにて送信しました。問い合わせありがとうございます。") 118 119 return redirect(url_for("contact_complete")) 120 121 return render_template("contact_complete.html") 122 123 124def send_email(to, subject, template, **kwargs): 125 """メールを送信する関数""" 126 msg = Message(subject, recipients=[to]) 127 msg.body = render_template(template + ".txt", **kwargs) 128 msg.html = render_template(template + ".html", **kwargs) 129 mail.send(msg) 130 131 132# Mailクラスのコンフィグを追加 133app.config["MAIL_SERVER"] = os.environ.get("MAIL_SERVER") 134app.config["MAIL_PORT"] = os.environ.get("MAIL_PORT") 135app.config["MAIL_USE_TLS"] = os.environ.get("MAIL_USE_TLS") 136app.config["MAIL_USERNAME"] = os.environ.get("MAIL_USERNAME") 137app.config["MAIL_PASSWORD"] = os.environ.get("MAIL_PASSWORD") 138app.config["MAIL_DEFAULT_SENDER"] = os.environ.get("MAIL_DEFAULT_SENDER") 139 140mail = Mail(app) 141
試したこと
何もできませんでした
補足情報(FW/ツールのバージョンなど)
特になし
何かわからないことありましたらコメントお願いします

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/06 15:35