【问题标题】:How do I email out ApostropheCMS contact form submissions?如何通过电子邮件发送 ApostropheCMS 联系表格提交?
【发布时间】:2018-12-12 01:29:26
【问题描述】:

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

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

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

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

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

或者,我应该创建一个 撇号-件-提交-小部件 的项目级副本,并覆盖那里的beforeInsert 方法(我觉得这可能也是 em> 全球性的,因此不理想)?

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

【问题讨论】:

    标签: apostrophe-cms


    【解决方案1】:

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

    beforeInsert 方法故意一开始什么都不做,因为这是您在项目级代码中做“一些额外的事情”的机会。

    要覆盖该方法,请创建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。

    【讨论】:

    • 实际上并没有使用撇号-提交-小部件...而是根据以下内容创建了一个模块:apostrophecms.org/docs/tutorials/intermediate/… 这会改变很多吗?我会修改模块 index.js 还是小部件 index.js?
    • 那篇文章首先推荐了pieces-submit-widgets。然后教你如何从头开始重新发明它,如果你真的想的话。
    • 我建议使用pieces-submit-widgets,否则您需要将其代码与您的代码(我们在这里看不到)进行比较,以了解如何调整我的答案。
    • 有道理。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2017-06-27
    • 2014-08-17
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多