Raspberry_Pi_Education_Manual

Notes:

email-attachment.py:

import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage # create multipart email

msg = MIMEMultipart() msg[ 'Subject' ] = 'Lily' msg[ 'From' ] = 'My RPi ' 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 file filename = 'picture.jpg'

with open (filename, 'rb' ) as f: img = MIMEImage(f.read()) img.add_header( 'Content-Disposition' , 'attachment' , filename=filename) msg.attach(img) # send email

s = smtplib.SMTP( 'smtpserver' ) s.login( 'username' , 'password' ) s.send_message(msg) s.quit()

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

Human-computer interfacing

117

Made with FlippingBook flipbook maker