SendGrid事务模板Netlify函数错误

时间:2018-11-01 04:41:59

标签: node.js aws-lambda sendgrid netlify sendgrid-templates

使用node.js和netlifys函数时,SendGrid事务处理模板遇到一些问题。

当我将function.js文件中的原始模板html用于msg.html属性时,它可以正常工作。

当我使用SendGrid v2替换时,电子邮件将发送到我的电子邮件,但是车把属性例如{{name}}不会放在电子邮件中。

我收到的错误是

5:18:42 PM: TypeError: s is not a function
5:18:42 PM: {"errorMessage":"TypeError: s is not a function"}

function.js是

const sgMail = require('@sendgrid/mail')

exports.handler = function (event, context, callback) {

  sgMail.setApiKey(process.env.SENDGRID_API_KEY)
  sgMail.setSubstitutionWrappers('{{', '}}')
  let body = JSON.parse(event.body)

  let msg = {
    to: process.env.CONTACT_MAIL_TO,
    from: process.env.CONTACT_MAIL_FROM,
    subject: 'Website Contact Form',
    text: 'Someone has filled out a form on your website, check out the message they left and get in touch with them :)',
    templateId: 'd-39c15b77271449548e2f13b66920f808',
    html: '<p>Hello HTML world!</p>',
    dynamic_template_data: {
      subject: 'Testing Templates',
      name: body.name,
      phone: body.phone,
      email: body.email,
      message: body.message,
      website: body.website
    }
  }

  sgMail.send(msg).then(() => {
    console.log(`Contact form sent from: ${process.env.CONTACT_MAIL_FROM}, to: ${process.env.CONTACT_MAIL_TO}, with name: ${body.name}`)
    callback()
  }).catch(e => {
    console.error(e.toString())
    callback(e.toString())
  })
}

package.json具有以下

 "engines": {
    "node": ">= 8.0.0",
    "npm": ">= 3.0.0"
  }
  "dependencies": {
    "@sendgrid/mail": "^6.3.1"...

以下是我如何在https://sendgrid.com/dynamic_templates

上使用模板的标记和把手的示例
        <div><span>Name:&nbsp;</span>{{name}}</div>
        <div><span>Email:&nbsp;</span>{{email}}</div>
        <div><span>Phone:&nbsp;</span>{{phone}}</div>
        <h1><span>Message</span></h1>
        <div><span>Message:&nbsp;</span>{{message}}</div>

我也在使用netlify-lambda压缩功能文件。

0 个答案:

没有答案