I am trying to use unicode in an email subject with Python.
Code is:
pub_unicode = u"\U0001f37a"
money_unicode = u"\U0001f4b0"
unicode_b64_utf = base64.b64encode(bytes(u'\U0001f37a PubBotC4.1 \U0001f4b0', "utf-8"))
unicode_b64_utf_decoded = base64.b64decode(unicode_b64_utf).decode("utf-8", "ignore")
print("base64 encoded to utf-8 and then base64 decoded", unicode_b64_utf_decoded)
print ("unicode symbols", pub_unicode, money_unicode)
print ("ascii format", ascii(pub_unicode), ascii(money_unicode))
version = " PubBotC4.1 "
#version = unicode_b64_utf_decoded # bot crash
email_address = "user@gmail.com"
email_msg = "test"
blynk.email(email_address, version, email_msg)
Script prints out the following:
base64 b'ZGF0YSB0byBiZSBlbmNvZGVk'
base64 encoded to utf-8 and then base64 decoded 🍺 PubBotC4.1 💰
unicode symbols 🍺 💰
ascii format '\U0001f37a' '\U0001f4b0'
But if I change from version = " PubBotC4.1 "
to version = unicode_b64_utf_decoded
the script crashes with the following error:
File "PubBot4.1app.py", line 816, in blynk_connected
blynk.email(email_address, version, email_msg)
File "/usr/local/lib/python3.6/dist-packages/BlynkLib.py", line 264, in email
self._send(self._format_msg(MSG_EMAIL, to, subject, body))
File "/usr/local/lib/python3.6/dist-packages/BlynkLib.py", line 151, in _format_msg
data = ('\0'.join(map(str, args))).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f37a' in position 32: ordinal not in range(128)
Has anyone managed to use unicode in email messages with Python?