title: Python mailer dropping subjects from all except last in list
body: <p>So I was working on an mail script to send mail to a list of users from text file. It works fine, iterating through the list, each user receives an email - except one detail, the subject is dropped from all emails except the last one sent by the script. What's happening here?</p>
  
  <pre><code>#!/usr/bin/env python
  
  import sys
  from smtplib import SMTP
  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText
  from email.mime.image import MIMEImage
  
  
  targets = open('targets.txt', "rb")
  
  def mailme():
      for line in targets:
          msg = MIMEMultipart('alternative')
          m_source = '&lt;sender@address.com&gt;'
          m_target = '%s' % line
          smtp_server = 'mail.server.com'
          smtp_port = '25'
          msg['From'] = m_source
          msg['To'] = m_target
          msg['Subject'] = "Subject"
          smtp = SMTP()
          smtp.set_debuglevel(0)
          smtp.connect(smtp_server, smtp_port)
  
          message_text = ('Some Text for %s' % (m_target)
                          )
  
          message_html = ("""&lt;body&gt;
                          &lt;h1&gt;Some HTML for %s&lt;/h1&gt;
                          &lt;/body&gt;"""
                          ) % (m_target)
  
          txt = MIMEText(message_text, 'plain')
          web = MIMEText(message_html, 'html')
  
  
          msg.attach(txt)
          msg.attach(web)
          smtp.sendmail(m_source, m_target, msg.as_string())
          smtp.quit()
  
  if __name__ == "__main__":
      mailme()
      sys.exit(0)
  </code></pre>