Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at contact@anvil.works.

Quickstart: Email

Communicate to and from your app by email

Your Anvil app can send and receive emails.

Follow this quickstart to create an app that sends you an email, and auto-replies to emails you send it.

Create an app

Log in to Anvil and click ‘create app’. Choose the Material Design theme.

Location of the Create App button

Add the Email Service

In the App Browser, click the + next to Services.

App Browser showing the plus next to Services

You’ll see a list of available services and integrations. Click on Email.

Services list with Email highlighted

Add a Server Module

In the App Browser, click the + next to Server Modules to add a new Server Module.

Adding a Server Module in the App Browser

You will see a code editor with a yellow background.

Python code with a yellow background

Send an email from your app

Write this function into the Server Module:

@anvil.server.callable
def send_email(address):
    anvil.email.send(
      from_name="Anvil Forum",
      to=address,
      subject="Have you used the Anvil Forum?",
      html='The Anvil <a href="https://anvil.works/forum">Forum</a> is friendly and informative.',
      text="The Anvil Forum (https://anvil.works/forum) is friendly and informative.",
    )

Call your server function from the client

Go to the code for Form1. It looks like this:

Code View for Form1

At the end of the __init__ method, write these lines (replace the <your email address> with your actual email address):

    email_address = "<your email address>"
    anvil.server.call('send_email', email_address)

This means your function will run when the app starts.

Run your app

Now click the ‘Run’ button at the top of the screen.

Running your app in the editor

Check your email - you will have an email from your app.

My email client showing an email successfully received from my app

Receive emails from your app

Stop your app and go to your Server Module.

Write this function:

@anvil.email.handle_message
def message_handler(msg):
  to_address = msg.addressees.to_addresses[0].raw_value
  msg.reply("Your email was sent to %s and received successfully." % to_address)

Send your app an email

At the bottom of the Server Module, there is a message like this:

Message showing the email address associated with your app

Any emails sent to any address ending in @ahd2umld63ixj6o6.anvil.app will be receieved by my app - your app will have a similar but different address.

Open your favourite email client and send an email to hey-it-works@<your-app-id>.anvil.app, where <your-app-id> would be ahd2umld63ixj6o6 in my case.

You should receive a reply containing the text Your email was sent to hey-it-works@<your-app-id>.anvil.app and receieved successfully.

(To set up a nicer email address for your app, see deployment.)

Copy the example app

Click on the button below to clone a finished version of this app into your account.

Next up

Want more depth on this subject?

Read more about Sending and receiving email in Anvil

Want another quickstart?

Every quickstart is on the Quickstarts page.