Anth's Computer Cave

AAIMI Email Out

11th November 2018

In this tutorial I'll show you how to use a simple Python module to send emails from your Python programs.

I use this method to get alerts from my home-automation and security systems.

You'll need a dedicated Gmail address for your programs to send from.

Download and setup

You can download the Python code here.

Extract the file to the folder containing your program and open the file.

On line 25 change the system_email to the dedicated gmail account your program will send from.

On line 27 is the password for this account. It's not wise to hard-code passwords into your program files, so I leave this blank, then have my home-automation program prompt for the password on startup. I'll explain this in a moment.

# Dedicated email account for AAIMI to send and receive email
system_email = ""
# Password for AAIMI's dedicated Gmail account
system_pass = ""
# User email address
user_email = ""

On line 29 enter your email account, the account that will receive the email alerts.

On line 41 you can change the layout and colors for the emails by modifying the CSS.

Changing the CSS for emails sent by AAIMI Email

On line 51 you can change the HTML tags to adjust the layout. By default the subject is a H1 and the rest of the arguments are paragraph tags.

Changing the HTML for emails sent by AAIMI Email

That's all you need to do in this file, let's import it into your programs.

Import and use AAIMI Email

Open your Python program that needs to send emails. At the top of your code, import AAIMI Email, getpass and time:

import aaimi_email_out
import getpass
import time

If, as suggested you left the password field blank in the aaimi_email file, add the following block of code below the import statement:

print("Enter password for " + aaimi_email_out.system_email)
pw = str(getpass.getpass())
aaimi_email_out.system_pass = pw

When your program starts it will prompt for the password for your system email account.

Next, create a time limit in seconds and a last_email variable to record the time of the last email. This is an important safety feature that sets a time limit for for subsequent emails. If your program somehow ends up in an unintended loop, you don't want it firing off emails one-after-the-other indefinitely.

It should look something like this:

time_limit = 300
last_email = time.time() - time_limit

The above example sets a minimum time of five minutes between emails.

Now the code to send an email. Let's say you have made an alarm system that already activates an audible alarm when it senses an intruder, and you also want it to send you an email.

It may look something like this:

while True:
    if intruder == "yes":

It's a really over-simplified example, but you can hopefully get the idea.

Here's what you would add:

while True:
    if intruder == "yes":
	  if (time.time() - last_email) > time_limit:
		last_email = time.time()
		aaimi_email_out.send_email_report("Intruder", time.strftime("%d:%h:%m"), "Details")

This checks the time since the last email, then send an email with the subject, timestamp, and any other details you wish to send.





Leave a comment on this article