JBHF's picture
Update app.py
c774407 verified
# app.py 07-04-2024
# https://discuss.streamlit.io/t/send-email-with-smtp-and-gmail-address/48145
# https://github.com/tonykipkemboi/streamlit-smtp-test
# streamlit-smtp-test/streamlit_app.py at main Β· tonykipkemboi/streamlit-smtp-test
# https://github.com/tonykipkemboi/streamlit-smtp-test/blob/main/streamlit_app.
#
# SENT EMAILS TO: [email protected]
# from dotenv import load_dotenv
# load_dotenv()
# genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# ZIE OOK:
# https://realpython.com/python-send-email/#adding-attachments-using-the-email-package
#
# https://www.saleshandy.com/smtp/gmail-smtp-settings/
"""
If you've two-step verification enabled, your regular password won't work. Instead, generate an app-specific password:
- Go to your Google Account.
- On the left navigation panel, click on "Security."
- Under "Signing in to Google," select "App Passwords." You might need to sign in again.
- At the bottom, choose the app and device you want the app password for, then select "Generate."
- Use this app password in your Streamlit app.
"""
import streamlit as st
import smtplib
import os
from email.mime.text import MIMEText
st.title('Send Streamlit SMTP Email πŸ’Œ πŸš€')
password = os.getenv("scitechtalk_gmail")
# password = "STT301256!"
# st.write("password:", password)
# -------------------------------------------------------------------------------------------------------------
# POE WEB-SEARCH
# https://poe.com/s/DoBILpU4zpkFaMdOpMPH
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, sender, recipients, password):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ', '.join(recipients)
# with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server:
# with smtplib.SMTP_SSL('smtp.gmail.com', 587) as smtp_server:
with smtplib.SMTP_SSL('smtp.gmail.com', 25) as smtp_server:
smtp_server.login(sender, password)
smtp_server.sendmail(sender, recipients, msg.as_string())
print("Email sent successfully!")
# Set the email details
subject = "Test Email"
body = "This is a test email sent via Python"
sender = "[email protected]"
# recipients = ["[email protected]", "[email protected]"] # ORIGINAL
recipients = ["[email protected]"] # JB
# password = "your_password"
# Call the send_email function
send_email(subject, body, sender, recipients, password)
# -------------------------------------------------------------------------------------------------------------
import smtplib, ssl
port = 587 # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "[email protected]"
receiver_email = "[email protected]"
# password = input("Type your password and press enter:") # ORIGINAL
# st.text_input
password = st.text_input("Type your password and press enter:") # JB
message = """\
Subject: Hi there
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
# -------------------------------------------------------------------------------------------------------------
st.markdown("""
**Enter your email, subject, and email body then hit send to receive an email from `[email protected]`!**
""")
# Taking inputs
email_sender = st.text_input('From', '[email protected]', disabled=True)
# email_receiver = st.text_input('To') # ORIGINAL
email_receiver = "[email protected]" # st.text_input('To') # JB
subject = st.text_input('Subject')
body = st.text_area('Body')
# Hide the password input
# password = st.text_input('Password', type="password", disabled=True) # ORIGINAL
# secret: scitechtalk_gmail
password = os.getenv("scitechtalk_gmail")
# password = "STT301256!"
st.write("password:", password)
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()
st.write("server.login(...) :")
# https://docs.streamlit.io/deploy/streamlit-community-cloud/deploy-your-app/secrets-management
# server.login(st.secrets["email"]["gmail"], st.secrets["email"]["password"]) # ORIGINAL
server.login(st.secrets["email"]["gmail"], st.secrets["email"]["password"]) # JB
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}")