python-2.7 - 属性错误: list object has not attribute lstrip in sending an email with attachment

标签 python-2.7 email-attachments attributeerror

我正在附加来自特定路径 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:\important\log.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:\important\secret_file.pyw')
  File "C:\important\secret_file.pyw", line 45, in <module>
    session.sendmail(sender,receiver,msg.as_string())
  File "C:\Python27\lib\email\message.py", line 137, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "C:\Python27\lib\email\generator.py", line 83, in flatten
    self._write(msg)
  File "C:\Python27\lib\email\generator.py", line 115, in _write
    self._write_headers(msg)
  File "C:\Python27\lib\email\generator.py", line 164, in _write_headers
    v, maxlinelen=self._maxheaderlen, header_name=h).encode()
  File "C:\Python27\lib\email\header.py", line 410, in encode
    value = self._encode_chunks(newchunks, maxlinelen)
  File "C:\Python27\lib\email\header.py", line 370, in _encode_chunks
    _max_append(chunks, s, maxlinelen, extra)
  File "C:\Python27\lib\email\quoprimime.py", line 97, in _max_append
    L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'

请帮忙。

最佳答案

这是一个小错误。接收者参数为列表类型。要么应该使用 join 方法将其列表转换为字符串,要么如果它是单个收件人,则仅将其作为字符串传递

关于python-2.7 - 属性错误: list object has not attribute lstrip in sending an email with attachment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26985683/

相关文章:

android - 电子邮件附件的 Intent 过滤器

python - 当我尝试使用 subprocess.call 时,导致 "AttributeError: ' tuple' object has no attribute 'rfind' 的原因是什么

python - 如何在 python Tkinter 中更新字典的值

python - 无法在 Mac OS El Capitan 上安装 nltk

python - json.loads 可以忽略尾随逗号吗?

python - 'datetime.datetime' 对象没有属性 'microseconds'

python - 当从 QObject 派生的类的属性中引发 AttributeError 时出现误导性错误消息

使用 Fortran 的 Python 模块 : LNK1112 `module machine type ' X8 6' conflicts with target machine type ' x6 4'`

cocoa - 附件中的边界

php - 如何在 PHP 中将加密的 Zip 存档作为邮件附件发送?