Attachments incoming mail missing

Hi all, I’m trying to store the attachments (xlsx and csv) attached to incoming mails using the example provided at Anvil Docs | Sending and Receiving.

This is the code I’m using:

@anvil.email.handle_message
def handle_incoming_emails(msg):

  msg_row = app_tables.received_messages.add_row(
              from_addr=msg.envelope.from_address, 
              to=msg.envelope.recipient,
              text=msg.text, 
              html=msg.html
            )

  for a in msg.attachments:
    
    app_tables.attachments.add_row(
      message=msg_row, 
      attachment=a
    )

Somehow msg.attachments is empty.

I’ve also tried to use inline_attachments using this code:

  for a in msg.inline_attachments:
    
    app_tables.attachments.add_row(
      message=msg_row, 
      attachment=a
    )

The result of this is an error in the logs:

The type and contents of “a” using print(type(a)):

image

To be sure I also sent the mail to another mail address and the file was attached to the email.

Does anyone have an idea why the attachments are not there?

Looking at the API docs, it looks like inline_attachments works a little differently from attachments:


This should work:

for header, a in msg.inline_attachments.items():
    app_tables.attachments.add_row(
      message=msg_row,
      attachment=a
    )
2 Likes

Thanks Tim, that solved the problem.

I’ll pay more attention to the docs next time.

2 Likes