How to make a webapp/server that can read & react to emails

In the last post, we talked about how to teach your Google Home device (or Google Assistant on your Android phone) to send emails.

But that itself is not too interesting (unless you really enjoy spamming yourself). What would be more useful is if you create a program to read and act on the emails automatically. Because, then, you have the ingredients for building your own Virtual Assistant.

The simplest (and cheapest way) I found to do this is using Google App Engine. It has a generous daily free quota. For all my home automation projects, I haven’t run above the free limit.

Start by following the “hello world” instructions to make your first web app:
https://cloud.google.com/appengine/docs/standard/python/quickstart

Then, add the files/code below to enhance your web app to handle emails. (Note: the code I provided below is an example of how I use email to control my home sprinkler system. This lets me say “Turn on sprinkler 1” to my Google Home Mini, which triggers IFTTT to send an email to my web app, which reads the email and triggers my home sprinkler system to turn on zone 1.)

app.yaml:

receive_mail.py:

handlers_mail.py:

After you set up the above, you can send emails to your Google App Engine web app using its special address:
this_can_be_anything@your_appengine_application_name.appspotmail.com

Now that you have a web app, there’s much more you can do beyond handling emails. It opens up a couple more opportunities for your home automation. E.g.:

  • Use this web app as a trusted HTTP server that your smart devices can fetch instructions from
  • Use this web app to serve a web GUI for your devices/projects
  • Use this web app (which supports HTTPS) to authenticate users and securely control your smart devices

Give it a go. I think the code I gave you above should work. If I missed something, please let me know at aaron@secretsciencelab.com. Happy hacking!

How to send emails with Google Assistant / Google Home

I got a Google Home Mini for $19 last Black Friday, and my family has been having fun with it. My kids like to ask it questions and play music on it. I’ve set it up with voice shortcuts so my kids can say, “Call mom” or “Call dad” to phone me from the Google Home itself (in case of an emergency). It works as a regular Bluetooth speaker and it also knows many useless party tricks.

But most interesting to me is I now have a natural language voice interface for all my projects! With the Google Home / Google Assistant, I can control things with my voice, like how Tony Stark talks to J.A.R.V.I.S. And as someone who was born before the Internet, it is amazing to me that I can do this with a $19 device that sits on my kitchen counter.

But what good is an “Assistant” if it can’t send emails? Not very, if you ask me. So in this guide, I’m going to share with you how I set up my Google Home Mini to send emails. By the way, this works for any Google Assistant interface (like on your Android phone), so it works even if you don’t have a Google Home.

To send emails with Google Home, there are 2 ingredients: IFTTT and Gmail. I’ll assume you have a Gmail account, so we’ll just focus on setting up IFTTT.

IFTTT is a free service at ifttt.com. IFTTT stands for “If this, then that.” Or more specifically, “If x happens, then do y.” IFTTT hooks up with all kinds of standards and protocols and services so that you can replace the x and y with anything.

Some example IFTTT “recipes” are:
If it is dark outside, then turn on the lights.
If it is going to rain tomorrow, then disable the sprinklers.
If the temperature drops below ___, then set thermostat to ___.
If you tell Google Home “Email Joe and say ___”, then send gmail to joe@joe.com with ___ .

I’m going to explain how to do that last “recipe”. It’s not that hard, and just a matter of setting up IFTTT.

1) Make a new “If this then that” applet on ifttt.com. Google Assistant will be the “this” part of the recipe and Gmail will be the “that” part.
2) Connect Google Assistant to your IFTTT account. You can program it to trigger an action when you speak a special phrase.
3) Connect a Gmail account to your IFTTT account. It does not have to be your main Gmail account. You can make a new account just for IFTTT mails. When you say your special phrase to Google Assistant, you can program it to send a special email.
(More detailed instructions here.)

Next, I’ll show you how to configure a Google App Engine web app to receive emails. This setup can be used to relay information to your smart devices or control them for free. (I’ll be using it to control my home sprinkler system.)