2015-01-07 4 views
1

먼저 메일을 분석 한 후 : 나는이 읽은 :SMTP 인코딩 오류 파이썬 3

지금이 읽은 후 내가 만드는 방법을 알고 utf8 메일을 보내십시오. 그러나 나는 우편물을 전달하고 싶다. 내 (간체) 코드는 다음과 같습니다

msg = email.parser.Parser().parse(sys.stdin) 
# I also tried reading from a file, makes no difference 
# left out some code adding ascii-only headers to the mail 
with smtplib.SMTP(conf.smtp_server, conf.smtp_port) as s: 
    s.starttls() 
    s.ehlo() 
    s.login(conf.smtp_user, conf.smtp_password) 
    s.send_message(msg, conf.smtp_srcadr, destinations) 

내가 얻을 것이 이것이다 :

File "./mailer.py", line 38, in send_mail 
s.send_message(msg, conf.smtp_srcadr, destinations) 
    File "/package/host/localhost/python-3.3/lib/python3.3/smtplib.py", line 822, in send_message 
    g.flatten(msg_copy, linesep='\r\n') 
    File "/package/host/localhost/python-3.3/lib/python3.3/email/generator.py", line 112, in flatten 
    self._write(msg) 
    File "/package/host/localhost/python-3.3/lib/python3.3/email/generator.py", line 164, in _write 
    self._dispatch(msg) 
    File "/package/host/localhost/python-3.3/lib/python3.3/email/generator.py", line 190, in _dispatch 
    meth(msg) 
    File "/package/host/localhost/python-3.3/lib/python3.3/email/generator.py", line 407, in _handle_text 
    super(BytesGenerator,self)._handle_text(msg) 
    File "/package/host/localhost/python-3.3/lib/python3.3/email/generator.py", line 220, in _handle_text 
    self.write(payload) 
    File "/package/host/localhost/python-3.3/lib/python3.3/email/generator.py", line 381, in write 
    self._fp.write(s.encode('ascii', 'surrogateescape')) 
UnicodeEncodeError: 'ascii' codec can't encode characters in position 505-506: ordinal not in range(128) 

내 문제는 : 내가 입력 메일의 구조 방법을 모르겠어요. 나는 정확하게 인코딩 할 수 없다. 모든 종류의 여러 가지 가능성에 대비해야합니다. 어떤 아이디어?

참고 :

msg.as_string() 

잘 작동하고 예상대로 유니 코드 문자가 포함되어 있습니다.

+0

관련 항목 : [Python을 사용하여 전자 메일을 보내려면 어떻게해야합니까?] (http://stackoverflow.com/a/9274387/4279)는 ASCII가 아닌 문자를 사용하여 전자 메일을 보내는 방법을 보여주는 코드 예제를 제공합니다 (Python 2 3. 다른 [유니 코드 전자 메일 코드 예제] (http://stackoverflow.com/a/20787826/4279) – jfs

답변

0

오케이. 해킹 된 솔루션을 찾았습니다. 이메일 대신. 메시지 기반 send_message 바이트 기반의 sendmail을 사용합니다.

s.sendmail(conf.smtp_srcadr, destinations, msg.as_string().encode('utf-8')) 

은 정말 파이썬 표준 라이브러리에서 (Bytes-)Generator 또는 다른 이메일 툴을 기반으로하는 솔루션을 선호하는 것이다.