File size: 4,801 Bytes
d179ce0 90e5735 8bedb56 71d8cca 8bedb56 d179ce0 abceb45 d179ce0 03840a7 c774407 03840a7 71d8cca 8d0673b 71d8cca 03840a7 8bedb56 baca08b 8bedb56 d179ce0 f34e741 abceb45 90e5735 d179ce0 8bedb56 90e5735 8bedb56 d179ce0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# 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}") |