2014-06-11 3 views
0

나는 메일을 보내는 Python3 프로그램이있다. 그것은 여러 컴퓨터에서 완벽하게 작동하지만, 그렇지 않은 컴퓨터가 있습니다. 이Python3에서 전자 메일을 보낼 때 ASCII 인코딩 오류가 발생합니까?

'ascii' codec can't encode character '\whatever' in position x: ordinal not in range(128) 

그리고 :

나는 다음 오류가 발생했습니다 ... 나는 노력 등 'N'또는 'A', 'E'와 같은 특수 문자가없는 경우이 일에만 작동

html = '<h1>niñería</h1>' 
text = 'niñería' 

mail = MIMEMultipart('alternative') 
mail['From'] = '[email protected]' 
mail['To'] = '[email protected]' 
mail['Cc'] = '' 
mail['Subject'] = 'My subject' 

# Record the MIME types of both parts - text/plain and text/html. 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 

# Attach parts into message container. According to RFC 2046, the last 
# part of a multipart message, in this case the HTML message, is best 
# and preferred. 
mail.attach(part1) 
mail.attach(part2) 

msg_full = mail.as_string() 

server = smtplib.SMTP('smtp.gmail.com:587') 
server.starttls() 
server.login('[email protected]', 'my_password') 
server.sendmail('[email protected]', ['[email protected]'], msg_full) 
server.quit() 

내 이메일의 내용을 인식 만들 수 .encode('utf-8') 또는 .decode('utf-8') 같은 어떤 마술 라인이 있습니까 : 내 코드는?

답변

0

OK, 해결.

msg_full = mail.as_string() 

대신 쓰기 :

msg_full = mail.as_string().encode() 
난 단지 줄을 변경했다
관련 문제