Python mailer dropping subjects from all except last in list

Viewed 54

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?

#!/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 = '<sender@address.com>'
        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 = ("""<body>
                        <h1>Some HTML for %s</h1>
                        </body>"""
                        ) % (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)
1 Answers
Related