简体   繁体   English

机车 CMS(托管):在提交内容类型(型号)表格后发送 email

[英]Locomotive CMS (hosted): send email upon submission of Content Type (Model) form

I am using the hosted version of Locomotive CMS.我正在使用机车 CMS 的托管版本。

Locomotive CMS provides a means by which to collect form submissions and a means by which to send an email using the Actions API . Locomotive CMS 提供了一种收集表单提交方法,以及一种使用 Actions API 发送 email 的方法

I want to collect form submissions and then send an email to the person who submitted the form.我想收集表单提交,然后将 email 发送给提交表单的人。

I have 3 caveats:我有 3 个警告:

  1. I wish to collect the form data in a content type (public submissions enabled)我希望以内容类型收集表单数据(启用公开提交)
  2. I wish to use Google reCAPTCHA v3我希望使用Google reCAPTCHA v3
  3. I wish to submit the form using AJAX我希望使用 AJAX 提交表格

All of the aforementioned items are available out of the box in the hosted version of Locomotive CMS.在 Locomotive CMS 的托管版本中,上述所有项目都是开箱即用的。 However, I can't figure out how to submit a content type form with Google reCAPTCHA via AJAX and send an email at the same time.但是,我不知道如何通过 AJAX 使用 Google reCAPTCHA 提交内容类型表单并同时发送 email。 The Send Email functionality requires an action on a dedicated page which I believe needs to be loaded in order for the action to run.发送 Email 功能需要在专用页面上执行操作,我认为需要加载该操作才能运行该操作。 Additionally I don't know how to get the data from the form submission to the page on which the Send Email action runs.此外,我不知道如何将表单提交中的数据获取到运行 Send Email 操作的页面。

How can I submit a Locomotive CMS content type form via AJAX with Google reCAPTCHA enabled and send an email containing the form submission data to the person who submitted in the form?如何在启用 Google reCAPTCHA 的情况下通过 AJAX 提交机车 CMS 内容类型表单,并将包含表单提交数据的 email 发送给在表单中提交的人?

A couple of prerequisites:几个先决条件:

  1. Set up metafields_schema.yml to include the necessary information for sending emails:设置 metafields_schema.yml 以包含发送电子邮件的必要信息:

     smtp_settings: label: Outgoing Email fields: server: label: SMTP Server type: string localized: false username: label: Username type: string localized: false password: label: Password type: string localized: false port: label: Port type: string hint: TLS localized: false
  2. Set up an email template:设置 email 模板:

    email-template.liquid :电子邮件模板.液体:

     <p>Dear {{ entryData.first_name }},</p> <.-- include form submission data via entryData.form_field_name -->

If the condition of using Google reCAPTCHA is removed, the task is relatively straight-forward.如果去掉使用 Google reCAPTCHA 的条件,任务就相对简单了。

We "simply" pass the form data to a custom page where we send an email and create a content entry using the Actions API.我们“简单地”将表单数据传递到自定义页面,在该页面发送 email 并使用操作 API 创建内容条目。

