2017-10-29 165 views
1

我想發送電子郵件到某個位置[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); 
 
    } 
 
}

當我點擊填寫表格後提交按鈕,我得到這個 -

enter image description here

我得到同樣的畫面時,我在測試的Web應用程序點擊你最新的代碼。

我已經找到了 -

也有一些是我需要添加到裏面的形式我的HTML標籤爲「name」屬性,但它不是很清楚什麼補充。

我在哪裏設置此功能出錯?

+0

可能有很多問題。你有沒有在你的gs腳本控制檯中設置觸發器?真的,我不認爲你可以在表單中使用動作標籤。您必須查找提交時單擊按鈕的時間,然後向網址發送ajax請求(https://script.google.com/macros/u/1/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec),這總是看起來不像上班。你部署正確嗎?將需要更多信息 – imox

+0

當我在部署完成後打開它作爲「測試最新代碼的Web應用程序」鏈接時,保持按鈕的工作狀態不起作用。 –

+0

我已經完成了與第1到第7點的教程中的完全相同的內容。除了這些說明,我沒有在我身邊添加任何內容。 –

回答

4

好的,我想我已經找出了你的問題。

似乎是兩個問題:

注:

  1. 如何添加觸發器?轉到編輯>當前項目的觸發器(或者我們也可以使用時鐘按鈕)。在那裏添加觸發器。在運行下,選擇doPost(),在電子表格事件中選擇 - >然後在表單提交。保存。
  2. 當您從瀏覽器訪問https://script.google.com/macros/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec(不含/ u/1 :-))時,會出現doGet()函數不存在,並且您的Web瀏覽器發送了GET請求時出錯。
+0

現在它的工作,但我沒有得到數據內的json對象的數據鍵。它完全是空的。 –

+1

您需要爲您的輸入和textarea元素提供名稱屬性。例如: imox

相關問題