Send email with SMTP and Gmail address

Hello
I would like my Streamlit application which is in the cloud to be able to send emails. I still have the same error that occurs:

Erreur lors de l’envoi de l’e-mail : (535, b’5.7.8 Username and Password not accepted. Learn more at\n5.7.8 Check Gmail through other email platforms - Gmail Help 6-20020a170902ee4600b001b9c5e07bc3sm3605362plo.238 - gsmtp’)

I followed and looked up what other people were doing. I do like them. I’ve also looked at google’s documentation on this and I’m good at what to do. I think it’s a Streamlit Cloud problem.

My code:
email_sender = st.text_input(‘sender’)
email_receiver = st.text_input(‘receiver’)
subject = st.text_input(‘subject’)
body = st.text_input(‘body’)
password = st.text_input(‘password’)
if st.button(“Send Email”):
try:
connection=smtplib.SMTP(‘smtp.gmail.com’,587)
connection.starttls()
connection.login(email_sender,password)
message=“Subject:{}\n\n”.format(subject,body)
connection.sendmail(email_sender,email_receiver,message)
connection.quit()
st.succes(‘ok’)
except Exception as e:
st.error(f"Erreur lors de l’envoi de l’e-mail : {e}")

The code is not protected at this time. I am still in the testing phase.

Hi @Jumitti,

Thanks for posting!

Here’s modified code that works:

import streamlit as st
import smtplib
from email.mime.text import MIMEText

# Taking inputs
email_sender = st.text_input('From')
email_receiver = st.text_input('To')
subject = st.text_input('Subject')
body = st.text_area('Body')
password = st.text_input('Password', type="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()
        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"Erreur lors de l’envoi de l’e-mail : {e}")

If you’ve two-step verification enabled for your Gmail account, 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.

Hello ! Thanks for your quick answer

Unfortunately it doesn’t work. I’ve tried just about everything I can find on the forums. The python packages are well installed elsewhere. And I don’t have two-factor authentication enabled. I also followed the recommendations of Google

Can you share your requirements.txt file?

It should be like this to work:

streamlit
sendgrid
Pillow

Here’s the example app I built for the demo. Try entering your email, subject, and body then send it to yourself to test. Here’s the repo as well.

Try generating a new email following the instructions from my previous post regardless of if you don’t have 2FA enabled.

'm back ! Sorry for the wait, the weekend…

I fixed the problem with your help. I didn’t have double authentication and I think Google is preventing SMTP connections without double authentication now… I just put double authentication, of course I created a password for the application and everything works perfectly

Thank you very much!

Hi @Jumitti,
I’m glad it worked! Happy Streamlit-ing! :balloon:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.