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

geo*_*nto 7 apostrophe-cms

我根据此文档创建了一个联系表单模块: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那里的方法(我觉得这可能太全局,因此不理想)?

最后,我应该涉及撇号 - 电子邮件吗?

Tom*_*ell 4

我在这里的回答假设您实际上正在使用apostrophe-pieces-submit-widgets. 所以到目前为止您还不必编写太多代码。您已经有了一个pieces模块,我们称之为products,并且您已经创建了一个提交小部件模块,products-submit-widgets它扩展了apostrophe-pieces-submit-widgets。太好了。让我们谈谈如何通过剩下的方式发送电子邮件。

该beforeInsert方法有意开始时不执行任何操作,因为这是您在项目级代码中执行“额外操作”的机会。

要覆盖该方法,请创建lib/modules/products-submit-widgets/index.js(如果您尚未这样做)。根据 的文档,您可能已经拥有该文件以及addFields等等。extendapostrophe-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);
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,在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 }}
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好!但是,为了使该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'
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,有关更完整的信息,请参阅从您的撇号项目发送电子邮件。如果您使用简单的配置,您的电子邮件可能会被作为垃圾邮件丢弃。如果您不打算使用 Amazon Simple Email Service 或 Postmark 等服务,并且您的流量较低,则可以将 Nodemailer 配置为通过有效的 Gmail 帐户发送消息,而无需进行太多工作。

希望这有帮助!将其添加为 apostrope-pieces-submit-widgets 模块的可选内置功能将是一个很棒的 PR。