使用谷歌脚本发送没有服务器的电子邮件:无法打开文件错误
问题描述:
我想发送电子邮件到某个位置[email protected]使用谷歌脚本 。我正在关注 - https://github.com/dwyl/html-form-send-email-via-google-script-without-server 以在我的网站上设置整个功能。使用谷歌脚本发送没有服务器的电子邮件:无法打开文件错误
有一个在我的网站的HTML表单,我想发送电子邮件只要有人点击提交按钮在我的网站
这里的HTML表单 -
<div id="content">
\t \t <h1>Contact Us</h1>
\t \t <h4>Fill out the form below and a representative will
contact you shortly.</h4>
\t \t <form id="gform" method="POST" action="https://script.google.com/macros/u/1/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec">
<div class="form-group">
<label for="exampleInputEmail1">Your Name (required)</label>
<input type="text" class="form-control" id="exampleInputEmail1" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Your Email (required)</label>
<input type="Email" class="form-control" id="exampleInputPassword1" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Subject</label>
<input type="text" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group">
\t <label for="exampleInputPassword1">Message</label>
\t <textarea class="message"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
这里是script.gs文件 -
/******************************************************************************
* This tutorial is based on the work of Martin Hawksey twitter.com/mhawksey *
* But has been simplified and cleaned up to make it more beginner friendly *
* All credit still goes to Martin and any issues/complaints/questions to me. *
******************************************************************************/
var TO_ADDRESS = "[email protected]"; // 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);
}
}
当我点击填写表格后提交按钮,我得到这个 -
我得到同样的画面时,我在测试的Web应用程序点击你最新的代码。
我已经找到了 -
也有一些是我需要添加到里面的形式我的HTML标签为“name”属性,但它不是很清楚什么补充。
我在哪里设置此功能出错?
答
好的,我想我已经找出了你的问题。
似乎是两个问题:
- 你似乎登录两个Gmail帐户:这就是为什么/ U/1得到追加到脚本的地址(我们并不需要它,可能是一个错误)。因此,https://script.google.com/macros/u/1/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec变为https://script.google.com/macros/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec
- 您需要向脚本添加触发器。当你点击url时,该触发器将调用脚本的特定功能。 你需要做的是设置一个触发器,点击提交时触发doPost()。 doPost()发送您的电子邮件。
注:
- 如何添加触发器?转到编辑>当前项目的触发器(或者我们也可以使用时钟按钮)。在那里添加触发器。在运行下,选择doPost(),在电子表格事件中选择 - >然后在表单提交。保存。
- 当您从浏览器访问https://script.google.com/macros/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec(不含/ u/1 :-))时,会出现doGet()函数不存在,并且您的Web浏览器发送了GET请求时出错。
+0
现在它的工作,但我没有得到数据内的json对象的数据键。它完全是空的。 –
+1
您需要为您的输入和textarea元素提供名称属性。例如: imox
可能有很多问题。你有没有在你的gs脚本控制台中设置触发器?真的,我不认为你可以在表单中使用动作标签。您必须查找提交时单击按钮的时间,然后向网址发送ajax请求(https://script.google.com/macros/u/1/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec),这总是看起来不像上班。你部署正确吗?将需要更多信息 – imox
当我在部署完成后打开它作为“测试最新代码的Web应用程序”链接时,保持按钮的工作状态不起作用。 –
我已经完成了与第1到第7点的教程中的完全相同的内容。除了这些说明,我没有在我身边添加任何内容。 –