Anth's Computer Cave Tutorials

Use IFTTT web requests to send email alerts

Updated 11th November, 2018

In this tutorial I'll show you how to set up an applet on ifttt.com to accept web requests from your Python programs and send you email alerts.

Note: I'm actually using a different method (without IFTTT) for email alerts now, sending directly from Python using the SMTPLIB library. You can check it out that tutorial here, and see which method you prefer.

The steps for setting up ifttt web requests. Picture: Anthony Hartup

IFTTT is a web service and phone app that allows you to customize actions to events.

You can create applets and choose from a series of pre-built triggers, then choose an action to respond to that trigger.

You need to sign up with ifttt.com. If you would like to use other IFTTT features you can also install their phone app, but you don't need it for this tutorial.

You will also need to join the IFTTT WebHooks service. Once you are part of the WebHooks service you will receive a secret key to access your applets. Do not share this key with anyone.

Creating your applet

As an example I'm making this applet for my smoke and gas-detection system.

From the main IFTTT window select My Applets, then click on New Applet.

In the next window click the blue this.

The steps for setting up ifttt web requests. Picture: Anthony Hartup

You'll be asked to choose a trigger channel in step 1.

The steps for setting up ifttt web requests. Picture: Anthony Hartup

Type webhooks into the search field and click on the Webhooks icon when it appears.


The steps for setting up ifttt web requests. Picture: Anthony Hartup

There is only one option in the step2 window. Click Receive web request.

In step 3 you'll need to give your event a name.

The steps for setting up ifttt web requests. Picture: Anthony Hartup

This name must be unique, because it identifies your recipe in a web request. Like your secret Maker Channel key, you should not share this event name.

Click Create Trigger.


The steps for setting up ifttt web requests. Picture: Anthony Hartup

Now we configure the THAT side of the recipe.

Click the blue +that.


The steps for setting up ifttt web requests. Picture: Anthony Hartup

In step 4 you'll be asked to choose an action channel

Type Email into the channel search field and click the Email icon when it appears.


The steps for setting up ifttt web requests. Picture: Anthony Hartup

Once again there is only one option. Click Send me an email.


The steps for setting up ifttt web requests. Picture: Anthony Hartup

In step 6 you can decide how you want your email alerts to look.

I have changed the Subject to AAIMI AIR ALERT. I've also changed the first line of the body to tell me smoke or gas has been detected.

The last two lines of the body I have left as-is. The OccuredAt" tells the system to include the time of the event.

The "Value1", "value2" and "value3" represent the extra details you can include in your email.

There are more details on email layout at the end of this article.

Click Create Action, then click Finish.

That is the web stuff out of the way, now lets put the system to use.

The Python Requests Library

The simplest way to access your ifttt recipe from Python is the Requests library. This is built specifically to conduct web requests.

I have only tried this on Python 2.7, there may be differences in Python 3+

You may have to install the Requests library. In your Raspberry Pi terminal type:

sudo pip install requests.

Follow the prompts to complete the installation.

Next make sure you have OpenSSL up to date. Type:

sudo apt-get install python-dev libffi-dev libssl-dev

This may take a few minutes. When it's finished, type:

sudo pip install --upgrade ndg-httpsclient

Sample code

Now we'll write some code to send an email alert and include three extra pieces of information in the message.

You can append three separate values with your email. They must be strings, and stored in Python dictionary format.

In the AAIMI Home Automation system alerts, the first two values I receive are the room where smoke is detected and the actual sensor reading. The third value AAIMI sends lists any rooms in the house that are occupied

AAIMI prepares these values and sends them to a function, which sorts them into a dictionary and executes the web request.

In this sample code we'll keep it simple, and just make a function that asks for three strings then appends them to the email alert.

Paste the code below into IDLE and save it to your Raspberry Pi.

import requests

def email_alert(first, second, third):
    report = {}
    report["value1"] = first
    report["value2"] = second
    report["value3"] = third
    requests.post("https://maker.ifttt.com/trigger/YourEventName/with/key\
    /YourSecretKey", data=report)    

print("Choose your first string.")
a = input()
print("Choose your second string.")
b = input()
print("Choose your third string.")
c = input()
email_alert(a, b, c)

You'll need to change two sections of the requests.post() line. Replace YourEventName with the exact name of the recipe you created earlier. You'll also need to replace YourSecretKey with your Maker Channel secret key.

Note that your secret key lives in plain text in the Python code. Make sure the file's permissions allow only you to read the file.

Now you can start the program.

I named my code "aaimi_alerts.py, so I open a terminal on the Raspberry Pi and type: python aaimi_alerts.py.

Starting a script to send IFTTT a web request. Picture: Anthony Hartup

After a moment a promp appears and asks you to enter a string.

Entering strings to include in an ifttt email alert. Picture: Anthony Hartup

I'll start my message with a hello.

Entering strings to include in an ifttt email alert. Picture: Anthony Hartup

You can use multiple words with spaces or under_scores to fit all the data you need into just three variables.

Entering strings to include in an ifttt email alert. Picture: Anthony Hartup

When prompted for a second string I create the middle of my message.

Entering strings to include in an ifttt email alert.. Picture: Anthony Hartup

I then enter Cave for the final string.

The steps for setting up ifttt web requests. Picture: Anthony Hartup

You will receive no feedback from the function, the program just exits as soon as it is done.

Check your emails, you should see one from IFTTT.

An email sent from IFTTT after a Python web requests. Picture: Anthony Hartup

You should see the three variables you entered at the bottom of the email.

Obviously "Hello from the Cave" is not that helpful in an email alert. Below you can see what my actual alerts look like form my smoke sensors.

An email sent from IFTTT after a Python web requests. Picture: Anthony Hartup

These variables are a lot more helpful.

There are a couple of things I don't like, though: The "When" and "Extra Data" text in the body.

That's okay, IFTTT allows you to modify your recipe. Log into IFTTT then go to My Applets and click on your applet.

Modify IFTTT recipe. Picture: Anthony Hartup

Click the settings icon at the top-right.

Scroll down untill you see the Send me an email section with the Subject and body fields.

IFTTT email body. Picture: Anthony Hartup

You can delete anything in the body that is not highlighted grey. If you delete the highlighted text you will lose your timestamp and variables.

IFTTT email body with CSS. Picture: Anthony Hartup

You can add some inline CSS if you want to pretty things up. So far I've only added a blue heading, but when I get time I'll style it to match the Cave.

When you are happy with your text, click Update.

An AAIMI Home Automation smoke and gas email alert. Picture: Anthony Hartup

That looks a bit better.

Leave a comment below if you need any help with this.

Cheers

Anth

_____________________________________________


Comments

Leave a comment on this article