2014-11-18 14 views
7

tôi đang gắn một tập tin từ một con đường đặc biệt c: \ quan trọng \ log.txtthuộc tính lỗi: danh sách đối tượng đã không thuộc tính lstrip trong việc gửi một email với tập tin đính kèm

sender = '[email protected]' 
receiver = ['[email protected]'] 
message = """From: From Pooja Gupta <[email protected]> 
To: To Shubha Goel <[email protected]> 
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') 

Nó vẫn tiếp tục đem lại cho tôi những lỗi tương tự các thuộc tính danh sách lỗi đối tượng không có thuộc tính lstrip sau đây là lỗi, stack trace:

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' 

Xin vui lòng giúp.

Trả lời

8

đó là lỗi nhỏ. thông số người nhận là loại danh sách. hoặc danh sách phải được chuyển đổi thành chuỗi bằng cách sử dụng phương thức kết nối hoặc nếu đó là một người nhận, sau đó chuyển nó thành một chuỗi chỉ

+0

Một chuỗi có loại dấu phân tách ... Dấu phẩy, dấu cách, dấu chấm phẩy, v.v ...? –

+2

@JamieIvanov: chuỗi phải được phân tách bằng dấu phẩy. –

3

receiver = ['[email protected]'] Đây là danh sách nhưng msg [' Để '] đang mong đợi một chuỗi và do đó lỗi.

Bạn có thể sử dụng ','. Join (receiver) và điều đó sẽ giải quyết được vấn đề của bạn.

Các vấn đề liên quan