Aws-sdk-js: SES: UTF8 Chars in sender name of email

Created on 19 Jun 2017  Â·  4Comments  Â·  Source: aws/aws-sdk-js

I'm trying to send emails from a sender name with characters not being in the 7-bit ASCII range.

ses.sendEmail({
  ...
  Source: fromName + ' <' + fromEmail + '>',
  ...
}, ...

This works well as long as fromName does not have any UTF-8 chars like ‘. Those chars are stripped from the sender name.

How do I encode them properly so that they are being displayed?

guidance

Most helpful comment

Okay thanks. Here's my actionable input for the next person searching for this issue:

You'll need two packages to properly encode the sender name.

npm i q-encoding --save
npm i utf8 --save

And then, you can just:

var qencode = require('q-encoding');
var utf8 = require('utf8');
var MIME = (string) => '=?UTF-8?Q?' + qencode.encode(utf8.encode(string)) + '?=';

...

ses.sendEmail({
  ...
  Source: MIME(fromName) + ' <' + fromEmail + '>',
  ...
}, ...

All 4 comments

You would need to use MIME encoded-word syntax to use non-ASCII characters in the Source text. There's some additional discussion in the SES API reference:

In all cases, the email address must be 7-bit ASCII. If the text must contain any other characters, then you must use MIME encoded-word syntax (RFC 2047) instead of a literal string. MIME encoded-word syntax uses the following form: =?charset?encoding?encoded-text?=. For more information, see RFC 2047.

Okay thanks. Here's my actionable input for the next person searching for this issue:

You'll need two packages to properly encode the sender name.

npm i q-encoding --save
npm i utf8 --save

And then, you can just:

var qencode = require('q-encoding');
var utf8 = require('utf8');
var MIME = (string) => '=?UTF-8?Q?' + qencode.encode(utf8.encode(string)) + '?=';

...

ses.sendEmail({
  ...
  Source: MIME(fromName) + ' <' + fromEmail + '>',
  ...
}, ...

Note it should also be possible to avoid the extra dependencies by using base64:

const base64ToName = Buffer.from(toName).toString('base64');
const finalToName = `=?UTF-8?B?${base64ToName}?= <${toEmail}>`;

I only tested it with Destination.ToAddresses but it should be the same thing

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

Was this page helpful?
0 / 5 - 0 ratings