Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

Issue with smtplib sending mail with unicode characters in Python 3.1

Hello i' ve this problem with unicode emails, when i try to send words in spanish like: "A?adir" or others the system collapse, i've try what says on this link: Python 3 smtplib send with unicode characters and doesn't work.

This is the code of my error:

server.sendmail(frm, to, msg.as_string())
g.flatten(self, unixfrom=unixfrom)
self._write(msg)
self._write_headers(msg)
header_name=h)
self.append(s, charset, errors)
input_bytes = s.encode(input_charset, errors)

UnicodeEncodeError: 'ascii' codec can't encode character 'xf1' in position 7: ordinal not in range(128)

This is the code on the server:

msg = MIMEMultipart('alternative')
frm = "sales@bmsuite.com"
msg['FROM'] = frm

to = "info@bmsuite.com"
msg['To'] = to
msg['Subject'] = "Favor a?adir esta empresa a la lista"

_attach = MIMEText("""Nombre:Prueba; Dirección:Calle A #12.""".encode('utf-8'), _charset='utf-8')
msg.attach(_attach)

server.sendmail(frm, to, msg.as_string())

server.quit()

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can instead just use:

msg = MIMEText(message, _charset="UTF-8")
msg['Subject'] = Header(subject, "utf-8")

But either way you still have issues if your frm = "xxxx@xxxxxx.com" or to = "xxxx@xxxxxx.com" constains unicode characters. You can't use Header there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...