将本地值传递给电子邮件模板使用Express-mailer

Kha*_*yen 5 email node.js express

我想在用户注册后发送一封感谢邮件,但我不知道如何将用户名传递给我的电子邮件模板.我有以下代码:

app.mailer.send('email', {
  to: 'newuser@gmail.com', // REQUIRED. This can be a comma delimited string just like a normal email to field. 
  subject: 'Test Email', // REQUIRED.
  user: req.body.user, // All additional properties are also passed to the template as local variables.
}, function (err) {
  if (err) {
    // handle error
    console.log(err);
    res.send('There was an error sending the email');
    return;
  }
  res.send('Email Sent');
});
Run Code Online (Sandbox Code Playgroud)

电邮模板:

<html>
<head>
    <title>{{subject}}</title>
</head>
<body>
    Thank you {{user}}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Eve*_*ers 2

您可以将属性传递给请求。

{ to: 'newuser@gmail.com', // REQUIRED. This can be a comma delimited string just like a normal email to field. subject: 'Test Email', // REQUIRED. username: req.body.username, myVar: 'username' }

该变量myVar作为局部变量传递给模板