Python 3.4发送给多个接收者的电子邮件会引发错误

时间:2015-07-02 16:15:09

标签: email python-3.x message account

我正在编写一个脚本来向多个电子邮件帐户发送电子邮件,但尚无法使用。

它的工作原理如下,但是如果我设置receivers='xxx@xxx.com','yyy@yyy.com'它将无效,则会抛出错误:

AttributeError: 'tuple' object has no attribute 'encode'. 

如何设置receivers=

def send_email (out_file):

    sender = 'xxx@xxx.com'
    receivers = 'xxx@xxx.com'
    email_pass = 'aaaa'

    filematch=re.findall('NE.*\.txt',out_file.name)

    subject = ("NEXXXX_price_update")
    message = ("The following file was forwarded to your ftp account %s "  %filematch)

    msg = 'Subject: %s\n%s' %(subject, message)


    try:
        smtpObj = smtplib.SMTP_SSL('smtp.gmail.com',0)
        smtpObj.login(receivers, email_pass)
        smtpObj.sendmail(sender, receivers, msg)

        print ("Successfully sent email")

    except SMTPException:        

        print ("email NOT successful")
        print(SMTPException.__cause__)        

        smtpObj.quit() 

1 个答案:

答案 0 :(得分:1)

你错误地分配了

receivers='xxx@xxx.com','yyy@yyy.com'

您希望指定为tuplelist,不确定100%是哪个。

试一试:

receivers=('xxx@xxx.com','yyy@yyy.com')

receivers=['xxx@xxx.com','yyy@yyy.com']