0

My test code started failing with GAE version 1.7.1. I went back to 1.7.0 to wait for a fix but my test code is still failing with 1.7.2. Here is a minimal test that causes the failure:

mail.send_mail("sender@example.com", "receiver@example.com", 
               u"sübject".encode("utf-8"), "body")
mail_list = self.mail_stub.get_sent_messages()

And I get the following error:

Traceback (most recent call last):
  File "/Users/.../tests/unit_tests.py", line 19, in testTest
    mail_list = self.mail_stub.get_sent_messages()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub.py", line 184, in WrappedMethod
    return method(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/mail_stub.py", line 183, in get_sent_messages
    email_message = mail.EmailMessage(mime_message=mime_message)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/mail.py", line 742, in __init__
    self.update_from_mime_message(mime_message)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/mail.py", line 1213, in update_from_mime_message
    super(EmailMessage, self).update_from_mime_message(mime_message)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/mail.py", line 1093, in update_from_mime_message
    subject = _decode_and_join_header(mime_message['subject'], separator=u'')
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/mail.py", line 558, in _decode_and_join_header
    for s, c in email.header.decode_header(header))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/mail.py", line 558, in <genexpr>
    for s, c in email.header.decode_header(header))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)

Could you let me know if I am doing something wrong or if this is a GAE bug?

new name
  • 15,861
  • 19
  • 68
  • 114
  • I do not understand, why you encode the subject. Does your python file contains in the top two lines: #!/usr/bin/python # -*- coding: utf-8 -*- – voscausa Oct 29 '12 at 00:24
  • @voscausa, my Python file has the coding line. My understanding is that you need to encode a non-ascii subject and that GAE does not do that for you. – new name Oct 29 '12 at 00:28
  • Yes, but you don not send it to the outside world, you send it to the Mail API. – voscausa Oct 29 '12 at 00:39
  • @voscausa, that is a call to the Mail API. – new name Oct 29 '12 at 00:40
  • But with encode( "utf-8" ) you convert it from unicode to bytes for the Mail API. See this article: http://www.evanjones.ca/python-utf8.html – voscausa Oct 29 '12 at 00:46
  • If you use Python 2.7 you can use: from __future__ import unicode_literals to make sure you have unicode string literals. – voscausa Oct 29 '12 at 01:05

1 Answers1

0

After reading this post and this page, this seems to be the right way to do it:

from email import header
mail.send_mail("sender@example.com", "receiver@example.com", 
               str(header.Header(u"sübject", "utf-8")), "body")

And it is working for me so far.

Community
  • 1
  • 1
new name
  • 15,861
  • 19
  • 68
  • 114