我已经使用节点js开发了REST服务。 我正在使用节点邮件程序发送电子邮件,这是我的示例代码:
var client = nodemailer.createTransport(sgTransport(options));
var email = {
from: 'noreply@website.com',
to: user.email,
subject: 'Registration successfully confirmed',
text: 'Hi welcome to our group'
};
client.sendMail(email, function(err, json){
if (err){
return res.status(500).send({ msg: err.message });
}
else {
res.status(200).send({status: 'ok', data: { msg: 'A verification email has been sent to ' + user.email + '.'}} )
}
});
现在,当客户在POST中发送国家代码请求时,我想发送带有翻译的邮件文本。 我如何将所有文本邮件存储到配置文件中并通过配置在我的控制器中读取?
谢谢
答案 0 :(得分:1)
您可以将其保存为json文件,然后从您的控制器保存require it。
示例emails.json文件:
{
"registration_confirmed": {
"en": {
"text": "Registration successfully confirmed",
"subject": "Hi welcome to our group"
},
"it": {
"text": "Registrazione confermata",
"subject": "Ciao, benvenuto nel nostro gruppo"
}
}
}
用法(在您的控制器中):
const emails = require('./emails.json');
const national_code = req.body.national_code;
const email_to_send = {
from: 'noreply@website.com',
to: user.email,
subject: email.registration_confirmed[national_code].subject,
text: email.registration_confirmed[national_code].text
};
client.sendMail(email_to_send, ...)
请记住,所需的文件路径应相对于您的控制器位置,并且应添加其他国家代码检查以避免错误。