2017-01-11 99 views
0

問候Gmail API粉絲。通過Gmail API從非主電子郵件地址發送

我已經寫了一些絕對可怕的代碼,它使用帶有JavaScript的Gmail API來整齊地驗證和發送電子郵件。

但是,即使有一個和我一樣大的大腦,我也有一個問題。雖然電子郵件使用gapi.client.gmail.users.messages.senduserId設置爲"me"完美髮送,但我無法從它與我的帳戶關聯的備用羣組電子郵件地址發送郵件。

例如,如果我嘗試將userId更改爲"[email protected]",即使該電子郵件地址與我的Gmail帳戶關聯,也會執行401「需要登錄」錯誤。

Settings screen

任何想法是怎麼回事?

下面代碼中的smidgeon:

// Stuff gleaned from a form 
var headers = { 
    'subject': e.data.subject, 
    'to': e.data.to, 
    'cc': e.data.cc, 
    'bcc': e.data.bcc, 
    'content-type': 'text/html; charset=utf-8' 
}; 

// grab the email content 
message = editor.getContent({format : 'raw'}); 

// construct the email 
var email = ''; 
for(var header in headers) 
    email += header += ': ' + headers[header] + '\r\n'; 

email += '\r\n' + message; 

// This is the bit - changing userId to anything other than 'me' (or the primary email address) kicks error 
var sendRequest = gapi.client.gmail.users.messages.send({ 
    'userId': 'me', 
     'resource': { 
      'raw': window.btoa(unescape(encodeURIComponent(email))).replace(/\+/g, '-').replace(/\//g, '_') 
      } 
     }); 

    return sendRequest.execute(function(){}); 
} 
+0

如果你使用uri編碼它會工作嗎? 'encodeURIComponent()「===」myalternate%40somewhere.com「 – Tholle

+1

好的想法,但不幸的是它沒有什麼區別。我可以將userId從'我'更改爲'[email protected]',它仍然有效 - 它從主電子郵件地址發送罰款。 – Booboobeaker

回答

0

URL路徑中不要更改 「用戶id」。只需在您的電子郵件中將「發件人」標題設置爲該地址(並且該地址必須是您的Gmail帳戶的驗證地址才能起作用)。

+0

非常感謝,工作!在將額外的帳戶與gmail關聯並且沒有任何事情發生之前,我曾嘗試過這種方式,然後忽略重試。 – Booboobeaker

相關問題