problem sending subject/body with non-ascii characters
When I send an email with non-ascii characters in the subject or body, these appear as question marks when the email arrives. Perhaps this is due to me sending the email incorrectly, or perhaps this is a core sendgrid issue more than a sendgrid-python issue, but I have not been able to sort it out. I have tried setting the encoding of my Python script (via the Python "magic comment"), but that doesn't help.
Here is a simple script that shows what I am talking about:
import sendgrid s = sendgrid.Sendgrid('username', 'password', secure=True) message = sendgrid.Message('[email protected]', u'Que día más bueno hace', u'Que día más bueno hace') message.add_to('[email protected]') s.web.send(message)
I have also tried these two versions of the sendgrid.Message call:
message = sendgrid.Message('[email protected]', 'Que día más bueno hace', 'Que día más bueno hace') message = sendgrid.Message('[email protected]', u'Que d\u00EDa m\u00E1s bueno hace', html=u'Que d\u00EDa m\u00E1s bueno hace')
In all three cases, the emails come through with question marks where the non-ascii characters are.
I also tried adding this filter, but it did not help:
message.add_header("Content-Type", "text/html charset=utf-8")
Am I making an obvious mistake? Thanks.
I'm also getting this issue. Below is the most recent traceback:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1687,\ in wsgi_app response = self.full_dispatch_request()
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1360,\ in full_dispatch_request rv = self.handle_user_exception(e)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1358,\ in full_dispatch_request rv = self.dispatch_request()
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1344,\ in dispatch_request return self.view_functions[rule.endpoint](**req.view_args)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-1.10.0.28/newrelic/api/object_wrapper.py", line 220, in __call__ self._nr_instance, args, kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-1.10.0.28/newrelic/api/function_trace.py", line 93, in literal_wrapper return wrapped(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-1.10.0.28/newrelic/api/object_wrapper.py", line 220, in __call__ self._nr_instance, args, kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-1.10.0.28/newrelic/api/transaction_name.py", line 51, in literal_wrapper return wrapped(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/flask_login.py", line\ 496, in decorated_view return fn(*args, **kwargs)
File "/app/opencnam/apps/portal/views.py", line 231, in billing s.web.send(message)
File "/app/.heroku/python/lib/python2.7/site-packages/sendgrid/transport/web.py", line 82, in send data = urllib.urlencode(data, 1)
File "/app/.heroku/python/lib/python2.7/urllib.py", line 1337, in urlencode \ l.append(k + '=' + quote_plus(str(elt)))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)
Any solution to this? I am getting this error for non-ascii characters :
"UnicodeEncodeError('ascii', u'Cl\\xe9mence example', 2, 3, 'ordinal not in range(128)')"
It looks as if this library isn't really maintained, at the moment =/
I like sendgrid service but it is quit surprising that they dont maintain their own library.
If you look at the other issues, we do maintain this library as well as our other libraries. We have a very small team that curates these libraries in addition to our normal duties. We depend on the community to help us out. Failing tests and pull requests are greatly appreciated and will certainly help us prioritize issues.
I should have responded earlier. (I'm the person who started this thread.)
I was able to completely resolve this issue by simply doing this:
subject_utf8 = subject.encode('utf-8') body_utf8 = body.encode('utf-8') bodyHtml_utf8 = bodyHtml.encode('utf-8')
...and then using the "xxx_utf8" variables in the
message = sendgrid.Message("[email protected]", subject_utf8, text=body_utf8, html=bodyHtml_utf8)
Works great with non-ascii characters. Hope that helps.
Would be open to adding a note in the docs about this if people feel that it would be helpful.
@ricpdx's solution works, but I think it makes more sense to make the API do this automatically. That way every developer doesn't have to think about it.
@ricpdx this was my solution and solved my issue:
subject = subject.encode("utf_8").decode("unicode_escape")
body = body.encode("utf_8").decode("unicode_escape")
I tried this and it doesn't work
Hello @carlosds730,
Do you mind opening a new ticket and proving the sample code so we can try to reproduce and fix?
Thanks!