Class GmailDraft

GmailDraft

使用者在 Gmail 帳戶中建立的郵件草稿。

方法

方法傳回類型簡短說明
deleteDraft()void刪除這則草稿訊息。
getId()String取得這個草稿訊息的 ID。
getMessage()GmailMessage傳回代表此草稿的 GmailMessage。
getMessageId()String傳回代表此草稿的 GmailMessage ID。
send()GmailMessage傳送這封電子郵件草稿。
update(recipient, subject, body)GmailDraft取代這個草稿訊息的內容。
update(recipient, subject, body, options)GmailDraft使用選用引數取代這個草稿訊息的內容。

內容詳盡的說明文件

deleteDraft()

刪除這則草稿訊息。

var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
draft.deleteDraft();
draft.getMessage(); // Throws exception.

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

  • https://mail.google.com/

getId()

取得這個草稿訊息的 ID。

var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
var draftId = draft.getId();
var draftById = GmailApp.getDraft(draftId);
Logger.log(draft.getMessage().getSubject() == draftById.getMessage().getSubject());

回攻員

String:草稿 ID

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

  • https://mail.google.com/

getMessage()

傳回代表此草稿的 GmailMessage。

var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
var message = draft.getMessage();
Logger.log(message.getSubject());

回攻員

GmailMessage:代表這個草稿內容的訊息

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

  • https://mail.google.com/

getMessageId()

傳回代表此草稿的 GmailMessage ID。

var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
var messageId = draft.getMessageId();
Logger.log(messageId == draft.getMessage().getId());

回攻員

String:郵件 ID

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

  • https://mail.google.com/

send()

傳送這封電子郵件草稿。電子郵件大小 (包含標頭) 有配額限制

var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
var msg = draft.send(); // Send it
Logger.log(msg.getDate()); // Should be approximately the current timestamp

回攻員

GmailMessage:新傳送的訊息

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

  • https://mail.google.com/

update(recipient, subject, body)

取代這個草稿訊息的內容。電子郵件大小 (包含標頭) 有配額限制

// The code below will update a draft email with the current date and time.
var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
var now = new Date();
draft.update("mike@example.com", "current time", "The time is: " + now.toString());

參數

名稱類型說明
recipientString以半形逗號分隔的電子郵件地址清單
subjectString電子郵件的主旨 (最多 250 個字元)
bodyString電子郵件內文

回攻員

GmailDraft:新更新的草稿

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

  • https://mail.google.com/

另請參閱


update(recipient, subject, body, options)

使用選用引數取代這個草稿訊息的內容。這封電子郵件可以包含純文字或 HTML 內文。電子郵件大小 (包含標頭) 有配額限制

// Update a draft email with a file from Google Drive attached as a PDF.
var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
draft.update('mike@example.com', 'Attachment example', 'Please see attached file.', {
    attachments: [file.getAs(MimeType.PDF)],
    name: 'Automatic Emailer Script'
});

參數

名稱類型說明
recipientString以半形逗號分隔的電子郵件地址清單
subjectString電子郵件的主旨 (最多 250 個字元)
bodyString電子郵件內文
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
attachmentsBlobSource[]要隨電子郵件傳送的檔案陣列
bccString要接收密件副本的電子郵件地址清單 (以半形逗號分隔)
ccString以半形逗號分隔的電子郵件地址清單
fromString傳送電子郵件的來源地址,且必須是 GmailApp.getAliases() 傳回的其中一個值
htmlBodyString設定後,能夠轉譯 HTML 的裝置會使用這個引數來取代必要的內文引數。如果有電子郵件內嵌圖片,您可以在 HTML 內文中新增選用的 inlineImages 欄位
inlineImagesObjectJavaScript 物件,包含圖片索引鍵 (String) 與圖片資料 (BlobSource) 的對應。這裡假設使用的是 htmlBody 參數,且包含這些圖片的參照 (格式為 <img src="cid:imageKey" />)
nameString電子郵件寄件者的名稱 (預設值:使用者名稱)
replyToString做為預設回覆地址的電子郵件地址 (預設值:使用者的電子郵件地址)

回攻員

GmailDraft:新更新的草稿

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

  • https://mail.google.com/

另請參閱