0

对于在 Netlify 上运行的站点,我正在尝试使用 Mailgun 向提交表单的站点访问者发送自动回复。

我正在尝试按照此示例进行操作:

https://gist.github.com/flatlinediver/0bdca4c2ae09d34d351d45230d3b2489

但是,当我尝试在本地运行它并提交表单(使用 netlify dev)时,我一直遇到这个错误:

Request from ::1: POST /.netlify/functions/submission-created
Response with status 500 in 1 ms.
SyntaxError: Unexpected token a in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.exports.handler (/Users/sam/Projects/wycd/functions/submission-created.js:15:23)
    at /usr/local/lib/node_modules/netlify-cli/src/utils/serve-functions.js:117:29
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/netlify-cli/node_modules/express/lib/router/layer.js:95:5)
    at next (/usr/local/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:137:13)
    at next (/usr/local/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:131:14)
    at next (/usr/local/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:131:14)
    at next (/usr/local/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:131:14)
    at next (/usr/local/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:131:14)
    at next (/usr/local/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:131:14)

这是我的函数提交创建的文件

require('dotenv').config()
const apiKey = process.env.MAILGUN_API_KEY
const domain = process.env.DOMAIN
// const receiver_mail = process.env.RECEIVER_MAIL
const mailgun = require('mailgun-js')({ apiKey, domain })

exports.handler = function(event, context, callback) {

    if(event.httpMethod!='POST' || !event.body){
        return callback(new Error('An error occurred!'))
    }

    const data = JSON.parse(event.body);

    if(data.antibot.length>0){
        return callback(new Error('Forbidden access'))
    }

    let messageData = {
        from: data.email,
        to: data.email,
        subject: `Message received from ${data.name}`,
        text: `${data.message}`
    }

    mailgun.messages().send(messageData, function (error) {
        if (error) {
            return callback(error);
        } else {
            return callback(null, {
                statusCode: 200,
                body: 'success'
            });
        }
    })

}

这是我在index.html上的 HTML 表单代码


<form class="contact" id="contactForm" action="/.netlify/functions/submission-created" name="contactForm" method="POST" data-netlify="true">

<input type="text" class="large" id="name" name="name" placeholder="Your Name *" required>

<input type="text" class="large" id="email" name="email" placeholder="example@email.com *" required>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="tel" class="large" id="phone" name="phone">
<script src="build/js/utils.js"></script>
<script>
var input = document.querySelector("#phone"),
 output = document.querySelector("#output");
 var iti = window.intlTelInput(input, {
 nationalMode: true,
utilsScript: "../../build/js/utils.js?1562189064761" // just for formatting/placeholders etc
});

var handleChange = function() {
var text = (iti.isValidNumber()) ? "International: " + iti.getNumber() : "Please enter a number below";
var textNode = document.createTextNode(text);
output.innerHTML = "";
output.appendChild(textNode);
 };

 // listen to "keyup", but also "change" to update when the user selects a country
input.addEventListener('change', handleChange);
input.addEventListener('keyup', handleChange);
</script>

<input type="text" id="role" name="role" style="display: none;">

<button type="submit" style="background: black; cursor: pointer;">Submit</button>

<p class="text-sm"><em>When you click Submit, we'll send your details to a top climate organization who will guide you on further action.</em></p>
                                                  </form>

任何人都可以分享建议以使此功能按预期工作吗?

4

0 回答 0