How do I unzip a zip file, and attach each file to an email?

What I’m trying to do:

I receive a zip file from a POST http api endpoint. I want to recursively attach each file in the zip to an email

What I’ve tried and what’s not working:

I am struggling with understanding the various formats of media objects within anvil. Every time I think I have found a solution, I get another error. In the below code sample, I tried another approach of first saving each file to a data table, but even that gave me errors.

Code Sample:

def process_email():
  filedata=anvil.server.request.body.get_bytes()
  file=anvil.BlobMedia('application/zip', filedata)
  
  with zipfile.ZipFile(file, 'r') as zip_ref:
        for file_info in zip_ref.infolist():
            if file_info.filename.endswith('.pdf'):
                with zip_ref.open(file_info) as file:
                    # process_pdf(io.BytesIO(file.read()))
                    app_tables.files.add_row(
                        File=file,
                        Description="test name"
                      )

Clone link:
share a copy of your app

As a place to start, which error is the above code getting, and at which line of code?

1 Like