HTML コンタクトフォーム PHP & MYSQL データベースに保存と管理画面から表示
By:admin admin
Jul 15, 2024 - 16:12
コンタクトフォームにバリデーション機能・セキュリティ対策を組み入れる。
必修事項が未入力の場合や文字数が少ないとき及び多すぎ場合にメッセージを表示させる。メッセージ確認画面・ページをを戻る時に内容を保持する機能を付ける。
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_mail import Mail, Message
# from vars import MAIL_USERNAME, MAIL_PASSWORD
app = Flask(__name__)
app.config['MAIL_SERVER'] = '送信メールサーバー名'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'メールアドレス'
app.config['MAIL_PASSWORD'] = 'パスワード'
# ポート465の場合
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['SECRET_KEY'] = 'my_randam_key'
mail = Mail(app)
@app.route('/')
def index():
return render_template('contact.html')
@app.route('/send_email', methods=['POST'])
def send_email():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
message = request.form['message']
return render_template('output.html', name = name, email = email, message = message)
@app.route('/cfn_email', methods=['POST'])
def cfm_email():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
message = request.form['message']
msg = Message(subject='Contact Us Dorm Submission', sender='me@click-pc.link', recipients=['me@click-pc.link'])
msg.body = f"Name: {name}
Email: {email}
Message: {message}"
mail.send(msg)
flash('Message sent succesfully', 'succes')
return redirect(url_for('index'))
app.run(debug=True)