お問い合わせフォームのデータをサーバー側のスクリプト(Python)で受け取り、その内容を自分のメールアドレス(Gmail)宛に送信する方法を、自分用に備忘録としてまとめました。
必要なライブラリのインストール
メール送信にはFlask-Mailを使用しました。 以下のコマンドでインストールします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install Flask-Mail |
Flask アプリの設定
- Flask-Mailの設定
Flask アプリでメール送信機能を設定するために、以下のようにします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, redirect, url_for, flash | |
from flask_mail import Mail, Message | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = 'your_secret_key' | |
app.config['MAIL_SERVER'] = 'smtp.example.com' | |
app.config['MAIL_PORT'] = 587 | |
app.config['MAIL_USE_TLS'] = True | |
app.config['MAIL_USERNAME'] = 'your-email@example.com' | |
app.config['MAIL_PASSWORD'] = 'your-email-password' |
Gmailを使用する場合の各種設定
- MAIL_SERVER: http://smtp.gmail.com/
- MAIL_PASSWORD:
① Googleアカウントにログインします。
② 「セキュリティ」セクションを開きます。
③ 「アプリパスワード」を見つけます。 ※このオプションを利用するためには、2段階認証を有効にしている必要があります。
④ 「アプリパスワードの生成」を選び、アプリケーションとして「メール」を選択し、デバイスとして「その他」を選び、カスタム名を入力します。 表示されるパスワードを安全に保存し、MAIL_PASSWORDとして使用します。 - SECRET_KEY:
Pythonのos.urandom()関数を使用して、ランダムなキーを生成することにしました。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
secret_key = os.urandom(24) |
この生成された値をSECRET_KEYとして設定します。
2. お問い合わせフォームのデータを受け取り、メールで送信
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, redirect, url_for | |
from flask_mail import Mail, Message | |
app = Flask(__name__) | |
app.config.from_pyfile('yourconfig.cfg') | |
mail = Mail(app) | |
@app.route('/submit_contact', methods=['POST']) | |
def submit_contact(): | |
name = request.form['name'] | |
email = request.form['email'] | |
message = request.form['message'] | |
msg = Message("お問い合わせ", | |
sender=email, | |
recipients=["your-email@gmail.com"], | |
body=f"名前: {name}\nEメール: {email}\nメッセージ: {message}") | |
mail.send(msg) | |
flash('お問い合わせありがとうございます。', 'success') | |
return redirect(url_for('contact')) | |
if __name__ == '__main__': | |
app.run(debug=True) |
3. ローカル環境でのテスト
ターミナルで以下のコマンドを実行して、ローカルで簡易メールサーバーを起動します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python -m smtpd -n -c DebuggingServer localhost:1025 |
これにより、localhostの1025ポートでメールサーバーが起動します。
次に、Flaskアプリケーションの設定で、メール送信用の設定をこのサーバーに向けて変更します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.config['MAIL_SERVER'] = 'localhost' | |
app.config['MAIL_PORT'] = 1025 | |
app.config['MAIL_USE_TLS'] = False | |
app.config['MAIL_USE_SSL'] = False | |
app.config['MAIL_USERNAME'] = None # ユーザー名は不要 | |
app.config['MAIL_PASSWORD'] = None # パスワードも不要 |
この設定で、送信されたメールはコマンドラインに出力されますが、実際にはインターネットを介して送信されません。
テスト
Flaskアプリケーションを実行し、お問い合わせフォームからデータを送信してみます。
送信が成功すれば、起動しているSMTPデバッグサーバーのコンソールに送信されたメールの内容が表示されるはずです。
4. 本番用の設定にする
ローカル環境でのテスト用に変更した部分を本番用に設定し直してください。
おわり