Outstripping all other read posts 5 to 1 is the motion detection post I did at the end of June.  For those who haven’t seen it before you can read it here.

photo copy

 

In the original post I sent a message via SMS to my phone to alert me that there was motion detected. That’s fine, some folk want to be emailed especially via their GMail accounts. That’s okay too…..

The change is quite simple:

import smtplib
def sendEmail():
  username = "xxxxxx"
  password = "xxxxxx"
  FROM = "jasebell@gmail.com"
  TO = ["jasebell@gmail.com"]
  SUBJECT = "Testing sending using gmail"
  TEXT = "Motion was detected from the Raspberry Pi"
 message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
 """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
  try:
   server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
   server.ehlo()
   server.starttls()
   server.login(username, password)
   server.sendmail(FROM, TO, message)
   server.close()
   print 'successfully sent the mail'
  except:
   print "failed to send mail"

This method connects the GMail SMTP server and with the user’s username and password details logs on and sends an email.

Like the SMS you have to be careful not to send too many emails from the Pi, you don’t want to upset Google.

I’ve both version of the code on the RPIMotion Github repo.

 

Advertisements