2009-12-22 118 views
2

我想通過電子郵件發送結果文件。我正在一個導入錯誤:python電子郵件錯誤

Traceback (most recent call last): 
    File "email_results.py", line 5, in ? 
    from email import encoders 
ImportError: cannot import name encoders 

我也不清楚如何得到這個連接到服務器。誰能幫忙?謝謝

#!/home/build/test/Python-2.6.4 
import smtplib 
import zipfile 
import tempfile 
from email import encoders 
from email.message import Message 
from email.mime.base import MIMEBase 
from email.mime.multipart import MIMEMultipart 

def send_file_zipped(the_file, recipients, sender='[email protected]'): 
zf = tempfile.TemporaryFile(prefix='mail', suffix='.zip') 
zip = zipfile.ZipFile(zf, 'w') 
zip.write(the_file) 
zip.close() 
zf.seek(0) 

# Create the message 
themsg = MIMEMultipart() 
themsg['Subject'] = 'File %s' % the_file 
themsg['To'] = ', '.join(recipients) 
themsg['From'] = sender 
themsg.preamble = 'I am not using a MIME-aware mail reader.\n' 
msg = MIMEBase('application', 'zip') 
msg.set_payload(zf.read()) 
encoders.encode_base64(msg) 
msg.add_header('Content-Disposition', 'attachment',filename=the_file + '.zip') 

themsg.attach(msg) 
themsg = themsg.as_string() 

# send the message 
smtp = smtplib.SMTP() 
smtp.connect() 
smtp.sendmail(sender, recipients, themsg) 
smtp.close() 
+0

那麼你是如何解決問題的?電子郵件是標準庫的一部分,那爲什麼它不起作用? – Michael 2014-02-19 18:57:25

回答

9

問題不在於你無法連接到服務器,而是因爲某些原因你無法導入email.encoders。你有沒有一個名爲email.py或email.pyc的文件?

+0

爲了解決這個python 2.x,只允許顯式的相對導入:'from __future__ import aboslute_import' – 2016-11-18 18:06:41