Setup as follows:设置如下:

  1. Manually create a form to collect the content type data.手动创建表单以收集内容类型数据。 Note the form action points to a custom page:请注意表单操作指向自定义页面:

     <form method="POST" enctype="multipart/form-data" class="form" action="{% path_to 'page-with-send-email-and-create-content-entry-action' %}"> <.-- form fields here --> </form> <script defer src="{{ 'form-submit.js' | javascript_url }}"></script>
  2. Submit the form with AJAX:使用 AJAX 提交表格:

    form-submit.js :表单提交.js

     $('.form').on('submit', function(e) { e.preventDefault(); var form = $(this); // error handling functions removed for brevity $.ajax({ type: 'POST', url: form.attr('action'), data: form.serialize(), dataType: 'json', success: function(data) { console.log('Success:', data); }, error: function(xhr, status, error) { console.log('Status:', status); console.log('Error:', error); var errors = jQuery.parseJSON(xhr.responseText).errors; console.log(errors); for (error in errors) { myError = error; console.log(myError); } } }); });
  3. Send an email and create a content type entry using the Actions API:发送 email并使用操作 API 创建内容类型条目

    page-with-send-email-and-create-content-entry-action.liquid :带有发送电子邮件和创建内容条目操作的页面

     {% action "send email and create content type entry" %} var SMTPsettings = getProp('site').metafields.smtp_settings; var entryData = getProp('params'); // params holds the data passed to the page via AJAX // save entryData into a Liquid variable to be called in the Email Template setProp('entryData',entryData); sendEmail({ to: formData.email_address, // must match the name attribute of the field used to collect the email address from: SMTPsettings.username, subject: '[Email Subject]', page_handle: 'email-template', // template page for email smtp: { address: SMTPsettings.server, port: SMTPsettings.port, user_name: SMTPsettings.username, password: SMTPsettings.password, authentication: 'plain', enable_starttls_auto: true } }); createEntry('content_entry_name', { content_type_field_name: formData.form_field_name, // comma-separated list - last item has no comma }); {% endaction %} {"success":true}

If Google reCAPTCHA is required, the task is more complex.如果需要 Google reCAPTCHA,则任务会更复杂。

I don't believe there is a way to manually create a form in Locomotive CMS with Google reCAPTCHA enabled, which means the above method won't work.我不相信有一种方法可以在启用 Google reCAPTCHA 的 Locomotive CMS 中手动创建表单,这意味着上述方法不起作用。

Google reCAPTCHA is available in Locomotive CMS via the default content type form setup: Google reCAPTCHA 可通过默认内容类型表单设置在 Locomotive CMS 中使用:

{% model_form 'content_type_slug', class: 'form', json: true, recaptcha: true %}
    // form fields here
{% endmodel_form %}

<script src="https://www.google.com/recaptcha/api.js?render={{ site.metafields.google.recaptcha_site_key }}"></script>

<script>
    grecaptcha.ready(function() {
        grecaptcha.execute('{{ site.metafields.google.recaptcha_site_key }}', {
            action: 'enquiry'
        })
        .then(function(token) {
            document.getElementById('g-recaptcha-response').value  = token;
        });
    });
</script>

<script defer src="{{ 'form-submit.js' | javascript_url }}"></script>

Note: The property recaptcha_required needs to be set to true in the content_type yml file.注意:需要在 content_type yml 文件中将属性recaptcha_required设置为true

In this case we cannot set a custom url for the form action.在这种情况下,我们无法为表单操作设置自定义 url。 Additionally the reCAPTCHA verification means we need to let the form submit and create the content entry via the usual process and send the email separately.此外,reCAPTCHA 验证意味着我们需要让表单提交并通过通常的流程创建内容条目,并单独发送 email。

To do this we will need to get the ID of the content entry created upon form submission.为此,我们需要获取表单提交时创建的内容条目的 ID。 We can then run an additional AJAX request and pass the ID to a custom page containing a send email action.然后,我们可以运行额外的 AJAX 请求并将 ID 传递到包含发送 email 操作的自定义页面。 On the custom page we will use the ID to reference the content entry and get the data to populate the email.在自定义页面上,我们将使用 ID 引用内容条目并获取数据以填充 email。

Setup as follows:设置如下:

  1. Create form via default method above.通过上面的默认方法创建表单。

  2. Submit the form with AJAX.使用 AJAX 提交表格。 Upon successful submission, get the content entry ID and pass it to a secondary AJAX request:成功提交后,获取内容条目 ID 并将其传递给辅助 AJAX 请求:

    form-submit.js:表单提交.js:

     $('.form').on('submit', function(e) { e.preventDefault(); var form = $(this); // error handling functions removed for brevity $.ajax({ type: 'POST', url: form.attr('action'), data: form.serialize(), dataType: 'json', success: function(data) { console.log('Success:', data); // get the content entry ID var entryID = data._id; // set up the data to be sent in the correct format var newData = 'id=' + entryID; // set up our secondary AJAX request function sendEmail() { $.ajax({ type: 'POST', url: 'page-with-send-email-action', data: newData, dataType: 'json', success: function(data) { console.log('Success:', data); }, error: function(xhr, status, error) { console.log('Status:', status); console.log('Error:', error); var errors = jQuery.parseJSON(xhr.responseText).errors; console.log(errors); for (error in errors) { myError = error; console.log(myError); } } }); } sendEmail(); showSuccess(); }, error: function(xhr, status, error) { console.log('Status:', status); console.log('Error:', error); var errors = jQuery.parseJSON(xhr.responseText).errors; console.log(errors); for (error in errors) { myError = error; console.log(myError); showError(); } } }); });
  3. Find the submitted content entry and send an email using the Actions API:找到提交的内容条目并使用 Actions API 发送 email:

    page-with-send-email-action.liquid:页面发送电子邮件action.liquid:

     {% action "send email" %} var SMTPsettings = getProp('site').metafields.smtp_settings; var entryID = getProp('params').id; var entryData = findEntry('content_type_slug', entryID); // save entryData into a Liquid variable to be called in the Email Template setProp('entryData',entryData); sendEmail({ to: entryData.email_address, from: SMTPsettings.username, subject: '[Email Subject]', page_handle: 'email-template', smtp: { address: SMTPsettings.server, port: SMTPsettings.port, user_name: SMTPsettings.username, password: SMTPsettings.password, authentication: 'plain', enable_starttls_auto: true } }); {% endaction %} {"success":true}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM