Streamlitでメール送信 - matutosi/support-ac GitHub Wiki
元ネタ https://github.com/tonykipkemboi/streamlit-smtp-test
準備
ライブラリのインストール
pip install streamlit
pip install sendgrid
アカウントとパスワードはコード内には書かず.別に保存する.
ただし,リポジトリ自体を公開していたら,ファイルの内容も公開されるので,private に設定する.
st.secrets["gmail"]
で取り出し可能.
# ./streamlit/secrets.toml
gmail = "[email protected]"
password = "hogehoge_password"
gmailで2段階認証を設定しているときは,アプリパスワードを生成する.
https://myaccount.google.com/apppasswords
Stremalit起動
"""
# send_mail_web.py
# streamlit run send_mail_web.py
メールの送信スクリプト
"""
import streamlit as st
import smtplib
from email.mime.text import MIMEText
st.title('Send Streamlit SMTP Email 💌 🚀')
# Taking inputs
email_sender = st.secrets["gmail"]
password = st.secrets["password"]
email_receiver = st.text_input('To', "[email protected]")
subject = st.text_input('Subject', "test")
body = st.text_area('Body', "test mail")
if st.button("Send Email"):
try:
msg = MIMEText(body)
msg['From'] = email_sender
msg['To'] = email_receiver
msg['Subject'] = subject
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_sender, password)
server.sendmail(email_sender, email_receiver, msg.as_string())
server.quit()
st.success('Email sent successfully! 🚀')
except Exception as e:
st.error(f"Failed to send email: {e}")