我使用nodemailer来提交电子邮件,并从我的本地主机运行。我在下面的dir /api/email/services/Email.js中手动创建了电子邮件服务
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'example.com',
port: 587,
secure: false,
auth: {
user: 'user',
pass: 'password',
},
});
module.exports = {
send: (from, to, subject, html) => {
const options = {
from,
to,
subject,
html
};
return transporter.sendMail(options);
},
};所以我可以像strapi.services.email.send(from, email, subject, html);一样使用它
通常,我会在代码行const html = '<p>Email testing</p>'中编写html模板,以便在电子邮件服务中传递。但我不想这样做的每一个电子邮件提交来自不同的控制器。
因此,我在/config/email-templates/custom-email.html中创建了一个html模板,并试图像const html = path.join(__dirname + '/../../../config/email-templates/custom-email.html');一样调用它。
当我运行它,电子邮件可以发送成功,但它不能呈现的html。它不是呈现的html,而是将custom-email.html的完整路径显示为电子邮件消息。这种方法可以在strapi中实现吗?
发布于 2021-03-04 19:14:14
您需要传递实际的内容,而不是将路径传递到文件。在第一种情况下,const html = '<p>Email testing</p>'实际上是传递内容,而在第二种情况下,则是传递文件路径。
修改后的send方法如下所示:
send: (from, to, subject, htmlpath) => {
const readHTMLFile = (path, callback)=> {
fs.readFile(path, {encoding: "utf-8"}, function (err, html) {
if (err)
return callback(err);
else
return callback(null, html);
});
}
readHTMLFile(htmlpath, function(err, html) {
const options = {
from,
to,
subject,
html
};
return transporter.sendMail(options);
}); }https://stackoverflow.com/questions/66473443
复制相似问题