Yajairo87
Yajairo87

Reputation: 345

Email not showing To:, Cc, and Bcc in Amazon SES SendRawEmail (Node.JS)

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

Upvotes: 5

Views: 4126

Answers (1)

Yajairo87
Yajairo87

Reputation: 345

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
}

Upvotes: 2

Related Questions