Trigger email notification from Rule Machine with Raspberry Pi and Python script using gmail

Hello all,

Quick and easy solution for users with running RPi (e.g. for OpenVPN purposes).
Tested on Rapsberian (RPi3 - python3) out of the box:

  • open terminal
  • create folder structure in home/pi: home/pi/scripts/cgi-bin
  • goto /home/pi/scripts/cgi-bin
  • create python script sendmail.py
    $ sudo nano sendmail.py
  • add following content:

#!/usr/bin/python
import smtplib
import cgi
fs = cgi.FieldStorage()

sender_email = "xxxxxhub@gmail.com"
receiver_email = "xxxxx@gmail.com"
if "subject" not in fs or "body" not in fs:
  SUBJECT = "Default Subject"
  TEXT = "Default body text"
else:
  SUBJECT = fs["subject"].value
  TEXT = fs["body"].value

message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.login(sender_email,'<sender password>')
    server.sendmail(sender_email, receiver_email, message)
    server.quit()
    print 'Email sent!'
except:
    print 'Something went wrong...'

Sender and receiver can be dynamic same like subject and body

In Rule machine use HTTP GET request with upper URL link to send email according to trigger.

That's all.
Majomitto.

4 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.