国际化电子邮件主题 [英] Internationalised email subjects

查看:112
本文介绍了国际化电子邮件主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写一个简单的电子邮件程序,它将在邮件主题和正文中发送包含中文字符的
电子邮件。

我没有遇到任何问题电子邮件正文在电子邮件客户端中正确显示

,但电子邮件主题和

发件人姓名(也是中文)是乱码且不是

在电子邮件客户端中正确显示。


这是代码片段:


writer = MimeWriter.MimeWriter(out)

headers = {" From" ;: senderName +''<''+ senderName +''>''," To":

recipientEmail," ;回复:senderEmail}


writer.addheader(" Subject",subject)

writer.addheader(" MIME-Version", 1.0)

writer.addheader(''From'',headers [''From''])

writer.addheader(''To'' ,标题[''To''])

writer.addheader(''回复'',标题[''回复''])


我是Python的新手(并且编程为并且我有一个很好的时间围绕着国际化的功能来支撑我的价格,所以希望有人可以指出我的权利

方向。是否有一种不同的方法我需要使用

发件人名称和主题是否正确显示?我缺少一个

的额外步骤吗?一些示例代码会非常有用。


谢谢!

I am writing a simple email program in Python that will send out
emails containing Chinese characters in the message subject and body.
I am not having any trouble getting the email body displayed correctly
in Chinese inside the email client, however the email subject and
sender name (which are also in Chinese) are garbled and are not
displayed correctly in the email client.

Here is the code snippet:

writer = MimeWriter.MimeWriter(out)
headers = {"From": senderName + '' <'' + senderName + ''>'', "To":
recipientEmail, "Reply-to": senderEmail}

writer.addheader("Subject", subject)
writer.addheader("MIME-Version", "1.0")
writer.addheader(''From'', headers[''From''])
writer.addheader(''To'', headers[''To''])
writer.addheader(''Reply-to'', headers[''Reply-to''])

I''m quite new to Python (and programming in general) and am having a
hard time wrapping my head around the internationalization functions
of Python, so was hoping someone could point me in the right
direction. Is there a different method I need to use in order for
the sender name and subject to be displayed correctly? Is there an
extra step I am missing? Some sample code would be very helpful.

Thanks!

推荐答案

来自:
http://docs.python.org/lib /module-email.header.html
From:
http://docs.python.org/lib/module-email.header.html

>>来自电子邮件.message import消息
来自email.header import标题
msg =消息()
h =标题(''p\xf6stal'',''iso-8859-1'')
msg [''主题''] = h
打印msg.as_string()
>>from email.message import Message
from email.header import Header
msg = Message()
h = Header(''p\xf6stal'', ''iso-8859-1'')
msg[''Subject''] = h
print msg.as_string()



主题: =?iso-8859-1?q?p = F6stal?=


/ Martin

Subject: =?iso-8859-1?q?p=F6stal?=

/Martin


谢谢马丁,我实际上有之前阅读该页面。

让我困惑的部分就是:


h = Header(''p\xf6stal'',''iso-8859-1''' )


我尝试过使用:


h =标题(''''''''GB2312'')


但是当我运行代码时,我收到以下错误:


UnicodeDecodeError:''gb2312''编解码器无法解码2-3位的字节:

非法多字节序列


为了将中文

字符编码为GB2312字符,我需要做些什么设置?

Thanks Martin, I actually have read that page before. The part that
confuses me is the line:

h = Header(''p\xf6stal'', ''iso-8859-1'')

I have tried using:

h = Header('' '', ''GB2312'')

but when I run the code, I get the following error:

UnicodeDecodeError: ''gb2312'' codec can''t decode bytes in position 2-3:
illegal multibyte sequence

Is there something I need to do in order to encode the Chinese
characters into the GB2312 character set?


我的上一篇文章中似乎缺少一些字符。

的行说:


h =标题('''''''GB2312'')


应该说:


h =标题(''''''''GB2312'')
Seems some characters are missing from my last post. The line that
says:

h = Header('' '', ''GB2312'')

should say:

h = Header('' '', ''GB2312'')


这篇关于国际化电子邮件主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