#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Unhappy Issue sending email to multiple recipients


    Hi,

    I'm having a problem sending emails to multiple recipients when I use the email module - if I send to a single recipient it works fine.

    My previous code which uses purely smtplib and strings works, but I now need to send email attachments so I'm having to expand it to use the email module.


    Code:
    import os
    import smtplib
    from datetime import datetime
    from email import Encoders
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    from email.Utils import formatdate
    
    SMTP_USERNAME = 'myusername@gmail.com'
    SMTP_PASSWORD = 'password'
    SMTP_RECIPIENTS = ['recipient1@gmail.com', 'recipient2@gmail.com', 'recipient3@hotmail.com', 'recipient4@hushmail.com']
    SMTP_SERVER = 'smtp.gmail.com'
    SSL_PORT = 465
    
    def send_email(attachment_filename):
    
    	print ""
    	print "Preparing to send email..."
    
    	try:
    		msg = MIMEMultipart()
    		msg["From"] = SMTP_USERNAME
    		msg["To"] = SMTP_RECIPIENTS
    		msg["Subject"] = "Email subject goes here."
    		msg['Date'] = formatdate(localtime=True)
    		msg['Body'] = "Email body goes here."
    	 
    		# attach a file
    		part = MIMEBase('application', "octet-stream")
    		part.set_payload( open(attachment_filename,"rb").read() )
    		Encoders.encode_base64(part)
    		part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attachment_filename))
    		msg.attach(part)
    	 
    		server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
    		server.login(SMTP_USERNAME, SMTP_PASSWORD)
     
    		server.sendmail(SMTP_USERNAME, SMTP_RECIPIENTS, msg.as_string())
    		
    		print ""
    		print "Email sent!"
    		
    	except Exception, e:
    		errorMsg = "Unable to send email. Error: %s" % str(e)
    		print errorMsg
    
    send_email(r,'\\path\to\file\attachment')
    This produces the error:
    Preparing to send email...
    Unable to send email. Error: 'list' object has no attribute 'lstrip'
    However, if my older code which used just smtplib and strings DOES send to multiple recipients without a probblem:

    Code:
    def send_mail(subject_text, body_text):
    
    	# try to send the email to all listed in the recipients list
    	try:
    		email_to = SMTP_RECIPIENTS
    		email_from = SMTP_USERNAME
    		email_body = string.join((
    								'From: %s' % email_from,
    								'To: %s' % email_to,
    								'Subject: %s' % subject_text,
    								'',
    								body_text
    								), '\r\n')
    		server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
    		server.login(SMTP_USERNAME, SMTP_PASSWORD)
    		server.sendmail (email_from, email_to, email_body)
    		server.quit()
    	except SMTPConnectError:
    		logging.error('SMTPConnectError:  Unable to connect to remote server.')
    		exit('SMTPConnectError: Exiting from send_mail().')
    I'm not sure what is causing the error as I'm still sending the email using smtplib, but using email to build the mail to send?

    Kind regards,
    Morrolan
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by Morrolan
    Hi,

    I'm having a problem sending emails to multiple recipients when I use the email module - if I send to a single recipient it works fine. [...]

    Code:
    [...]
    SMTP_RECIPIENTS = ['recipient1@gmail.com', 'recipient2@gmail.com', 'recipient3@hotmail.com', 'recipient4@hushmail.com']
    [...]
    
    		msg["To"] = SMTP_RECIPIENTS
    [...]
    
    [...]
    [...]
    When using Mimemultipart(), the recipient addresses need to be specified comma-separated in a string.

    See here (third example): http://docs.python.org/2/library/email-examples.html

    So basically, change to:
    Code:
    msg["To"] = ', '.join(SMTP_RECIPIENTS)
    Not tested.

IMN logo majestic logo threadwatch logo seochat tools logo