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.
Log in to Anvil and click ‘New Blank App’. Choose the Material Design theme.
In the App Browser, click the + next to Services.
You’ll see a list of available services and integrations. Click on Email.
In the App Browser, click the + next to Server Modules to add a new Server Module.
You will see a code editor with a yellow background.
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.", )
Go to the code for Form1. It looks like this:
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.
Now click the ‘Run’ button at the top of the screen.
Check your email - you will have an email 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.raw_value msg.reply("Your email was sent to %s and received successfully." % to_address)
At the bottom of the Server Module, there is a message like this:
Any emails sent to any address ending in
be receieved by my app - your app will have a similar but different address.
Open your favourite email client and send an email to
<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.)
Click on the button below to clone a finished version of this app into your account.
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.