0

I'm trying to send an email using the gmail rest api, everything works fine except if I try to use special characters in the subject. I've tried different solutions that I've found around like:

example1

`Subject: =?utf-8?B?${email.subject.toString('base64')}?=\n\n` +

result from example 1 enter image description here

example2

`Subject: =?iso-8859-1?Q? ${quotedPrintable.encode(utf8.encode(email.subject))}?=\n\n` +

result from example 2

enter image description here

This is the actual code:

const mail = Buffer.from(`From: ${from}\n` +
    'Content-Type: text/html; charset=UTF-8\n' +
    `To: ${to}\n` +
    `Cc: ${cc}\n` +
    `Bcc: ${bcc}\n` +
    `Subject: =?utf-8?B?${email.subject.toString('base64')}?=\n\n` +
    `${$.html()}`)
    .toString('base64').replace(/\+/g, '-').replace(/\//g, '_');
  const response = await axios.post(`https://www.googleapis.com/gmail/v1/users/${from}/messages/send?access_token=${token}`, {
    raw: mail,
  });

This is a sample of the subject I want to send:

1 - = live unbound!!   que viva España
irokhes
  • 1,643
  • 1
  • 14
  • 20
  • Can I ask you about your question? 1. Can you provide the results of example1 and example2? 2. Is your current script using the method of "example1"? 3. Can you provide a sample value of the subject with the special characters? – Tanaike Nov 05 '19 at 00:44
  • @Tanaike I've updated the description to include the points you asked. My current script is just sending the subject without any coding, somethig like: `Subject: ${email.subject}\n\n` + – irokhes Nov 05 '19 at 07:45
  • Thank you for replying and adding the information. But I noticed that your issue had been resolved. I'm glad your issue was resolved. – Tanaike Nov 05 '19 at 22:40

1 Answers1

0

I've found the solution for this problem

`Subject: =?UTF-8?Q?${quotedPrintable.encode(utf8.encode(email.subject))}?=\n\n`
irokhes
  • 1,643
  • 1
  • 14
  • 20