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()

刪除這封草稿郵件。

const 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。

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

回攻員

String:草稿 ID

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getMessage()

傳回代表這封草稿的 GmailMessage。

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

回攻員

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

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getMessageId()

傳回代表這項草稿的 GmailMessage ID。

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

回攻員

String:郵件 ID

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

send()

傳送這封草稿電子郵件訊息。電子郵件大小 (包括標頭) 超出配額限制

const draft =
    GmailApp.getDrafts()[0];  // The first draft message in the drafts folder
const 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.
const draft =
    GmailApp.getDrafts()[0];  // The first draft message in the drafts folder
const 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.
const draft =
    GmailApp.getDrafts()[0];  // The first draft message in the drafts folder
const 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/

另請參閱