-
Notifications
You must be signed in to change notification settings - Fork 135
UnicodeEncodeError: 'ascii' codec can't encode character [with ugettext_lazy] #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Anymail is meant to handle Unicode, but there may be some cases we missed.
|
Hmm, I'm having trouble reproducing the problem. This seems to work with the Mailgun backend: # -*- coding: utf-8 -*-
from django.core.mail.message import EmailMessage
msg = EmailMessage(
u"Wymagane zresetowanie hasła do GSMtasks",
u"Kliknij aby zresetować hasło",
'[email protected]', ['[email protected]'])
msg.send() What version of requests do you have installed, and what version of python? (I've just tested this on python 2.7.11 with both requests 2.9.1 and 2.11.1.) |
Looking at the requests source, it seems like the only way you'd see this error (in python 2.x) is if your subject were somehow a unicode string, but Is there any chance the subject is a Django ugettext_lazy translation string? If so, you could call django.utils.encoding.force_text on it before placing it in the EmailMessage subject. (I'm trying to figure out whether Anymail should be calling force_text for you before handing off to requests. I have this vague memory of a similar problem with the from_email, which we ended up fixing in Django's EmailMessage code.) |
@madisvain were you able to track this down to a Django lazy translation string, or otherwise reproduce it? As I mentioned above, Anymail+Mailgun seems to handle the unicode characters just fine. |
@medmunds I had the same problem and it was due to a lazy translation string, force_text fixed it. |
@anderspetersson thanks for the confirmation; I updated the issue title to reflect this. There are actually some potentially-serious problems with using ugettext_lazy but not explicitly forcing the result to non-lazy text somewhere. Even if you're not seeing encoding errors, you could end up with unexpected results. The issue isn't specific to Anymail. I posted to the django-dev list seeking clarification; will see what comes out of the discussion there. |
OK, I've become convinced that as a package with "Django" in its name, one of django-anymail's responsibilities is handling ugettext_lazy objects for its users. Anymail is the bridge between the Django world and non-Django ESP APIs, so it needs to handle converting Django lazy strings for consumption by those APIs. The fix needs to go into Anymail somewhere in the base backend, because it applies to every EmailMessage attribute that might be text. (Or that might contain text -- consider |
In BasePayload, ensure any Django ugettext_lazy (or similar) are converted to real strings before handing off to ESP code. This resolves problems where calling code expects it can use lazy strings "anywhere", but non-Django code (requests, ESP packages) don't always handle them correctly. * Add utils helpers for lazy objects (is_lazy, force_non_lazy*) * Add lazy object handling to utils.Attachment * Add lazy object handling converters to BasePayload attr processing where appropriate. (This ends up varying by the expected attribute type.) Fixes #34.
In BasePayload, ensure any Django ugettext_lazy (or similar) are converted to real strings before handing off to ESP code. This resolves problems where calling code expects it can use lazy strings "anywhere", but non-Django code (requests, ESP packages) don't always handle them correctly. * Add utils helpers for lazy objects (is_lazy, force_non_lazy*) * Add lazy object handling to utils.Attachment * Add lazy object handling converters to BasePayload attr processing where appropriate. (This ends up varying by the expected attribute type.) Fixes #34.
Seems like anymail does not handle unicode correctly.
The text was updated successfully, but these errors were encountered: