Send an Email From the Phidget SBC4 With Python

From Phidgets Support
Revision as of 16:57, 10 August 2023 by Lmpacent (talk | contribs) (Created page with "Category: Project {{TOC limit|3}} {{#seo:|keywords=phidget SBC}} {{#seo:|description=Send an email from your Phidget SBC4 with Python.}} 400px|link=|thumb ==Introduction== In this project, we will create a simple Python program that sends an email from a Phidget SBC4. <br><br><br><br> <br><br> ==Parts== <ul> <li>[https://www.phidgets.com/?tier=3&catid=1&pcid=0&prodid=969 Phidget SBC4 - SBC3003_0]</li> </ul> ==Configure Email (Gmail)== In...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Sbcemail intro.png

Introduction

In this project, we will create a simple Python program that sends an email from a Phidget SBC4.





Parts

Configure Email (Gmail)

In order to send emails from the Phidget SBC4, we will use the smtplib module that is included in the Python Standard Library. We will be using a Gmail account for this demonstration.

Turn On 2-Step Verification

Navigate to your account settings and enable 2-Step Verification.

Sbcemail twostep.png


Set App Password

At the bottom of the 2-Step Verification page, set an app password. You will use this later in your Python program.

Sbcemail apppassword.png



SBC Configuration

Install Python

If you haven't already installed Python on the Phidget SBC4, navigate to System > Packages and press Install Python. Instructions to install Python can also be found here.

Sbcemail installpython.png


Create Project

Navigate to Projects and create a new project called PythonEmail

Sbcemail createproject.png

Python Code

Add the following python code to a Python file in your new project. You will need to enter your email address and your app password.

#For email
import smtplib
import ssl
#For timestamp
from datetime import datetime
import time 


SMTP_SERVER = "smtp.gmail.com"# Change this if not using Gmail
SMTP_PORT = 465  # Change this if not using Gmail
USERNAME = "YOUR_EMAIL@gmail.com"
PASSWORD = "YOUR_APP_PASSWORD"
TO = USERNAME # Select a recipient, for now, just send an email to yourself

def sendMessage(time):
    context = ssl.create_default_context() #create a secure SSL context
    with smtplib.SMTP_SSL(SMTP_SERVER,SMTP_PORT,context=context) as server: #connect to the email server and send the alert message
        server.login(USERNAME,PASSWORD) #here we log in with our username and password.
        #The message formatting must be done very specifically so we have the right indentation and spaces.
        #"Subject:" must be the first line in our string. It will mark the text we want in our subject line.
        #We can use the format function to fill in the details of the email.
        message = """Subject: This is a subject heading!

Hello,

This is an email from a Phidget SBC!

Notice that, after the subject line, you can format this like a normal email.

Formatting works too. For example, here's the time: {0}
""".format(time)
        server.sendmail(USERNAME,TO,message) #This statement sends the email
        
def main():
    now = datetime.now()
    sendMessage(now.strftime("%m/%d/%Y, %H:%M:%S"))

main()

Result

After running the Python program, an email will be sent out.

Sbcemail email.png


This functionality can now be paired with Phidget sensors to create an alert system. For example, you could use a temperature sensor to monitor a server room, and send an email if the temperature gets too hot.