我正在从特定路径c:\important\log附加一个文件.txt
sender = 'poojagupta4112@gmail.com'
receiver = ['shubh4112@gmail.com']
message = """From: From Pooja Gupta <poojagupta4112@gmail.com>
To: To Shubha Goel <shubh4112@gmail.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
file_name = 'C:importantlog.txt'
msg=MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = message
msg['Date'] = email.Utils.formatdate(localtime=True)
# build the attachment
att = MIMEBase('application', 'base64')
att.set_payload(open(file_name, 'rb').read())
email.Encoders.encode_base64(att)
att.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name))
msg.attach(att)
print 'successfully built attachment'
try:
session = smtplib.SMTP('smtp.gmail.com',587)
print 'Starting..'
session.ehlo()
print 'ehlo executed..'
session.starttls()
print 'starttls done'
session.login(sender,'snxzoumwhpybzvmo')
print 'logged in'
session.sendmail(sender,receiver,msg.as_string())
print 'sendmail executed..now quitting'
session.close()
except smtplib.SMTPRecipientsRefused:
print 'Recipient refused'
except smtplib.SMTPAuthenticationError:
print 'Auth error'
except smtplib.SMTPSenderRefused:
print 'Sender refused'
except smtplib.SMTPException:
print('Error')
它不断给我相同的错误,即属性错误列表对象没有属性 lstrip以下是错误,堆栈跟踪:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
execfile('C:importantsecret_file.pyw')
File "C:importantsecret_file.pyw", line 45, in <module>
session.sendmail(sender,receiver,msg.as_string())
File "C:Python27libemailmessage.py", line 137, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:Python27libemailgenerator.py", line 83, in flatten
self._write(msg)
File "C:Python27libemailgenerator.py", line 115, in _write
self._write_headers(msg)
File "C:Python27libemailgenerator.py", line 164, in _write_headers
v, maxlinelen=self._maxheaderlen, header_name=h).encode()
File "C:Python27libemailheader.py", line 410, in encode
value = self._encode_chunks(newchunks, maxlinelen)
File "C:Python27libemailheader.py", line 370, in _encode_chunks
_max_append(chunks, s, maxlinelen, extra)
File "C:Python27libemailquoprimime.py", line 97, in _max_append
L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'
请帮忙。
这是一个小错误。 接收器参数为列表类型。 要么应该使用 join 方法将其列表转换为字符串,要么如果它是单个收件人,则仅将其作为字符串传递
> receiver = ['shubh4112@gmail.com']这是一个列表,但 msg['To'] 需要一个字符串,因此会出现错误。
你可以使用','.join(receiver),这应该可以解决你的问题。
这似乎是 smtplib 的问题。文档明确表示它接受列表
The arguments are:
- from_addr : The address sending this mail.
- **to_addrs : A list of addresses to send this mail to. A bare
string will be treated as a list with 1 address.**
- msg : The message to send.
文档中的用法:
"Example:
>>> import smtplib
>>> s=smtplib.SMTP("localhost")
**>>> tolist=
["one@one.org","two@two.org","three@three.org","four@four.org"]**
>>> msg = '''\
... From: Me@my.org
... Subject: testin'...
...
... This is a test '''
>>> s.sendmail("me@my.org",tolist,msg)"
此外,如文档中所述,如果收件人以字符串形式传递,则邮件仅发送到第一个邮件ID。
所以实际上问题在于SMTP.sendmail和电子邮件。MIMEText需要两件不同的东西。
电子邮件。MIMEText为电子邮件正文设置了"收件人:"标头。是的仅用于向另一端的人显示结果,并且与所有电子邮件标头一样,必须是单个字符串。(请注意,它没有实际上与实际接受的人有任何关系消息。
另一方面,SMTP.sendmail为邮件设置了"信封"SMTP 协议。它需要一个 Python 字符串列表,每个字符串都有一个单一地址。
因此,您需要做的是合并您收到的两个回复。设置msg['To'] 到单个字符串,但将原始列表传递给 sendmail:
电子邮件 = ['a.com','b.com', 'c.com']****msg['To'] = ', '.join( email )....s.sendmail( msg['From'], email, msg.as_string() )****
我有同样的问题,我的解决方案:
msg['To'] = receiver
接收必须是字符串,如'aa@bb.com,bb@cc.com'
,而不是列表