2009-12-22 6 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

답변

9

문제는 서버에 연결할 수없는, 그것은 당신이 어떤 이유로 email.encoders를 가져올 수없는 것이 있습니다. 혹시 email.py 또는 email.pyc라는 파일이 있습니까?

+0

파이썬 2.x에서 이것을 수정하려면 명시 적 상대 임포트 만 허용 :'from __future__ import aboslute_import' –

관련 문제