我在Python3中有一个发送邮件的程序。它在几台计算机上运行完美,但有一台没有。我尝试过这个只有在没有'ñ'或'á','é'等特殊字符的情况下才有效...
我收到了下一个错误:
'ascii' codec can't encode character '\whatever' in position x: ordinal not in range(128)
这是我的代码:
html = '<h1>niñería</h1>'
text = 'niñería'
mail = MIMEMultipart('alternative')
mail['From'] = 'my_account@gmail.com'
mail['To'] = 'destiny@gmail.com'
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('my_account@gmail.com', 'my_password')
server.sendmail('my_account@gmail.com', ['destiny@gmail.com'], msg_full)
server.quit()
是否有像.encode('utf-8')
或.decode('utf-8')
这样可以识别我的电子邮件内容的魔术行?
答案 0 :(得分:0)
好的,解决了。我只需改变这条线:
msg_full = mail.as_string()
然后写下来:
msg_full = mail.as_string().encode()