Internationalised email subjects
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://mail.python.org/mailman/listinfo/python-list
Re: Internationalised email subjects
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: Internationalised email subjects
Seems some characters are missing from my last post. The line that says: h = Header(' ', 'GB2312') should say: h = Header(' ', 'GB2312') -- http://mail.python.org/mailman/listinfo/python-list
Re: Internationalised email subjects
That's really strange. The chinese characters I am inputing into the post are not being displayed. Basically, what I am doing is this: h = Header('(Some Chinese characters inserted here', 'GB2312') And when I run this code, I receive the following error message: UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 2-3: illegal multibyte sequence Any idea what I may be doing wrong? How do I convert Chinese characters into something like p\xf6stal in the original code posted by Martin? Can someone point me in the right direction? I'm not even sure what class/method to look into for this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Internationalised email subjects
Thanks Martin, The "Some Chinese characters" are loaded from a MySQL table and are encoded in GB2312 format. I've added the following line at the top of the code: # -*- coding: GB2312 -*- I've also added the following line into the code: h = Header(subject.encode('GB2312'), 'GB2312') Note that the 'subject' variable consists of GB2312 encoded text, so I am not sure if it is necessary to call the subject.encode('GB2312') method. When I try to execute this code, I get the following error: File "/home/web88/html/app/test.py", line 17, in Header(subject.encode('GB2312'), 'GB2312') LookupError: unknown encoding: GB2312 Any idea what may be wrong? -- http://mail.python.org/mailman/listinfo/python-list
Re: Internationalised email subjects
Thanks Richie, I've tried removing the encode('GB2312') line, so the code looks like this: h = Header(subject, 'GB2312') However, this line still causes the following error message: Traceback (most recent call last): File "/home/web88/html/app/sendmail.py", line 314, in h = Header(subject, 'GB2312') File "/usr/lib/python2.2/email/Header.py", line 188, in __init__ self.append(s, charset, errors) File "/usr/lib/python2.2/email/Header.py", line 272, in append ustr = unicode(s, incodec, errors) LookupError: unknown encoding: gb2312 ) Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
Re: Internationalised email subjects
I'm an idiot! Gabriel, you're right! Turns out the ISP was running Python 2.3, which has known issues with the GB2312 codec. They've upgraded to 2.4 and now everything runs smoothly! -- http://mail.python.org/mailman/listinfo/python-list