2015-11-25 29 views
-1

我們使用普通的學生信息系統來管理我們的學校。它很容易在客戶端定製,並且在發生某些操作時(表單提交,頁面加載等),我們有幾個地方想要觸發電子郵件。不可能使用PHP,ASP或Java等服務器端編程語言。帶JS AJAX調用的GMail API

我們想要做的就是使用AJAX調用來發送電子郵件。我完全使用JavaScript工作,但我必須使用我想發送的GMail帳戶表單登錄。我們需要對其進行身份驗證(或者只要持票人令牌有效),然後在用戶操作發生後發送電子郵件。

由於客戶端語言似乎能夠擊中這些端點併發送電子郵件而無需在瀏覽器會話中「登錄」發送帳戶,這對我來說似乎是可能的。是嗎?

我也意識到創建一個使用GMail作爲SMTP中繼的服務器端腳本是一種選擇,但許多使用Google Apps for Education的學校根本沒有可以託管此類腳本的網站服務器。客戶端插件實際上就是我們想要構建的。

回答

0

你可以試試這個,只需更換CLIENT_ID值:

範圍這是只讀,但我可以改變根據您需要執行的操作。

<html> 
    <head> 
    <script type="text/javascript"> 
     // Your Client ID can be retrieved from your project in the Google 
     // Developer Console, https://console.developers.google.com 
     var CLIENT_ID = '<YOUR_CLIENT_ID>'; 

    var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']; 

    /** 
    * Check if current user has authorized this application. 
    */ 
    function checkAuth() { 
    gapi.auth.authorize(
     { 
     'client_id': CLIENT_ID, 
     'scope': SCOPES.join(' '), 
     'immediate': true 
     }, handleAuthResult); 
    } 

    /** 
    * Handle response from authorization server. 
    * 
    * @param {Object} authResult Authorization result. 
    */ 
    function handleAuthResult(authResult) { 
    var authorizeDiv = document.getElementById('authorize-div'); 
    if (authResult && !authResult.error) { 
     // Hide auth UI, then load client library. 
     authorizeDiv.style.display = 'none'; 
     loadGmailApi(); 
    } else { 
     // Show auth UI, allowing the user to initiate authorization by 
     // clicking authorize button. 
     authorizeDiv.style.display = 'inline'; 
    } 
    } 

    /** 
    * Initiate auth flow in response to user clicking authorize button. 
    * 
    * @param {Event} event Button click event. 
    */ 
    function handleAuthClick(event) { 
    gapi.auth.authorize(
     {client_id: CLIENT_ID, scope: SCOPES, immediate: false}, 
     handleAuthResult); 
    return false; 
    } 

    /** 
    * Load Gmail API client library. List labels once client library 
    * is loaded. 
    */ 
    function loadGmailApi() { 
    gapi.client.load('gmail', 'v1', listLabels); 
    } 

    /** 
    * Print all Labels in the authorized user's inbox. If no labels 
    * are found an appropriate message is printed. 
    */ 
    function listLabels() { 
    var request = gapi.client.gmail.users.labels.list({ 
     'userId': 'me' 
    }); 

    request.execute(function(resp) { 
     var labels = resp.labels; 
     appendPre('Labels:'); 

     if (labels && labels.length > 0) { 
     for (i = 0; i < labels.length; i++) { 
      var label = labels[i]; 
      appendPre(label.name) 
     } 
     } else { 
     appendPre('No Labels found.'); 
     } 
    }); 
    } 

    /** 
    * Append a pre element to the body containing the given message 
    * as its text node. 
    * 
    * @param {string} message Text to be placed in pre element. 
    */ 
    function appendPre(message) { 
    var pre = document.getElementById('output'); 
    var textContent = document.createTextNode(message + '\n'); 
    pre.appendChild(textContent); 
    } 

</script> 


    <script src="https://apis.google.com/js/client.js?onload=checkAuth"> 
    </script> 
    </head> 
    <body> 
    <div id="authorize-div" style="display: none"> 
     <span>Authorize access to Gmail API</span> 
     <!--Button for the user to click to initiate auth sequence --> 
     <button id="authorize-button" onclick="handleAuthClick(event)"> 
     Authorize 
     </button> 
    </div> 
    <pre id="output"></pre> 
    </body> 
</html> 

來源:Gmail API - Quickstart

+0

我下樓與示例中的路徑,但問題是,它會提示你的谷歌帳戶。這很好,但我們確實需要在沒有人登錄的情況下悄悄地發送電子郵件。我使用的其他API允許您使用ID和密碼獲取不記名令牌。有沒有客戶端的方法來做類似的事情,我們可以使用JavaScript進行調整?我不是新手開發人員,但我發現Google文檔很難瀏覽。 –