Raspberry_Pi_Education_Manual

Notes:

Lesson 4.2: Email application

Email is transferred across the internet using something called SMTP – Simple Mail Transfer Protocol. SMTP uses port 25. Don’t worry; you don’t need to understand how SMTP works to be able to send an email. There is a Python module that does all the hard work for you.

Use the Python script below to send a single email.

email-example.py:

import smtplib from email.mime.text import MIMEText # create the email message = """This is a test of using Python on Raspberry Pi to send an email. Have fun!""" msg = MIMEText(message) msg[ 'Subject' ] = 'RPi Python test' msg[ 'From' ] = 'My RPi ' msg[ 'To' ] = 'you@yourdomain.com' # send the email s = smtplib.SMTP( 'smtpserver' ) s.login( 'username' , 'password' ) s.sendmail(msg[ 'From' ], msg[ 'To' ], msg.as_string()) s.quit()

You can modify the program to send email attachments as well as simple text. The example on the next page sends an image (such as a JPG file) as an attachment to the message.

Human-computer interfacing

116

Made with FlippingBook flipbook maker