Operation Manual

Human-computer interfacing
117
Notes:
email-attachment.py:
import smtplib
from email.mime.multipart import MIMEMultipart
fromemail.mime.textimportMIMEText
from email.mime.image import MIMEImage
# create multipart email
msg = MIMEMultipart()
msg['Subject'] = 'Lily'
msg['From'] = 'MyRPi<my_rpi@example.com>'
msg['To'] = 'you@yourdomain.com'
msg.preamble = 'This is a multi-part message in MIME format.'
#attachemailtext
message = """This is a test of using Python on Raspberry Pi
to send an email. This email also includes a picture as an
email attachment. Have fun!"""
msg.attach(MIMEText(message))
#attachaJPGle
lename='picture.jpg'
with open(lena m e,'rb') as f:
img = MIMEImage(f.read())
img.add_header('Content-Disposition', 'attachment',lename=lename)
msg.attach(img)
# send email
s = smtplib.SMTP('smtpserver')
s.lo gin('username', 'password')
s.send_message(msg)
s.q u it()
Over to you
Again, try and think of other ways you could use this script. For instance, you
could generate the content of the email (the output) based on an input. For
example, checking the room temperature and emailing a warning if it is getting too
hot. This is regularly used in server rooms, for instance, to detect when the air
conditioning has failed.
NOTE: You will have to change the text highlighted in yellow to your own email
address and email server details for this program to work.
There is more information about the email modules in Python available at
http://docs.python.org/py3k/library/email.html