Email library attachment problem

Bob McCallister hcsrpm at texlife.com
Tue Jan 8 12:45:02 EST 2002


I'm using email .96 with python 2.0.  I read the bug about only
attaching one file and getting an error.  Am I missing something or do
I have to attach a dummy file first, then attach my text, followed by
any other attachments.  When I attach two files, only the second one
gets delivered.  When I attach three files, only the last two get
delivered, etc.  Here is the program (modified from the examples
section of the doc)

# Import smtplib for the actual sending function
import smtplib

# Here are the email pacakge modules we'll need
from email import Encoders
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEBase import MIMEBase

COMMASPACE = ', '

# Create the container (outer) email message.
me = 'hcsrpm at texlife.com'
family = ['hcsrpm']
msg = MIMEBase('multipart', 'mixed')
msg['Subject'] = 'Python email test'
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Message and Two jpg images attached'
# Guarantees the message ends in a newline
msg.epilogue = ''

fp = open('messagetext')
text = MIMEText(fp.read(), _encoder=Encoders.encode_quopri)
msg.attach(text)
fp.close

# Attach images
attachfiles=['DV1.jpg','DV2.jpg']
for file in attachfiles:
   # Open the files in binary mode.  Let the MIMEIMage class
automatically
   # guess the specific image type.
   fp = open(file, 'rb')
   img = MIMEImage(fp.read())
   fp.close()
   msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP()
s.connect()
s.sendmail(me, family, msg.as_string(0))
s.close()

With this I get the two image files, but not the contents of the file
'messagetext.'  If I skip the text attachment, I only get 'DV2.jpg.'

Thanks,
Bob McCallister



More information about the Python-list mailing list