Operation Manual
Human-computer interfacing
116
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
fromemail.mime.textimportMIMEText
# 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'] = 'MyRPi<my_rpi@example.com>'
msg['To'] = 'you@yourdomain.com'
# send the email
s = smtplib.SMTP('smtpserver')
s.lo gin('username', 'password')
s.sendmail(msg['From'], m s g['To'], msg.as_string())
s.q u it()
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.