Chapter 5:
Send Emails
We’ll use the Email Service to send you an email each time someone leaves feedback.
Step 1: Add the Email Service
Let’s start by adding the Email Service to our app. Select the blue ‘+’ button in the sidebar menu to open the list of available services. Then, click on the ‘Email’.
Now you’ve enabled the Email Service, it’s just one line of Python to send an email.
Step 2: Call the send function
Go to your Server Module, and modify your add_feedback
function to look like this. Remember to change the address to your email address! We added this function in Chapter 3.
@anvil.server.callable
def add_feedback(name, email, feedback):
app_tables.feedback.add_row(
name=name,
email=email,
feedback=feedback,
created=datetime.now()
)
# Send yourself an email each time feedback is submitted
anvil.email.send(#to="noreply@anvil.works", # Change this to your email address!
subject=f"Feedback from {name}",
text=f"""
A new person has filled out the feedback form!
Name: {name}
Email address: {email}
Feedback:
{feedback}
""")
Step 3: Publish your app
You can easily publish you app online for anyone to use. Click the ‘Publish’ button at the top right of the editor, then select ‘Publish this app’ and use the public URL provided or enter your own.
Fill in a name, email address, and some feedback and click Submit. You’ll receive an email letting you know someone has filled out your feedback form.
And that’s it. You’ve just built a working database-backed feedback app in Anvil.
You can also use the following link to clone the finished app and explore it yourself:
In the next section, you can challenge yourself to reinforce what you’ve learnt and learn more!
If you’d rather not try the challenge, check out the below section to see what’s next.
What next?
Head to the Anvil Learning Centre for more tutorials, or head to our examples page to see how to build some complex apps in Anvil.
Build Database-Backed Apps
In this tutorial, you’ll learn how to do all of those things and more in Anvil.