Dealing with non-ASCII characters in email headers

Hi!

I had troubles with working on non-latin characters for my email header (gmail api). I implemented something like this to help with the conversion:

var subj = startTrigger.data.subject.split(/([^\x00-\x7F]+)/)
var res = []

function rfc2047(str) {
  const base64 = btoa(unescape(encodeURIComponent(str)));
  return `=?utf-8?B?${base64}?=`
}

subj.forEach((x) => {
  if(/[^\x00-\x7F]/.test(x)) {
    res.push(rfc2047(x))
  } else {
    res.push(x)
  }
})

return res.join(' ')

I was wondering though how others perform this as I can't simply pass on the subject as is from a webhook payload trigger. I might be overdoing it and there might be an easier way of doing this.

I have a similar issue with non-ascii characters in file names bombing an API I use.

I just did this:

const correctedFileName = fileInput.files[0].name.replace(/[^\x00-\x7F]/g, "")

So I think you would use: startTrigger.data.subject.replace(/[^\x00-\x7F]/g, "")

Got it from ChatGPT and it seems to be working for me so far.

1 Like

Oh yeah, easy replace. But I had to keep the non-ascii characters as they are personalized email subjects catering to our clients biz names. Soz for missing that detail in the initial post.

Got mine from ChatGPT as well after a couple of wrong prompts.