Exercise (Python email)

exercises should be done in ~/netsp/ directory

Python Email Package

  • Application interface for composing/parsing MIME email message
  • Improved through develepment of Mailman mailing list management system
  • You may find my name in the test suite for this package, though it is not written by myself.

Compose Simple English Message

  • ex01.py
  • Use MIMEText class to create a message instance
  • Headers are added by dictionary-like substitution
  • Utils help constructing Date header
  • print it with as_string() function

Send the Message

  • ex02.py
  • Use smtplib for connecting to our server
  • Note that recipt should be a list or tuple to include multiple recipients

Compose Quoted-Printable French Message

  • ex03.py
  • Text and Subject are given in unicode string
  • You must expicitly designate character set
  • MIME header is composed with Header class and properly output by msg.as_string()

Compose Message with Image Attachment

  • ex04.py
  • wxhome.jpg for attachment
  • Multipart/mixed message is created by MIMEMultipart class
  • Image part is made by MIMEImage class
  • msg.attach() attaches text and image parts

Parse MIME Message

  • ex05.py
  • sample.msg for test parsing
  • Use message_from_file or message_from_string to obtain parsed message object
  • Further parsing is needed for MIME header
  • Multipart parts are obtained by walk() iterator

Additional Exercise

  • /tmp directory can be used by any user for storing temporary files
  • Use appropreate file name and extension to store the image in sample.msg
  • Use os.system("open filename") to invoke Preview.app and display the imge

Further Additional Exercise

  • Create a CGI aplication to prompt for text input and send the message by mail to yourself.
  • Use <textarea> tag for input of multiline text for message content


  • Write a mail report to Kikuchi (in Japanese)