send email to recipient from .txt in python 3.3
Question: I am trying to send an email with smtp in python 3.3 to a recipient listed in
a text file. The error I receive is:  
`session.sendmail(sender, recipient, msg.as_string())`
`smtplib.SMTPRecipientsRefused: {}`

Where is the error in sendmail? Thanks!

Full code below:

    
    
    #!/usr/bin/python
    import os, re
    import sys
    import smtplib
    
    from email.mime.image import MIMEImage
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    
    mails = open('/path/emails.txt','r+')
    mailList = mails.read()
    mailList = [i.strip() for i in mails.readlines()] 
    directory = "/path/Desktop/"
    
    SMTP_SERVER = 'smtp.gmail.com'
    SMTP_PORT = 587
    
    sender = 'Sender@gmail.com'
    password = "Sender'sPassword"
    recipient = [i.strip() for i in mails.readlines()] 
    subject = 'Python (-++-) Test'
    message = 'Images attached.'
    
    def main():
        msg = MIMEMultipart()
        msg['Subject'] = 'Python (-++-) Test'
        msg['To'] = recipient
        msg['From'] = sender
    
        files = os.listdir(directory)
        pngsearch = re.compile(".png", re.IGNORECASE)
        files = filter(pngsearch.search, files)
        for filename in files:
            path = os.path.join(directory, filename)
            if not os.path.isfile(path):
                continue
    
             img = MIMEImage(open(path, 'rb').read(), _subtype="png")
             img.add_header('Content-Disposition', 'attachment', filename=filename)
             msg.attach(img)
    
        part = MIMEText('text', "plain")
        part.set_payload(message)
        msg.attach(part)
    
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    
        session.ehlo()
        session.starttls()
        session.ehlo
        session.login(sender, password)
    
        session.sendmail(sender, recipient, msg.as_string())
        session.quit()
    
    if __name__ == '__main__':
        main()
    


Answer: You want to double-check your recipients code. It looks like you're trying to
consume the contents of the file more than once, which won't work--the file
objects should be understood as a stream, not a block of data, so once you've
done `f.read()` or `[i.strip() for i in mails.readlines()]` one time, that
stream is empty, so doing it a second time is going to produce an empty list.
You should check this yourself by printing `recipient`

then try this:

    
    
    mails = open('/path/emails.txt','r+')
    #mailList = mails.read()
    #mailList = [i.strip() for i in mails.readlines()] 
    directory = "/path/Desktop/"
    
    SMTP_SERVER = 'smtp.gmail.com'
    SMTP_PORT = 587
    
    sender = 'Sender@gmail.com'
    password = "Sender'sPassword"
    recipient = [i.strip() for i in mails.readlines()] 
    print(recipient)
    subject = 'Python (-++-) Test'
    message = 'Images attached.'
    

Now you should have the a populated recipient list, and on to the next issue!

