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

How do I email out ApostropheCMS contact form submissions?

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

提交的作品和提交的表格显示在 "Contact Forms" 管理菜单项下。

我还想为每次提交生成一封电子邮件到收件人电子邮件地址。 apostrophe-pieces-submit-widgets 说明我可以 override the beforeInsert method to send email, if desired.

但是,我不确定应该在哪里覆盖此方法。

我应该在 /lib/modules/contact-form/index.js self.submit 方法中添加一个 beforeInsert 方法吗?

或者,我是否应该创建 apostrophe-pieces-submit-widgets 的项目级副本,并覆盖那里的 beforeInsert 方法(我觉得这是可能 全局,因此不理想)?

最后,我是否应该涉及 apostrophe-email

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

beforeInsert 方法有意开始什么也不做,因为这是您在项目级代码中执行 "something extra" 的机会。

要覆盖该方法,请创建 lib/modules/products-submit-widgets/index.js(如果您尚未创建)。根据 apostrophe-pieces-submit-widgets.

的文档,您可能已经拥有 addFieldsextend 等文件

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

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。