St.file_uploader and decoding image files

Hello, I was making a bug report function using redmail library.

This is the library I was using-

Also I wanted to use st.file_uploader() and include the photos in my email.
But I cannot decode the uploaded file into usable bytes(?).
Can you try my code and see what is wrong in here?

import streamlit as st
from redmail import gmail
import base64

if 'send_email' not in st.session_state:
    st.session_state.send_email=False
if 'username' not in st.session_state:
    st.session_state.username='Johnny'

st.subheader('Bug Report')

st.write("""
**Plz report any bugs bro**
            
**I wanna fix it bro**
""")

# Taking inputs

error_subject = st.text_input('Title')
error_body = st.text_area('Context')
error_image=st.file_uploader('Photos')

col1,col2=st.columns([8,2])
with col2:
    if st.button("Send Email",use_container_width=True):
        st.session_state.send_email=True
if st.session_state.send_email==True:
    try:
        base64_str = base64.b64encode(error_image.read())
        imgdata = base64.b64decode(base64_str)
        subtype_name=error_image.name[error_image.name.find('.')+1:]      
        gmail.username=st.secrets.admin_email
        gmail.password=st.secrets.admin_pw
        gmail.send(
            subject=f'{error_subject}',
            sender=f'{st.secrets.admin_email}',
            receivers=[f'{st.secrets.bug_report_email}'],
            html=f'''
<h5>user : {st.session_state.username}</h5>
<p>{error_body}</p>
{{myimage}}
            ''',
            body_images={
            'myimage':f'{imgdata}',
            'subtype':f'{subtype_name}'
            }
)
        st.success('Email sent successfully! 🚀')
        del st.session_state.send_email
    except Exception as e:
        st.error(f"Failed to send email: {e}")
else:
    pass

I guess f'''{{my_image}}''' doesn’t do what you think it does.

@Goyo I see. Can you tell me what format an image file is converted to when it is uploaded via st.file_uploader()? Maybe stupid question but I started learning CS quite recently.

There is no conversion. You get the very same bytes that are in the file.

It looks like {{myimage}} conforms to some template language but I don’t know what format or encoding is expected there.

In any case, note that applying base64.b64encode and then base64.b64decode is a no-op.

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