javascript 将动态模板用于sendgrid(node.js)

rnmwe5a2  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(150)

我尝试通过SendGrid直接以HTML格式发送一个可行的。但是,它在电子邮件中以简单的HTML格式返回
我的代码:

const message = {
  to: email,
  from: {
    email: "christianguimaraes1996@gmail.com",
  },
  subject: "Forgot Password",
  text: "TOKEN",
  token: token,
  html: "<p> token: {{{token}}}} </p>",
}
sgMail.send(message)

return res.send()

在电子邮件中是这样收到的:

token: {{{token}}}}
e1xvtsh3

e1xvtsh31#

像这样试试,

const message = {
      to: event.to,
      content: {
        token: "your token"
      },
      cc: event.cc,
      bcc: event.bcc,
      templateId: 'your dynamic tempalte ID from sendgrid"
 }

注意:在动态模板中,它必须像Ex: Hello {{token}}这样
content中的key value应该与您的template value expression匹配。

06odsfpq

06odsfpq2#

试试这个

const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(
  "kjhgdkikgjdguwuywy97397397nvsnbsjgjgs"
);   

async function sendEmail(data) {
  const msg = {
    //extract the email details
    to: data.receiver,
    from: data.sender,
    templateId: "6475hsfhsfhfyrshshfs",
    //extract the custom fields
    dynamic_template_data: {
      firstName: data.name,
      verificationToken: data.verificationToken,
      
    },
  };
  //send the email

  try {
    await sgMail.send(msg);
   
  } catch (error) {
    console.log(error);
  }
}

相关问题