Gmail API not respecting UTF encoding in theme

In the app I'm helping with development, we've added the ability to invite other users and personalize an email invitation, then send it through the Gmail API. I encode it using base64 as the state of the documents, and the emails you send are formatted correctly as they are sent to recipients correctly. This works well for US users who are typing in English, but there are messages from users who have sent emails with non-ASCII characters (like Hebrew) when their messages were garbled when sent.

I tested it and made sure we new Buffer(emailString).toString('base64')

code it correctly - we code it by executing and then replacing certain characters by executing encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')

. I created a random lorem ipsum cyrillic string and encoded it using the interface and registered the base64 encoded string:

VG86IGpvc2h1YXNtb2NrQGdtYWlsLmNvbQ0KQ29udGVudC10eXBlOiB0ZXh0L2h0bWw7IGNoYXJzZXQ9VVRGLTgNCk1JTUUtVmVyc2lvbjogMS4wDQpTdWJqZWN0OiDQndGL0Log0LDQvSDQvNGO0L3QtNC5INC60L7QvdCy0YvQvdGR0YDRiw0KDQrQndGL0Log0LDQvSDQvNGO0L3QtNC5INC60L7QvdCy0YvQvdGR0YDRiywg0Y_QvdCy0YvQvdGP0YDRiyDQutCy0Y7QsNC70YzQuNC30LrQstGO0Y0g0LDQtCDQvNGN0LvRjCwg0Y3QuCDQsNCz0LDQvCDRhdC-0LzRjdGA0L4g0LDQu9GM0YzRgtGL0YDQsCDRjdC-0LYuINCc0L7QtNGO0LYg0LDQu9GP0LrQstGO0LjQtCDRiNGL0L3Rh9C10LHRjtC3INGN0L7QtiDQudC9LCDQutGDINCy0LXQutC2INC50YPQttGC0L4g0YbRgNGP0LssINC00YPQviDQsNGCINC00L7QutGC0Y7QtiDQsNC70YzQuNC60LLRg9Cw0L3QtNC-INC20LrRgNGP0L_RiNGN0YDQuNGCLiDQldC0INC80YvQsCDRidC-0LvRjNGL0LDRgiDRjdC70YzRjNGN0LXRhNGN0L3QtC4g0KvQsNC8INC00LXQutGC0LDQtiDQvNGN0LvRjNGR0YPQtyDQstGN0YDRi9Cw0YAg0LDRgiwg0Y3Qt9GI0Y0g0L_Ri9GA0YLQt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O0LzQvNC-INC80L7Qu9GM0LvQuNC3INC40YDQtdGD0YDRiyDRjdC-0LYg0YvRgiwg0Y3QsCDQutCy0YPQuSDQsNC90ZHQvNCw0Lsg0LXQvdGC0YvRgNC_0YDRi9GC0LDRgNGP0Ygu

      

This is the following line when decoded to UTF8 (I removed the email address):

To: <>
Content-type: text/html; charset=UTF-8
MIME-Version: 1.0
Subject:    

   ,    ,     .     ,    ,     .    .     ,    .    ,    .<br><br>   ,     ,     .    ,     .    .     ,     .     ,     .    ,    .     ,    .

      

The body is fine, but the header is messed up and distorted when it is actually sent to the API:

Actual email sent

Am I doing something wrong here? Is there a way to force the Gmail API to respect the UTF encoding of the header / subject through a flag or setting, or is this a bug?

+3


source to share


2 answers


According to the RFC standard, the email subject MUST be in US ASCII (7 bits).

If you want non-US ASCII characters in a subject, you must use quoted-printable encoding

So your

Subject:    

      

should become



Subject: =?iso-8859-1?Q?=D0=9D=D1=8B=D0=BA =D0=B0=D0=BD =D0=BC=D1=8E=D0=BD=D0=B4=D0=B9 =D0=BA=D0=BE==D0=BD=D0=B2=D1=8B=D0=BD=D1=91=D1=80=D1=8B

      

Edit Updated in response to comment:

RFC 822 / RFC2822 ( https://www.ietf.org/rfc/rfc0822.txt ) Section 2.2 Header Fields:

Header fields are strings consisting of the field name, followed by a colon (":"), followed by a field field, and terminated by CRLF. The name field MUST be composed of printable US-ASCII characters (that is, characters that have values ​​between 33 and 126, inclusive), except for the colon. The body body can be any US-ASCII character except CR and LF. However, a field field may contain a CRLF when used in a fold and unfold header as described in section 2.2.3. All field bodies MUST conform to the syntax described in clauses 3 and 4 of this standard.

US-ASCII refers to the original 7-bit ASCII encoding (0-127).

+3


source


I faced the same problem and I am getting the following information: Using UTF-8 characters in email subject .

So, I'll replace the topic: =?utf-8?B?${convertToBase64(subject)}?=

it works well.



${}

is a variable template, if you want to set

as theme it will look like this:

=?utf-8?B?0J3Ri9C6INCw0L0g0LzRjtC90LTQuSDQutC-0L3QstGL0L3RkdGA0Ys?=

+2


source







All Articles