# 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: scitechtalk@gmail.com # 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 """ 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 ๐Ÿ’Œ ๐Ÿš€') # ------------------------------------------------------------------------------------------------------------- import smtplib, ssl port = 587 # For starttls smtp_server = "smtp.gmail.com" sender_email = "my@gmail.com" receiver_email = "scitechtalk@gmail.com" password = input("Type your password and press enter:") 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 `summittradingcard@gmail.com`!** """) # Taking inputs email_sender = st.text_input('From', 'summittradingcard@gmail.com', disabled=True) # email_receiver = st.text_input('To') # ORIGINAL email_receiver = "scitechtalk@gmail.com" # 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}")