You are currently viewing the new Beta Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the Beta Editor Docs

Sending and Receiving Email Attachments

As well as text and HTML content, email messages can contain binary attachments. Like all other binary data in Anvil, attachments are Media objects.

This makes it easy to store attachments in Data Tables, display them on Image components, accept them from browser uploads, and more.

Sending attachments

To add attachments to an email, pass a list of Media objects to the function:
    text="See files attached",
    attachments=[media1, media2])

Receiving attachments

When you receive an email via, a list of all attachments is available as the attachments property of the Message object:
def incoming_email(msg):
  for attachment in msg.attachments:

Using inline attachments

Normally, an attachment is a file sent alongside an email. But sometimes, you want to use binary within the email body – for example, an image in an HTML email.

To do this, we use inline attachment, and refer to it from the HTML of your email. Each inline attachment has an id, and we can refer to that within an HTML email with a url like cid:my_id_here.

When calling, you can specify inline_attachments as a dictionary whose keys are the (string) IDs and values are (Media) attachments.

When receiving email via, the inline_attachments attribute of the Message object will be a similar dictionary of IDs to data.

Here is a code sample that sends an image as an inline attachment in an HTML email:

def send_picture(picture):
      Here's a picture:<br>
      <img src="cid:mypic"><br>
      Wasn't that cute?
    inline_attachments={'mypic': picture})

And here’s what that email looks like, when opened in GMail:

Screenshot of an HTML email with an image