[Tutor] Email module problem -- Bug ?

Prahlad Vaidyanathan slime@vsnl.net
Tue, 26 Feb 2002 12:25:21 +0530


Hi,

I was playing around with the email module in 2.2, when I came across a
problem. Here is the code that caused it :

<code>
outer = email.MIMEBase.MIMEBase('multipart','mixed')
outer['Subject'] = 'Daily Cartoon Strips %s' % \
    time.strftime("%Y-%m-%d",time.localtime())
outer['from'] = from_addr
outer['to'] = to_addrs
files = ['test.gif']

for filename in files :
    ctype, encoding = mimetypes.guess_type(filename)
    if ctype is None or encoding is not None :
        ctype = 'application/octet-stream'
    maintype, subtype = ctype.split('/',1)
    if maintype == 'text' :
        fp = open(filename)
        msg = email.MIMEText.MIMEText(fp.read(),_subtype=subtype)
        fp.close()
    elif maintype == 'image' :
        fp = open(filename,'rb')
        msg = email.MIMEImage.MIMEImage(fp.read(),_subtype=subtype)
        fp.close()
    else :
        fp = open(filename,'rb')
        msg = email.MIMEBase.MIMEBase(maintype,subtype)
        msg.add_payload(fp.read())
        fp.close()
        email.Encoders.encode_base64(msg)
    outer.attach(msg)

s = smtplib.SMTP()
s.connect()
s.sendmail(from_addr,to_addrs,outer.as_string(0))
s.close()
</code>

This is taken pretty much straight from the documentation, and yet it
throws up this error :

<error>
Traceback (most recent call last):
  File "/home/prahlad/bin/cartoons.py", line 256, in ?
    mailEmAll(from_addr=frm, to_addrs=to)
  File "/home/prahlad/bin/cartoons.py", line 249, in mailEmAll
    s.sendmail(from_addr,to_addrs,outer.as_string(0))
  File "/usr/local/lib/python2.2/email/Message.py", line 59, in as_string
    g(self, unixfrom=unixfrom)
  File "/usr/local/lib/python2.2/email/Generator.py", line 83, in __call__
    self._write(msg)
  File "/usr/local/lib/python2.2/email/Generator.py", line 104, in _write
    self._dispatch(msg)
  File "/usr/local/lib/python2.2/email/Generator.py", line 134, in _dispatch
    meth(msg)
  File "/usr/local/lib/python2.2/email/Generator.py", line 240, in _handle_multipart
    for part in msg.get_payload():
  File "/usr/local/lib/python2.2/email/Message.py", line 151, in __getitem__
    return self.get(name)
  File "/usr/local/lib/python2.2/email/Message.py", line 214, in get
    name = name.lower()
AttributeError: 'int' object has no attribute 'lower'
</error>

Is this a bug ? 

pv.
-- 
Prahlad Vaidyanathan  <http://www.symonds.net/~prahladv/>

A gentleman is a man who wouldn't hit a lady with his hat on.
		-- Evan Esar
		[ And why not?  For why does she have his hat on?  Ed.]