我正在编写一个脚本,向多个电子邮件帐户发送电子邮件,但还不能。
它的工作原理如下,但如果我设置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: %sn%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()
您分配错误的
receivers='xxx@xxx.com','yyy@yyy.com'
您假设分配为tuple
或list
,但不能100%确定是哪个。
试试看:
receivers=('xxx@xxx.com','yyy@yyy.com')
或
receivers=['xxx@xxx.com','yyy@yyy.com']