我在这里的回答假设您实际上正在使用apostrophe-pieces-submit-widgets。因此,到目前为止,您不必编写太多代码。您已经有一个部件模块,我们称之为products,并且您已经创建了一个提交小部件模块products-submit-widgets,它扩展了apostrophe-pieces-submit-widgets。好,很好。让我们来谈谈如何将其用于发送电子邮件的其余部分。
beforeInsert 方法故意一开始什么都不做,因为这是您在项目级代码中做“一些额外的事情”的机会。
要覆盖该方法,请创建lib/modules/products-submit-widgets/index.js(如果您尚未这样做)。根据apostrophe-pieces-submit-widgets 的文档,您可能已经拥有带有addFields 和extend 等的文件。
现在您的代码可能如下所示:
module.exports = {
// ALL OF YOUR EXISTING CONFIGURATION OF THE products-submit-widgets
// MODULE STILL GOES HERE, then...
construct: function(self, options) {
self.beforeInsert = function(req, piece, callback) {
return self.email(req, 'emailSubmission', {
piece: piece
}, {
// can also specify from and other
// valid properties for nodemailer messages here
to: 'admin@example.com',
subject: 'A new submission was received'
}, callback);
};
}
}
现在,在 项目级别 的 lib/modules/my-pieces-module-name-submit-widgets/views 中,创建一个 emailSubmission.html 文件并像这样填充它:
<h4>A new submission was received</h4>
A new submission was received from {{ piece.someFieldOrOther }}.
It contains this information:
{{ piece.someOtherField }}
{{ piece.yetAnotherField }}
{{ piece.etc }}
到目前为止一切顺利!但是,为了使self.email 方法起作用,您需要配置apostrophe-email 模块。如果您的 Linux 服务器上已经有电子邮件传递代理,那么基本配置可能如下所示:
// in app.js
modules: {
'apostrophe-email': {
// Default "from" address. Note your email can be dropped as spam
// if this address is invalid or your server has no right to send
// email for it
from: '"Jane Doe" <jane@doe.com>',
// See the nodemailer documentation, many
// different transports are available, this one
// matches how PHP does it on Linux servers
nodemailer: {
sendmail: true,
newline: 'unix',
path: '/usr/sbin/sendmail'
}
}
}
但是,有关这方面的更完整信息,请参阅sending email from your Apostrophe project。如果您使用幼稚的配置,您的电子邮件可能会作为垃圾邮件被丢弃。如果您不打算使用 Amazon Simple Email Service 或 Postmark 之类的服务,并且您的流量很低,您可以将 nodemailer 配置为通过有效的 gmail 帐户发送邮件而无需太多工作。
希望这有帮助!将其添加为 apostrophe-pieces-submit-widgets 模块的可选内置功能将是一个很棒的 PR。