ex04.py

ex04.py — Python Source, 0Kb

ファイルコンテンツ

#!/usr/bin/env python
# ex04.py
# compose email with image attachment
# imports for python 2.4 ... see comments for python 2.5
import email.Utils           # email.utils
import email.MIMEMultipart   # email.mime.multipart
import email.MIMEText        # email.mime.text
import email.MIMEImage       # email.mime.image

txt = """Hi!, This is sample image."""
img = file('wxhome.jpg').read()

txtpart = email.MIMEText.MIMEText(txt)
imgpart = email.MIMEImage.MIMEImage(img, _subtype='jpeg')

msg = email.MIMEMultipart.MIMEMultipart()
msg['Subject'] = 'Email with Image Attachment'
msg['From'] = 'foo@example.com'
msg['To'] = 'bar@example.jp'
msg['Date'] = email.Utils.formatdate(localtime=True)
msg.attach(txtpart)
msg.attach(imgpart)

print msg.as_string()