用 google script 寄 email 測試

閱讀時間約 2 分鐘
在開發 email 的過程,一定會有要寄信的需要,這時候 google script 可以快速的幫忙這段,透過簡單的 code 就可以完成

建立新的專案


先打開自己的 google script

登入後選擇自己的 google script,可透過下面的 url 一直接進入到畫面中,在 click 「start scripting」

google script 的首面


建立專案

簡單的建立一個新的專案,基本畫面和 google drive 是差不多的

raw-image

新增部署作業

新建立一個部署作業,並選擇網頁應用程式,完成後在 click 「執行」

raw-image

這個時候可能會有權限問題,此時可以參考這個 youtube ,在 11:12 秒的時候會說怎麼排除,權限問題

youtube 說明權限問題

coding

在上述中都完成後,就可以開始 coding 了,執行一個 sendEmail 的 function

function doGet() {
sendEmail();
return ContentService.createTextOutput("完成");
}

function 中設定好,mail, option 等等的東西

function sendEmail() {
const respondentEmail = '[email protected]'
const subject = 'Subject';const message = 'Message';
const templ = HtmlService.createTemplateFromFile('email');
// filename is email.html

const htmlMessage = templ.evaluate().getContent();
const options = { htmlBody: htmlMessage };

GmailApp.sendEmail(respondentEmail, subject, message, options);
}
在你的檔案底下,放一 email.html 的檔案,在根目錄,這樣子在寄信的時候就會去找此檔案寄出。

然後在種新的執行一次,就 ok 了喔~~

👍 👍 👍 

raw-image


留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容
你可能也想看