我使用parse-server-amazon-ses-email-adapter,想了解如何本地化验证邮件并重置密码电子邮件。
我的线索是检查用户的字段,然后在server.js
或AmazonSESAdapter.js
上分配正确的模板。问题是用户属性是空的,除了电子邮件,用户名。
例如,在下面的示例中,firstName
未定义。
感谢。
emailAdapter: {
module: 'parse-server-amazon-ses-email-adapter',
options: {
// The address that your emails come from
fromAddress: 'Iron rr',
accessKeyId: 'gg',
secretAccessKey: 'gg',
region: 'eu-west-1',
// The template section
templates: {
passwordResetEmail: {
subject: 'Redefinir sua senha Iron Trainers',
pathPlainText: '/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/node_modules/parse-server-amazon-ses-email-adapter/test/email-templates/password_reset_email.txt',
pathHtml: '/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/node_modules/parse-server-amazon-ses-email-adapter/test/email-templates/password_reset_email.html',
callback: (user) => {
return {
firstName: user.get('firstName')
}
}
// Now you can use {{firstName}} in your templates
},
verificationEmail: {
subject: 'Confirmar email no Iron Trainers',
pathPlainText: '/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/node_modules/parse-server-amazon-ses-email-adapter/test/email-templates/verification_email.txt',
pathHtml: '/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/node_modules/parse-server-amazon-ses-email-adapter/test/email-templates/resendEmailVerification.html',
callback: (user) => {
return {
firstName: user.get('firstName')
}
}
// Now you can use {{firstName}} in your templates
},
customEmailAlert: {
subject: 'Urgent notification!',
pathPlainText: '/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/node_modules/parse-server-amazon-ses-email-adapter/test/email-templates/custom_alert.txt',
pathHtml: '/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/node_modules/parse-server-amazon-ses-email-adapter/test/email-templates/custom_alert.html',
}
}
答案 0 :(得分:2)
您需要在模板回调中进行本地化。
回调是同步的,因此您的所有本地化也需要同步。
emailTemplate.html
<div>
{{localizedText}}
</div>
每个区域设置的其他模板:
emailTemplate.en.html
<p>
Hi {{nome}}...
</p>
电子邮件逻辑:
// The same templater used by parse-server-amazon-ses-email-adapter
import template from 'lodash.template'
/* ... */
const TemplatesByLocale = {
en: fs.readFileSync('./emailTemplate.en.html'),
}
verificationEmail: {
/* ... */
pathHtml: './path/to/emailTemplate.html',
callback: (user) => {
const locale = getLocaleSomehow(user) // needs to be synchronous
const localizedTemplate = TemplatesByLocale[locale]
const compiled = template(localizedTemplate, {
// same interpolation as parse-server-amazon-ses-email-adapter
interpolate: /{{([\s\S]+?)}}/g
})
const localizedText = compiled({
nome: user.get('nome'), /* ... */
})
return {
localizedText: localizedText,
}
},
/* ... */
}
值得注意的是parse-server-amazon-ses-email-adapter
在使用纯文本模板之前将使用HTML模板(通过pathHtml
指定的模板),因此如果您指定了HTML模板,则可以放弃pathPlainText
属性。
答案 1 :(得分:2)
根据我的理解,这可以通过插件中的代码更改来完成。
代码
下面的行有一个if-else
条件
由于你没有提供任何反馈,我无法设置这个,我假设else部分是被执行的部分
const {
link,
appName,
user,
templateConfig
} = options;
const {
callback
} = templateConfig;
let userVars;
if (callback && typeof callback === 'function') {
userVars = callback(user);
// If custom user variables are not packaged in an object, ignore it
const validUserVars = userVars && userVars.constructor && userVars.constructor.name === 'Object';
userVars = validUserVars ? userVars : {};
}
pathPlainText = templateConfig.pathPlainText;
pathHtml = templateConfig.pathHtml;
templateVars = Object.assign({
link,
appName,
username: user.get('username'),
email: user.get('email')
}, userVars);
message = {
from: this.fromAddress,
to: user.get('email'),
subject: templateConfig.subject
};
}
现在在这部分中,2行决定了要使用的模板
pathPlainText = templateConfig.pathPlainText;
pathHtml = templateConfig.pathHtml;
此时,您提供的callback
已被调用。现在在回调中你可以设置一个变量,假设它是名字locale
。所以你可以像下面那样更新代码
pathPlainText = templateConfig.pathPlainText + (userVars["locale"] || "en");
pathHtml = templateConfig.pathHtml + (userVars["locale"] || "en");
然后,您将创建在文件路径中具有locale
的模板,并使用更新的代码选择正确的模板。
您还可以查看@bgran
答案,初看起来我相信它应该也能正常运作