Day 7 of the Anvil Advent Calendar

Build a web app every day until Christmas, with nothing but Python!

Add to your Wish List by Email

Earlier this month, we shared a wishlist app to let you list and track the presents you want for Christmas. But what if you want to add items to your wishlist on the go, without opening the app and logging in? Well, now we have an updated version that lets you add new gift ideas to the list, by simply sending a quick email.

All you have to do is send an email to newgift@<app-name-here>.anvil.app and put the description of the gift in the subject line. You can also add an image attachment!

Find our example version of the app here:

https://wishlist-add-by-email.anvil.app

How it works

The first step was to create a new server function, and decorate it with @anvil.email.handle_message() so that it gets called whenever we email the app. Next, we add the parameter require_dkim=True to the decorator, so that Anvil will check whether the incoming email is DKIM signed (which gives us a good emasure of confidence that the email in question wasn’t spoofed).

Then, in the body of the function, we check that the email address belongs to someone who’s a registered user of the app - which, in this case will only be you, the owner. The description of your new wishlist item item will be set to the subject line of the email, and if there are any attachements, the image for the item will be set as the first attachment.

@anvil.email.handle_message(require_dkim=True)
def newgift_email(msg):
  if app_tables.users.get(email=msg.envelope.from_address) is not None:
    attachments = list(msg.inline_attachments.values()) + msg.attachments
    if attachments:
      app_tables.gifts.add_row(description=msg.subject, photo=attachments[0])
    else:
      app_tables.gifts.add_row(description=msg.subject)

Learn more about using Anvil’s email service here.

Clone the app to check out the source code in more detail, play around with the app, or set up your own wishlist!


Give the Gift of Python

Share this post: