Search code examples
javascriptamazon-ses

AWS sendRawEmail - InvalidParameterValue: Empty header names are illegal


I'm using the latest JS SDK, and cannot send a raw email without a confusing error.

Here's my code:

  const msg = mimemessage.factory({
    contentType: 'text/plain',
    contentTransferEncoding: 'base64',
    body: 'hello world'
  })

  msg.header('From', 'Admin <hello@condobeaver.com>')
  msg.header('To', destination)
  msg.header('Subject', 'Customer service contact info')

  const command = new SendRawEmailCommand({
    Destinations: [destination],
    Source: 'hello@condobeaver.com',
    RawMessage: { Data: msg.toString() }
  })

  console.log(command)

  const response = await client.send(command)

The output of command is:

SendRawEmailCommand2 {
  middlewareStack: {
    add: [Function: add],
    addRelativeTo: [Function: addRelativeTo],
    clone: [Function: clone],
    use: [Function: use],
    remove: [Function: remove],
    removeByTag: [Function: removeByTag],
    concat: [Function: concat],
    applyToStack: [Function: cloneTo],
    resolve: [Function: resolve]
  },
  input: {
    Destinations: [ 'ilia.reingold@gmail.com' ],
    Source: 'hello@condobeaver.com',
    RawMessage: {
      Data: 'Content-Type: text/plain\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        'From: Admin <hello@condobeaver.com>\r\n' +
        'To: ilia.reingold@gmail.com\r\n' +
        'Subject: Customer service contact info\r\n' +
        '\r\n' +
        'hello world'
    }
  }
}

It seems to have everything I need: to/from/subject/content.

Yet when I run it, I get:

InvalidParameterValue: Empty header names are illegal.

What header is empty?


Solution

  • The JS SDK takes care of the base64 encoding for you, so you don't need to encode the msg before sending.

      const msg = mimemessage.factory({
        contentType: 'text/plain',
        body: 'hello world'
      })
    
      msg.header('From', 'Admin <hello@condobeaver.com>')
      msg.header('To', destination)
      msg.header('Subject', 'Customer service contact info')
    
      const command = new SendRawEmailCommand({
        Destinations: [destination],
        Source: 'hello@condobeaver.com',
        RawMessage: { Data: msg.toString() }
      })
    
      console.log(command)
    
      const response = await client.send(command)
    

    (Notice the contentTransferEncoding: 'base64' is removed)

    If you get

    TypeError: Cannot read property 'byteLength' of undefined
    

    just replace RawMessage: { Data: msg.toString() } with RawMessage: { Data: Buffer.from(msg.toString()) }