我想使用谷歌脚本向位置target@example.com发送电子邮件。我正在关注此事 - https://github.com/dwyl/html-form-send-email-via-google-script-without-server以在我的网站中设置整个功能。
我的网站上有一个html表单,我想在有人点击我网站上的提交按钮时发送电子邮件
这是表单的html -
<form id="gform" method="POST" action="https://script.google.com/macros/s/AKfycbyacpl2YxzZIVDPuizRqrzedC-7mAtMywSvoVbfLbebA6MdkUKs/exec">
<div class="form-group">
<label for="exampleInputEmail1">Your Name (required)</label>
<input type="text" class="form-control" name="name" id="exampleInputEmail1" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Your Email (required)</label>
<input type="Email" class="form-control" name="email" id="exampleInputPassword1" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Your Phone Number (required)</label>
<input type="text" class="form-control" name="phone-num" id="exampleInputPassword1" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Subject</label>
<input type="text" class="form-control" name="subject" id="exampleInputPassword1">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Message</label>
<textarea class="message" name="message"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
&#13;
这是script.g文件正常工作 -
var TO_ADDRESS = "target@example.com"; // where to send form data
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class Logger
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}
我想设置传递的json对象的样式,所以当我使用以下脚本时 -
// if you want to store your email server-side (hidden), uncomment the next line
var TO_ADDRESS = "target@example.com";
// spit out all the keys/values from the form in HTML for email
function formatMailBody(obj, order) {
var result = "";
// loop over all keys in the ordered form data
for (var idx in order) {
var key = order[idx];
result += "<h4 style='text-transform: capitalize; margin-bottom: 0'>" + key + "</h4><div>" + obj[key] + "</div>";
// for every key, concatenate an `<h4 />`/`<div />` pairing of the key name and its value,
// and append it to the `result` string created at the start.
}
return result; // once the looping is done, `result` will be one long string to put in the email body
}
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class Logger
record_data(e);
// shorter name for form data
var mailData = e.parameters;
// names and order of form elements
var dataOrder = JSON.parse(e.parameters.formDataNameOrder);
// determine recepient of the email
// if you have your email uncommented above, it uses that `TO_ADDRESS`
// otherwise, it defaults to the email provided by the form's data attribute
var sendEmailTo = (typeof TO_ADDRESS !== "undefined") ? TO_ADDRESS : mailData.formGoogleSendEmail;
MailApp.sendEmail({
to: String(sendEmailTo),
subject: "Contact form submitted",
// replyTo: String(mailData.email), // This is optional and reliant on your form actually collecting a field named `email`
htmlBody: formatMailBody(mailData, dataOrder)
});
return ContentService // return json success results
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}
我收到此错误 -
{"result":"error","error":{"parameter":{"subject":"subj",
"name":"shan","message":"msg","email":"sh@sh","phone-num":"46546"},
"contextPath":"","contentLength":64,"queryString":"","parameters":
{"subject":["subj"],"name":["shan"],"message":["msg"],"email":["sh@sh"],
"phone-num":["46546"]},"postData":{"type":"application/x-www-form-urlencoded",
"length":64,"contents":"name=shan&email=sh%40sh&phone-num=46546&subject=subj&message=msg",
"name":"postData"}}}
只有第二个脚本才会发生这种情况。我哪里错了?