Sendgrid-python: Mail Helper throws ascii uncode error on Russian last name

Created on 30 Nov 2016  路  4Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

Currently Mail Helper does not support non-ascii names. "Sirmen K谋z谋lcan" would fail on a last name subsitution.

Steps to Reproduce

  1. Create a Sendgrid email using helper
  2. Add mail.personalizations[0].add_substitution(Substitution(":lastname", "K谋z谋lcan"))
  3. It should throw UnicodeEncodeError: 'ascii' codec can't encode character u'\u0131' in position 1: ordinal not in range(128)

Technical details:

  • sendgrid-python Version: master (latest commit: [commit number])
  • Python Version: 2.7.2
help wanted community enhancement

Most helpful comment

Hi @iamgollum

Was taking a look at the issue you were experiencing, and it's definitely a bit annoying to constantly need to encode Python 2.7 strings, which are by default in ASCII, to UTF-8.

It would be great if the helper methods would enforce encoding based on Python version, but in the meantime if you need to continue using the SendGrid library w/ Python 2.7 you can try inserting:

#!/usr/bin/python
# -*- coding: utf-8 -*-

at the beginning of your .py file that's sending mail. Within most code editors (I tested on Vim), this will safely change the default encoding to UTF-8 when the file is executed. This allowed me to send mail with the substitution value "K谋z谋lcan" without issue.

All 4 comments

Hi @iamgollum,

Thanks for the heads up! We will investigate.

Hi @iamgollum

Was taking a look at the issue you were experiencing, and it's definitely a bit annoying to constantly need to encode Python 2.7 strings, which are by default in ASCII, to UTF-8.

It would be great if the helper methods would enforce encoding based on Python version, but in the meantime if you need to continue using the SendGrid library w/ Python 2.7 you can try inserting:

#!/usr/bin/python
# -*- coding: utf-8 -*-

at the beginning of your .py file that's sending mail. Within most code editors (I tested on Vim), this will safely change the default encoding to UTF-8 when the file is executed. This allowed me to send mail with the substitution value "K谋z谋lcan" without issue.

@LiYChristopher Thank you sir. I will try this out this weekend. Much appreciated!

@iamgollum I encountered a similar error, and recently made a small code tweak to address this issue with unicode characters being used in Substitutions. Can you update your version of SendGrid and see if the issue still exists? I think it should not be a problem any more.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thinkingserious picture thinkingserious  路  5Comments

hanhaa picture hanhaa  路  4Comments

hugomallinson picture hugomallinson  路  3Comments

andriisoldatenko picture andriisoldatenko  路  4Comments

MadReal picture MadReal  路  4Comments