<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Chris Rebert wrote:
<blockquote
 cite="mid:x2p50697b2c1004291337u4866b964j713d554ce440e177@mail.gmail.com"
 type="cite">
  <pre wrap="">On Thu, Apr 29, 2010 at 1:06 PM, Alan Harris-Reid
<a class="moz-txt-link-rfc2396E" href="mailto:aharrisreid@googlemail.com"><aharrisreid@googlemail.com></a> wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Hi there,

I want to send an email with an attachment using the following code (running
under Python 3.1, greatly simplified to show example)

  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText

  msg = MIMEMultipart()
  msg['From'] = from_addr
  msg['To'] = to_addr
  msg['Subject'] = subject
  msg.attach(MIMEText(body))

  fp = open(att_file)
  att_msg = MIMEText(fp.read())
  attachment = att_msg.add_header('Content-Disposition', 'attachment',
filename=att_file)
  msg.attach(attachment)

  # set string to be sent as 3rd parameter to smptlib.SMTP.sendmail()
  send_string = msg.as_string()


The attachment object msg1 returns 'email.mime.text.MIMEText' object at
<address>', but when the att_msg.add_header(...) line runs the result is
None, hence the program falls-over in msg.as_string() because no part of the
attachment can have a None value.  (Traceback shows "'NoneType' object has
no attribute 'get_content_maintype'" in line 118 of _dispatch in
generator.py, many levels down from msg.as_string())

Has anyone any idea what the cause of the problem might be?  Any help would
be appreciated.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
.add_header() modifies the MIMEText object *in-place*; per Python
conventions, mutator methods return None; hence, attachment = None.

Try instead (untested):
att_msg.add_header('Content-Disposition', 'attachment', filename=att_file)
msg.attach(att_msg)

Cheers,
Chris
--
<a class="moz-txt-link-freetext" href="http://blog.rebertia.com">http://blog.rebertia.com</a></pre>
</blockquote>
Hi Chris, <br>
<br>
You are right - that does the trick.<br>
<br>
Many thanks,<br>
Alan<br>
<br>
</body>
</html>