Forward Mail as Text Message

This guide provides directions to set up Gmail to Forward Filtered Mail to a Smart Phone (i.e, send email from Raspberry Pi to gmail and then as text message to Smart Phone).

Google has made sending text mails from a Raspberry Pi through gmail progressively more difficult. As of 06DEC2020, to send text mails through a gmail account requires lowering the security on gmail. This is not recommended.

Even with Google's restrictions, the simplest solution is to use gmail. (I am thinking about setting up a private email server).

There is no way to use a Raspberry Pi to send an email directly to an email account.

So, create a gmail account just for your home. Don't use it for anything else. If it gets compromised, then abandon it and create another.

This approach requires changes to gmail security, but on a throw-away account.

Requirements:

Raspberry Pi

Smart Phone

Google Gmail account

Step 1. Create a gmail account

Go to the gmail creation page and follow the directions

Step 2. Lower security on throw-away gmail account

If the link does not work, then here are the step-by-step instructions:

Open a browser

Login to throw away gmail account

Click on settings icon (gear)

Click on See All Settings, which pops up a new tab on my laptop

Click on tab Accounts and Import

Click on Other Google Account settings.

On the left side menu, click Security

Scroll down and enable Less secure app access

Close the Settings tab if one was opened

Step 3. Subject Identifier (ID)

The idea is to forward any email sent to your gmail account to your phone if it contains the ID in the Subject line of the email.

An ID of 6 alphanumerics creates a domain of over 10^27 possible results. For example, a-z = 26 plus 0-9 = 10. So, 6^36 > 10^27 possible values.

The ID can be used by multiple Raspberry Pis to foward alerts to your Smart Phone.

Create a simple script to generate a random string of 6 alphanumerics from StackExchange. Login to a Raspberry Pi and run the command:

$ wget https://raw.githubusercontent.com/dumbo25/send-text-message/main/genID.py

Run the script (each run will create a new number):

$ python3 genID.py

DWV6MC

Step 4. Create a Standard Subject Line for your projects

This technique can be used on multiple Raspberry Pi projects. So, it might be worthwhile to spend some time and figure out what a standard text message from a system might be.

A Subject line should be less than 40 characters. Anything over 40 characters will be truncated

For example, an email Subject line from a Raspberry Pi, might look like:

<ID-from-above> <system>: <message>

So, an example message from a Smart Doorbell when it captures motion detection might be:

DWV6MC Doorbell: motion detected

And here is an example message from a Smart Garage Door Opener when the door is supposed to be closed, but it is open:

DWV6MC Garage: open but must be closed


Step 5. Create a Forwarding Address in Gmail

Open a browser and login into gmail

Click on settings icon and then click on See all settings

Click Forwarding and POP/IMAP

Click Add a forwarding address

Use the table above to determine how to send yourself a text

For me, it is ♣my-cell-number@txt.att.net

Follow the directions to confirm the forwarding. This will not forward all of your email to your phone.

Step 6. Create a Gmail Filter on the ID and Forward to Smart Phone

If step above was just completed, then click on gmail icon, which should take you back to inbox, or

Open gmail

In the search bar, select the down arrow (see above)

In the Subject line enter your ID (see below, example ID is DWV6MC)

Click Create Filter

Click Mark as Read

Click Forward and select your Smart Phone from the drop down

Click Create Filter

Step 7. Edit the Gmail Filter and or the forwarding email [optional]

If you need to edit the filter or forwarding address do the following:

Open Settings Icon, then See all settings

Clcik, on the Filters and Blocked Addresses

Scroll until you find your filer

Select Edit and make your changes

Step 8. Try it

Send an email from your phone to your gmail account with the ID in the subject line, and you should receive a text message on your phone.

Sample python script to send text message:

Get the script:

$ wget https://raw.githubusercontent.com/dumbo25/send-text-message/main/sendTextMessage.py

Edit the file and replace items in angle brackets with actual values:

$ nano sendTextMessage.py

Run the script:

$ python sendTextMessage.py