Generating Emails from Media in Data Table:

What I’m trying to do:
I have built out a form system that generates a stylized excel spreadsheet and saves it to a Data Table as a Media object. I have enabled the email function and I can’t figure out how to send an email with the excel document attached when it is created.

What I’ve tried and what’s not working:
I have been reading the documentation and have tried a few various ideas. Below is my most recent attempt.

Code Sample:

 anvil.email.send(
    to="testemail@gmail.com",
    subject="Anvil Test",
    attachments=[app_tables.table_1(doc=anvil.media.from_file(file_name))]
    ) ``` 

Clone link:
https://anvil.works/build#clone:QP2WDDDHMQUFKMHV=SAHQFW4SWERSYJXSBRWDKK4Z

Thanks in advance for any help with this!

attachments is supposed to be a list of Media objects. What you’ve given it isn’t a Media object. I would have expected something like:

row = app_tables.table_1.get(id=somevariablethathastheidinit)
media = row['columnnameforthemediaobject']

anvil.email.send(
    to="testemail@gmail.com",
    subject="Anvil Test",
    attachments=[media]
    ) 

In your case, since you generate the media object in that same function, you could save that into a variable, use that variable for both adding the row and sending the email.

3 Likes

@jshaffstall,

Thanks so much! I finally got it with your help. What was throwing me off is that I wasn’t able to search for the media object by name, because the Data Table sees this from the CSV export:

ID doc
[261947,447125991] #MEDIA
[261947,447126085] #MEDIA
[261947,447135613] #MEDIA
[261947,447136153] #MEDIA

Since I was trying to .get based on the file name variable (which is what the excel document gets named), it was failing. I added another text column to the Data Table that is generated from a different variable and now it works with this:

row = app_tables.table_1.get(project=project_name_stripped)
    media = row['doc']
    
    
    anvil.email.send(
    to="test@test.com",
    subject="Anvil Test",
    attachments=[media]
    )

This is what the Data Table CSV export looks like now:

ID doc project
[261947,447168210] #MEDIA test
[261947,447168641] #MEDIA 123
1 Like