3

我正在尝试建立一个与nodemailer它使用的传输类型无关的电子邮件系统。

这是我尝试发送电子邮件的方式:

const config = require('config')
const mailerConfig = config.get('mailer')
const transporter = nodemailer.createTransport(mailerConfig.transport)
transporter.sendMail({
  from: mailerConfig.from,
  to: toEmail,
  subject,
  text: textBody,
  html: htmlBody,
})

这是我的config文件的样子:

const AWS = require('aws-sdk')
module.exports = {
  mailer: {
    from: 'test_sender@domain.com',
    transport: {
      SES: new AWS.SES({
        accessKeyId: 'secret-key',
        secretAccessKey: 'access-key',
        region: 'region',
      }),
    },
  }
}

然后,每当我尝试发送电子邮件时,都会收到以下错误:

error:  TypeError: Key must be a buffer
    at new Hmac (crypto.js:117:16)
    at Object.Hmac (crypto.js:115:12)
    at Object.hmac (/Users/sebi/Work/node_modules/aws-sdk/lib/util.js:401:30)
    at Object.getSigningKey (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4_credentials.js:59:8)
at V4.signature (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4.js:97:36)
at V4.authorization (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4.js:92:36)
at V4.addAuthorization (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4.js:34:12)
at /Users/sebi/Work/node_modules/aws-sdk/lib/event_listeners.js:215:18
at finish (/Users/sebi/Work/node_modules/aws-sdk/lib/config.js:320:7)
at /Users/sebi/Work/node_modules/aws-sdk/lib/config.js:338:9
at /Users/sebi/Work/node_modules/aws-sdk/lib/credentials.js:123:23
at Credentials.refresh (/Users/sebi/Work/node_modules/aws-sdk/lib/credentials.js:194:5)
at Credentials.get (/Users/sebi/Work/node_modules/aws-sdk/lib/credentials.js:121:12)
at getAsyncCredentials (/Users/sebi/Work/node_modules/aws-sdk/lib/config.js:332:24)
at Config.getCredentials (/Users/sebi/Work/node_modules/aws-sdk/lib/config.js:352:9)
at Request.SIGN (/Users/sebi/Work/node_modules/aws-sdk/lib/event_listeners.js:192:22)

请注意,当我AWS.SES()在发送电子邮件的同一个地方进行实例化时,代码可以正常工作。有什么特别的东西config可以破坏密码吗?

4

3 回答 3

2

config 有什么特别之处可以破坏代码吗?

确切地。config包深度合并所有配置。因此它会遍历您的配置,并破坏从AWS.SES(). 您可以尝试将密钥放在配置中,并ses仅在使用时应用它们:

const AWS = require('aws-sdk')
module.exports = {
  mailer: {
    from: 'test_sender@domain.com',
    transport: {
      SES: {
        accessKeyId: 'secret-key',
        secretAccessKey: 'access-key',
        region: 'region',
      },
    },
  }
}

并使用它:

const config = require('config')
const mailerConfig = config.get('mailer')
const transporter = nodemailer.createTransport({
    SES: new AWS.SES(mailerConfig.transport.SES)
})
transporter.sendMail({
  from: mailerConfig.from,
  to: toEmail,
  subject,
  text: textBody,
  html: htmlBody,
})

作为替代方案,您可以直接要求您的配置文件:

const config = require('./config') // depends on your files structure
于 2018-02-19T13:30:39.333 回答
1

在我的情况下 accessKey 和 secretAccessKey 是未定义的。

于 2018-07-26T08:25:09.657 回答
0

我是node-config. node-config当您只想返回纯数据时,这听起来可能是向您的配置对象添加了一些方法的问题。

从 v1.27.0 开始config.util.toObject(someValue)添加了允许您获取给定配置值的普通旧 JavaScript 对象。它记录在我们的utils wiki 页面上

如果你有一个旧版本node-config并且不想升级,你也可以在由返回的配置结构上执行此操作node-config

var plainOldValue = JSON.parse(JSON.stringify(configValue)

这具有删除由添加的方法的效果,node-config并且正是新.toObject()方法在内部所做的。

于 2018-02-28T14:52:19.627 回答