5

Following up this solution about Adding CC and BCC when sending a RawEmail, I'm getting blank the To, Cc, and Bcc fields when receiving the email. For quickest implementation, I'm using this OSS aws-thin-ses-node library.

My send raw email method is defined as (to, cc and bcc are arrays, so I just have a little tweak with the asOptionalArray and asValueFromArray methods to send in the required format as defined in AWS SDK).

What could I improve to fix this minor issue that is nice to have it, although it is sending to all To, Cc and Bcc addresses? I'm attaching my sendRawEmail and getRawMessages methods definitions as follow

sendRawEmail: async ({ to, cc, bcc data}) => {
      let destinations

      if (cc && cc.length && bcc && bcc.length) {
        destinations = [...cc, ...bcc]
        destinations.unshift(asValueFromArray(to, 0))
      } else {
        destinations = to
      }

      const params = {
        Destinations: asOptionalArray(destinations),
        To: to instanceof Array ? to : asOptionalArray(to),
        Cc: asOptionalArray(cc),
        Bcc: asOptionalArray(bcc),
        RawMessage: {
          Data: await getRawMessage(data)
        }
      }
      return client.sendEmail(params)
    }

const getRawMessage = (data) => {
  const template = getReportTemplate()
  const subject = getSubject()
  const reportName = getReportName()
  let sesMail = 'From: noReply <' + noreplyEmail + '>\n'
  sesMail += 'Subject: ' + subject + '\n'
  sesMail += 'MIME-Version: 1.0\n'
  sesMail += 'Content-Type: multipart/mixed; boundary="NextPart"\n\n'
  sesMail += '--NextPart\n'
  sesMail += 'Content-Type: text/html\n\n'
  sesMail += template.report + '\n\n'
  sesMail += '--NextPart\n'
  sesMail += 'Content-Type: application/msexcel; name="' + reportName + '"\n'
  sesMail += 'Content-Transfer-Encoding: base64\n'
  sesMail += 'Content-Disposition: attachment\n\n'
  sesMail += data.toString('base64') + '\n\n'
  sesMail += '--NextPart--'
  // eslint-disable-next-line
  const base64Encoded = new Buffer.from(sesMail).toString('base64')
  return base64Encoded
}

Send Raw email looks like:

email_raw_without_to_cc_bcc

Yajairo87
  • 345
  • 1
  • 8
  • 25
  • 2
    The SO post you have shared, says you should not send anything for `Destinations` param but I see you are sending `destinations` object always which makes to ignore `Cc`,`Bcc`. is that correct? – Imran Aug 10 '19 at 19:52
  • @Imran when sending Destinations param as empty without defining the To, Cc and Bb in the headers, AWS response is a 400 Bad request. I finally figured out a solution. – Yajairo87 Aug 13 '19 at 20:47
  • great!!. Glad its resolved!! – Imran Aug 14 '19 at 02:31

1 Answers1

2

I finally figured out the solution and the problem was that I wasn't defining the To, Cc and Bcc in the ses mail headers, as suggested, my destinations array is empty now and rewrite my

const getRawMessage = (to, cc, bcc, data) => {
  const template = getReportTemplate()
  const subject = getSubject()
  const reportName = getReportName()
  let sesMail = 'From: noReply <' + noreplyEmail + '>\n'
  sesMail += 'To: ' + asValueFromArray(to, 0) + '\n'
  sesMail += cc && cc.length ? 'Cc: ' + asOptionalArray(cc) + '\n' : ''
  sesMail += bcc && bcc.length ? 'Bcc: ' + asOptionalArray(bcc) + '\n' : ''
  sesMail += 'Subject: ' + subject + '\n'
  sesMail += 'MIME-Version: 1.0\n'
  sesMail += 'Content-Type: multipart/mixed; boundary="NextPart"\n\n'
  sesMail += '--NextPart\n'
  sesMail += 'Content-Type: text/html\n\n'
  sesMail += template.report + '\n\n'
  sesMail += '--NextPart\n'
  sesMail += 'Content-Type: application/msexcel; name="' + reportName + '"\n'
  sesMail += 'Content-Transfer-Encoding: base64\n'
  sesMail += 'Content-Disposition: attachment\n\n'
  sesMail += data.toString('base64') + '\n\n'
  sesMail += '--NextPart--'
  // eslint-disable-next-line
  const base64Encoded = new Buffer.from(sesMail).toString('base64')
  return base64Encoded
}
Yajairo87
  • 345
  • 1
  • 8
  • 25