gpt4 book ai didi

apostrophe-cms - 如何通过电子邮件发送 ApostropheCMS 联系表格提交?

转载 作者:行者123 更新时间:2023-12-04 06:41:32 24 4
gpt4 key购买 nike

我根据本文档创建了一个联系表单模块:https://apostrophecms.org/docs/tutorials/intermediate/forms.html

提交工作和提交的表格显示在“联系表格”管理菜单项下。

我还想为每次提交生成一封电子邮件到收件人电子邮件地址。 撇号件提交小部件说我可以override the beforeInsert method to send email, if desired.
但是,我不确定应该在哪里覆盖此方法。

我应该添加 beforeInsert内部方法/lib/modules/contact-form/index.js self.submit方法?

或者,我应该创建 的项目级副本吗?撇号件提交小部件 , 并覆盖 beforeInsert那里的方法(我觉得这可能太全局化了,因此不理想)?

最后,我是否应该涉及 撇号电子邮件 ?

最佳答案

我在这里的回答假设您实际上正在使用 apostrophe-pieces-submit-widgets .因此,到目前为止,您不必编写太多代码。你已经有一个 pieces 模块,我们称之为 products ,并且您已经制作了一个提交小部件模块,products-submit-widgets , 扩展 apostrophe-pieces-submit-widgets .好的,不错。让我们谈谈如何将其用于发送电子邮件的其余部分。
beforeInsert方法故意一开始什么都不做,因为这是您在项目级代码中做“额外的事情”的机会。

要覆盖该方法,请创建 lib/modules/products-submit-widgets/index.js如果您还没有这样做。您可能已经有了 addFields 的文件。和 extend等根据 apostrophe-pieces-submit-widgets 的文档.

现在您的代码可能如下所示:

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。

关于apostrophe-cms - 如何通过电子邮件发送 ApostropheCMS 联系表格提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53586549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com