可存取 Gmail 討論串、郵件和標籤。
方法
內容詳盡的說明文件
create Draft(recipient, subject, body)
建立電子郵件草稿。電子郵件大小 (包括標頭) 超出配額限制。
// The code below creates a draft email with the current date and time. const now = new Date(); GmailApp.createDraft( 'mike@example.com', 'current time', `The time is: ${now.toString()}`, );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
recipient | String | 以半形逗號分隔的電子郵件地址清單 |
subject | String | 電子郵件主旨 |
body | String | 電子郵件內文 |
回攻員
Gmail:新建立的 Gmail 草稿
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
create Draft(recipient, subject, body, options)
建立草擬電子郵件訊息,並可選擇性地加入引數。電子郵件可以包含純文字或 HTML 內文。電子郵件大小 (包括標題,但不包括附件) 受配額限制。
// Create a draft email with a file from Google Drive attached as a PDF. const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz'); GmailApp.createDraft( 'mike@example.com', 'Attachment example', 'Please see attached file.', { attachments: [file.getAs(MimeType.PDF)], name: 'Automatic Emailer Script', }, );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
recipient | String | 收件者的地址 |
subject | String | 主旨行 |
body | String | 電子郵件內文 |
options | Object | 指定進階參數的 JavaScript 物件,如下所示 |
進階參數
| 名稱 | 類型 | 說明 |
|---|---|---|
attachments | Blob | 要隨電子郵件傳送的檔案陣列 |
bcc | String | 以半形逗號分隔的密件副本電子郵件地址清單 |
cc | String | 以半形逗號分隔的副本收件者電子郵件地址清單 |
from | String | 電子郵件的寄件者地址,必須是 get 傳回的值之一 |
html | String | 如果已設定,能夠算繪 HTML 的裝置會使用這個引數,而非必要的主體引數;如果電子郵件有內嵌圖片,您可以在 HTML 主體中加入選用的 inline 欄位 |
inline | Object | JavaScript 物件,內含圖片鍵 (String) 到圖片資料 (Blob) 的對應;這假設使用 html 參數,且該參數包含格式為 <img src="cid:imageKey" /> 的這些圖片的參照 |
name | String | 電子郵件寄件者的名稱 (預設為使用者名稱) |
reply | String | 做為預設回覆地址的電子郵件地址 (預設值:使用者的電子郵件地址) |
回攻員
Gmail:新建立的 Gmail 草稿
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
create Label(name)
建立指定名稱的新使用者標籤。
// Creates the label @FOO and logs label: FOO Logger.log(`label: ${GmailApp.createLabel('FOO')}`);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 新標籤的名稱 |
回攻員
Gmail:新建立的標籤
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
delete Label(label)
刪除指定標籤。
// Have to get the label by name first const label = GmailApp.getUserLabelByName('FOO'); GmailApp.deleteLabel(label);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
label | Gmail | 要刪除的標籤 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
get Aliases()
取得 Gmail 中設為這個帳戶別名的電子郵件清單。
您可以使用「from」選用引數,從任一別名傳送訊息。
// Log the aliases for this Gmail account and send an email as the first one. const me = Session.getActiveUser().getEmail(); const aliases = GmailApp.getAliases(); Logger.log(aliases); if (aliases.length > 0) { GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', { from: aliases[0], }); } else { GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.'); }
回攻員
String[] - 這個帳戶的別名陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
get Draft(draftId)
依 ID 擷取電子郵件草稿。
請搭配使用這個方法和 Gmail 草稿的 getId()。
// Get the first draft message in your drafts folder const draft = GmailApp.getDrafts()[0]; // Get its ID const draftId = draft.getId(); // Now fetch the same draft using that ID. const draftById = GmailApp.getDraft(draftId); // Should always log true as they should be the same message Logger.log( draft.getMessage().getSubject() === draftById.getMessage().getSubject(), );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
draft | String | 要擷取的草稿 ID |
回攻員
Gmail - 具有指定 ID 的草稿
擲回
Error:如果找不到指定 ID 的草稿
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Draft Messages()
擷取所有郵件草稿。
// Logs the number of draft messages const drafts = GmailApp.getDraftMessages(); Logger.log(drafts.length);
回攻員
Gmail:Gmail 郵件草稿陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Drafts()
取得所有 Gmail 郵件草稿。
const drafts = GmailApp.getDrafts(); for (let i = 0; i < drafts.length; i++) { Logger.log(drafts[i].getId()); }
回攻員
Gmail - Gmail 草稿郵件陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Inbox Threads()
擷取所有收件匣討論串,不論標籤為何。
如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。
// Log the subject lines of your Inbox const threads = GmailApp.getInboxThreads(); for (let i = 0; i < threads.length; i++) { Logger.log(threads[i].getFirstMessageSubject()); }
回攻員
Gmail - 收件匣中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Inbox Threads(start, max)
擷取一系列收件匣郵件串,不論標籤為何。
// Log the subject lines of up to the first 50 emails in your Inbox const threads = GmailApp.getInboxThreads(0, 50); for (let i = 0; i < threads.length; i++) { Logger.log(threads[i].getFirstMessageSubject()); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 要擷取的第一個執行緒的索引 |
max | Integer | 要擷取的執行緒數量上限 |
回攻員
Gmail - 收件匣中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Inbox Unread Count()
取得收件匣中未讀討論串的數量。
Logger.log(`Messages unread in inbox: ${GmailApp.getInboxUnreadCount()}`);
回攻員
Integer:收件匣中含有未讀郵件的討論串數量
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Message By Id(id)
依 ID 取得訊息。
請搭配使用這個方法和 Gmail 郵件的 getId()。
// Get the first message in the first thread of your inbox const message = GmailApp.getInboxThreads(0, 1)[0].getMessages()[0]; // Get its ID const messageId = message.getId(); // Now fetch the same message using that ID. const messageById = GmailApp.getMessageById(messageId); // Should always log true as they should be the same message Logger.log(message.getSubject() === messageById.getSubject());
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
id | String | 要擷取的訊息 ID |
回攻員
Gmail - 具有指定 ID 的訊息
擲回
Error:如果找不到指定 ID 的訊息
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
get Messages For Thread(thread)
擷取指定討論串中的所有訊息。
// Log all the subject lines in the first thread of your inbox const thread = GmailApp.getInboxThreads(0, 1)[0]; const messages = GmailApp.getMessagesForThread(thread); for (let i = 0; i < messages.length; i++) { Logger.log(`subject: ${messages[i].getSubject()}`); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要擷取的訊息串 |
回攻員
Gmail:這個討論串對應的訊息陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
get Messages For Threads(threads)
擷取指定討論串中的所有訊息。
// Log the subject lines of all messages in the first two threads of your inbox const thread = GmailApp.getInboxThreads(0, 2); const messages = GmailApp.getMessagesForThreads(thread); for (let i = 0; i < messages.length; i++) { for (let j = 0; j < messages[i].length; j++) { Logger.log(`subject: ${messages[i][j].getSubject()}`); } }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要擷取的訊息串 |
回攻員
Gmail:訊息陣列的陣列,其中外部陣列中的每個項目都對應至一個討論串,而內部陣列則包含該討論串中的訊息
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
get Priority Inbox Threads()
不論標籤為何,都會擷取所有優先收件匣郵件串。
如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。
Logger.log( `# of messages in your Priority Inbox: ${ GmailApp.getPriorityInboxThreads().length}`, );
回攻員
Gmail - 優先收件匣中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Priority Inbox Threads(start, max)
擷取優先收件匣中的一系列郵件串,不論郵件串是否已加上標籤。
// Will log some number 2 or less Logger.log( `# of messages in your Priority Inbox: ${ GmailApp.getPriorityInboxThreads(0, 2).length}`, );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 要擷取的第一個執行緒的索引 |
max | Integer | 要擷取的執行緒數量上限 |
回攻員
Gmail - 優先收件匣中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Priority Inbox Unread Count()
取得優先收件匣中的未讀討論串數量。
Logger.log( `Number of unread emails in your Priority Inbox : ${ GmailApp.getPriorityInboxUnreadCount()}`, );
回攻員
Integer:優先收件匣中含有未讀郵件的郵件串數量
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Spam Threads()
擷取所有垃圾內容討論串,不論標籤為何。
如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。
Logger.log(`# of total spam threads: ${GmailApp.getSpamThreads().length}`);
回攻員
Gmail:垃圾郵件資料夾中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Spam Threads(start, max)
不論標籤為何,都會擷取一系列垃圾郵件討論串。
// Will log a number at most 5 Logger.log(`# of total spam threads: ${GmailApp.getSpamThreads(0, 5).length}`);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 要擷取的第一個執行緒的索引 |
max | Integer | 要擷取的執行緒數量上限 |
回攻員
Gmail:垃圾郵件資料夾中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Spam Unread Count()
取得垃圾郵件中未讀討論串的數量。
// Unless you actually read stuff in your spam folder, this should be the same // as the number of messages in your spam folder. Logger.log(`# unread threads that are spam: ${GmailApp.getSpamUnreadCount()}`);
回攻員
Integer:含有未讀訊息的垃圾郵件討論串數量
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Starred Threads()
擷取所有已加星號的郵件串,不論標籤為何。
如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。
// Logs the number of starred threads Logger.log(`# Starred threads: ${GmailApp.getStarredThreads().length}`);
回攻員
Gmail:已加星號的 Gmail 郵件串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Starred Threads(start, max)
擷取一系列已加星號的郵件串,不論標籤為何。
// Logs the number of starred threads to a maximum of 5 Logger.log(`# Starred threads: ${GmailApp.getStarredThreads(0, 5).length}`);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 要擷取的第一個執行緒的索引 |
max | Integer | 要擷取的執行緒數量上限 |
回攻員
Gmail:已加星號的 Gmail 郵件串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Starred Unread Count()
取得已加星號的未讀討論串數量。
Logger.log(`# unread and starred: ${GmailApp.getStarredUnreadCount()}`);
回攻員
Integer:已加星號且有未讀訊息的討論串數量
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Thread By Id(id)
依 ID 取得執行緒。
請搭配使用這個方法和 Gmail 郵件串的 getId()。
// Gets the first inbox thread. const firstThread = GmailApp.getInboxThreads(0, 1)[0]; // Gets the same thread by ID. const threadById = GmailApp.getThreadById(firstThread.getId()); // Verifies that they are the same. console.log( firstThread.getFirstMessageSubject() === threadById.getFirstMessageSubject(), );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
id | String | 要擷取的討論串 ID。 |
回攻員
Gmail:具有指定 ID 的執行緒,或如果找不到,則為 null。
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
get Trash Threads()
擷取所有垃圾桶中的郵件串,不論標籤為何。
如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。
Logger.log(`# of total trash threads: ${GmailApp.getTrashThreads().length}`);
回攻員
Gmail - 垃圾桶中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get Trash Threads(start, max)
擷取一系列垃圾桶中的郵件串,不論標籤為何。
// Will log a number at most 5 Logger.log( `# of total trash threads: ${GmailApp.getTrashThreads(0, 5).length}`, );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 要擷取的第一個執行緒的索引 |
max | Integer | 要擷取的執行緒數量上限 |
回攻員
Gmail - 垃圾桶中的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get User Label By Name(name)
依標籤名稱擷取標籤。
const labelObject = GmailApp.getUserLabelByName('myLabel');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 要擷取的標籤名稱 |
回攻員
Gmail:具有指定名稱的 Gmail 標籤
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
get User Labels()
擷取使用者建立的標籤清單。
// Logs all of the names of your labels const labels = GmailApp.getUserLabels(); for (let i = 0; i < labels.length; i++) { Logger.log(`label: ${labels[i].getName()}`); }
回攻員
Gmail - 使用者建立的標籤陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
mark Message Read(message)
將這則訊息標示為已讀,並強制重新整理訊息。
// Mark the first message in the first thread of your inbox as read const message = GmailApp.getInboxThreads(0, 1)[0].getMessages()[0]; GmailApp.markMessageRead(message);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
message | Gmail | 要標示為已讀的郵件 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Message Unread(message)
將這則訊息標示為未讀,並強制重新整理訊息。
// Mark the first message in the first thread of your inbox as unread const message = GmailApp.getInboxThreads(0, 1)[0].getMessages()[0]; GmailApp.markMessageUnread(message);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
message | Gmail | 要標示為未讀取的郵件 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Messages Read(messages)
將這些訊息標示為已讀取,並強制重新整理訊息。
// Mark first three messages in the first inbox thread as read. // Assumes that the first inbox thread has 3 messages in it. const threadMessages = GmailApp.getInboxThreads(0, 1)[0].getMessages(); const messages = [threadMessages[0], threadMessages[1], threadMessages[2]]; GmailApp.markMessagesRead(messages);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
messages | Gmail | 要標示為已讀的訊息陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Messages Unread(messages)
將這些訊息標示為未讀,並強制重新整理訊息。
// Mark first three messages in the first inbox thread as unread. // Assumes that the first inbox thread has 3 messages in it const threadMessages = GmailApp.getInboxThreads(0, 1)[0].getMessages(); const messages = [threadMessages[0], threadMessages[1], threadMessages[2]]; GmailApp.markMessagesUnread(messages);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
messages | Gmail | 要標示為未讀取的訊息陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Thread Important(thread)
將這個討論串標示為重要,並強制重新整理討論串。
// Marks first inbox thread as important const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadImportant(thread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要標示為重要的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Thread Read(thread)
將這個討論串標示為已讀,並強制重新整理該討論串。
// Marks first inbox thread as read const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadRead(thread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要標示為已讀的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Thread Unimportant(thread)
將這個討論串標示為不重要,並強制重新整理該討論串。
// Marks first inbox thread as unimportant const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadUnimportant(thread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要標示為不重要的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Thread Unread(thread)
將這個討論串標示為未讀,並強制重新整理討論串。
// Marks first inbox thread as unread const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadUnread(thread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要標示為未讀的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Threads Important(threads)
將這些討論串標示為重要,並強制重新整理討論串。
// Marks first two threads in inbox as important const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsImportant(threads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要標示為重要的會話群組陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Threads Read(threads)
將這些討論串標示為已讀,並強制重新整理討論串。
// Marks first two threads in inbox as read const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsRead(threads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要標示為已讀的討論串陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Threads Unimportant(threads)
將這些討論串標示為不重要,並強制重新整理討論串。
// Marks first two threads in inbox as unimportant const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsUnimportant(threads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要標示為不重要的討論串陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
mark Threads Unread(threads)
將這些討論串標示為未讀,並強制重新整理討論串。
// Marks first two threads in inbox as unread const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsUnread(threads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要標示為未讀的討論串陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Message To Trash(message)
將郵件移至垃圾桶,並強制重新整理郵件。
// Move the first message in your inbox to trash const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const firstMessage = firstThread.getMessages()[0]; GmailApp.moveMessageToTrash(firstMessage);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
message | Gmail | 要刪除的郵件 |
回攻員
Gmail - Gmail 服務 (適用於鏈結)
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Messages To Trash(messages)
將指定郵件移至垃圾桶,並強制重新整理郵件。
// Move first two messages in your inbox to trash const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const messages = firstThread.getMessages(); const toDelete = [messages[0], messages[1]]; GmailApp.moveMessagesToTrash(toDelete);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
messages | Gmail | 要刪除的郵件 |
回攻員
Gmail - Gmail 服務 (適用於鏈結)
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Thread To Archive(thread)
將這個討論串移至封存,並強制重新整理討論串。
// Archive the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.moveThreadToArchive(firstThread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要封存的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Thread To Inbox(thread)
將這個討論串移至收件匣,並強制重新整理討論串。
// Find a thread not already in your inbox const thread = GmailApp.search('-in:inbox')[0]; // Get the first one GmailApp.moveThreadToInbox(thread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要移至收件匣的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Thread To Spam(thread)
將這個討論串移至垃圾訊息,並強制重新整理討論串。
// Tag first thread in inbox as spam const firstThread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.moveThreadToSpam(firstThread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要移至垃圾內容的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Thread To Trash(thread)
將這個執行緒移至垃圾桶,並強制重新整理執行緒。
// Move first thread in inbox to trash const firstThread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.moveThreadToTrash(firstThread);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要刪除的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Threads To Archive(threads)
將這些執行緒移至封存,並強制重新整理執行緒。
// Move first two threads in your inbox to the archive const firstTwoThreads = GmailApp.getInboxThreads(0, 2); GmailApp.moveThreadsToArchive(firstTwoThreads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要封存的討論串陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Threads To Inbox(threads)
將這些郵件串移至收件匣,並強制重新整理郵件串。
// Find two threads not already in your inbox const firstTwoThreads = GmailApp.search('-in:inbox', 0, 2); GmailApp.moveThreadsToInbox(firstTwoThreads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要移至收件匣的執行緒陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Threads To Spam(threads)
將這些郵件串移至垃圾郵件,並強制重新整理郵件串。
// Move first two threads in your inbox to spam const firstTwoThreads = GmailApp.getInboxThreads(0, 2); GmailApp.moveThreadsToSpam(firstTwoThreads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要移至垃圾內容的執行緒陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
move Threads To Trash(threads)
將這些執行緒移至垃圾桶,並強制重新整理執行緒。
// Move first two threads in your inbox to trash const firstTwoThreads = GmailApp.getInboxThreads(0, 2); GmailApp.moveThreadsToTrash(firstTwoThreads);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要刪除的執行緒陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
refresh Message(message)
從 Gmail 重新載入郵件和相關狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const firstMessage = firstThread.getMessages()[0]; // ...Do something that may take a while here.... GmailApp.refreshMessage(firstMessage); // ...Do more stuff with firstMessage...
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
message | Gmail | 要重新整理的訊息 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
refresh Messages(messages)
從 Gmail 重新載入郵件和相關狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const coupleOfMessages = firstThread.getMessages().slice(0, 2); // ...Do something that may take a while here.... GmailApp.refreshMessages(coupleOfMessages); // ...Do more stuff with coupleOfMessages...
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
messages | Gmail | 要重新整理的訊息 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
refresh Thread(thread)
從 Gmail 重新載入郵件串和相關聯的狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
const firstThread = GmailApp.getInboxThreads(0, 1)[0]; // ...Do something that may take a while here.... GmailApp.refreshThread(firstThread); // ... Do more stuff with the thread ...
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
thread | Gmail | 要重新整理的討論串 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
refresh Threads(threads)
從 Gmail 重新載入郵件串和相關聯的狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
const threads = GmailApp.getInboxThreads(0, 3); // ...Do something that may take a while here.... GmailApp.refreshThreads(threads); // ... Do more stuff with threads ...
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
threads | Gmail | 要重新整理的執行緒 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
search(query)
使用指定查詢搜尋 Gmail。
如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。
// Find starred messages with subject IMPORTANT const threads = GmailApp.search('is:starred subject:"IMPORTANT"');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
query | String | 搜尋查詢,就像在 Gmail 中輸入一樣 |
回攻員
Gmail:符合這項查詢的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
search(query, start, max)
使用指定查詢搜尋 Gmail。
// Find starred messages with subject IMPORTANT and return second batch of 10. // Assumes there are at least 11 of them, otherwise this will return an empty // array. const threads = GmailApp.search('is:starred subject:"IMPORTANT"', 10, 10);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
query | String | 搜尋查詢,就像在 Gmail 中輸入一樣 |
start | Integer | 起始執行緒的索引 |
max | Integer | 要傳回的執行緒數量上限 |
回攻員
Gmail:符合這項查詢的 Gmail 討論串陣列
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
send Email(recipient, subject, body)
傳送電子郵件訊息。電子郵件大小 (包括標頭) 超出配額限制。
// The code below will send an email with the current date and time. const now = new Date(); GmailApp.sendEmail( 'mike@example.com', 'current time', `The time is: ${now.toString()}`, );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
recipient | String | 以半形逗號分隔的電子郵件地址清單 |
subject | String | 電子郵件主旨 (最多 250 個半形字元) |
body | String | 電子郵件內文 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
send Email(recipient, subject, body, options)
傳送電子郵件,並可選擇是否加入引數。電子郵件可以包含純文字或 HTML 內文。電子郵件大小 (包括標題,但不包括附件) 受配額限制。
// Send an email with a file from Google Drive attached as a PDF. const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz'); GmailApp.sendEmail( 'mike@example.com', 'Attachment example', 'Please see the attached file.', { attachments: [file.getAs(MimeType.PDF)], name: 'Automatic Emailer Script', }, );
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
recipient | String | 收件者的地址 |
subject | String | 主旨行 (最多 250 個半形字元) |
body | String | 電子郵件內文 |
options | Object | 指定進階參數的 JavaScript 物件,如下所示 |
進階參數
| 名稱 | 類型 | 說明 |
|---|---|---|
attachments | Blob | 要隨電子郵件傳送的檔案陣列 |
bcc | String | 以半形逗號分隔的密件副本電子郵件地址清單 |
cc | String | 以半形逗號分隔的副本收件者電子郵件地址清單 |
from | String | 電子郵件的寄件者地址,必須是 get 傳回的值之一 |
html | String | 如果已設定,能夠算繪 HTML 的裝置會使用這個引數,而非必要的主體引數;如果電子郵件有內嵌圖片,您可以在 HTML 主體中加入選用的 inline 欄位 |
inline | Object | JavaScript 物件,內含圖片鍵 (String) 到圖片資料 (Blob) 的對應;這假設使用 html 參數,且該參數包含格式為 <img src="cid:imageKey" /> 的這些圖片的參照 |
name | String | 電子郵件寄件者的名稱 (預設為使用者名稱) |
no | Boolean | true 如果電子郵件應從一般不回覆電子郵件地址傳送,以避免收件者回覆電子郵件;這個選項僅適用於 Google Workspace 帳戶,不適用於 Gmail 使用者 |
reply | String | 做為預設回覆地址的電子郵件地址 (預設值:使用者的電子郵件地址) |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
set Current Message Access Token(accessToken)
設定目前訊息存取權杖,讓指令碼存取目前的 Gmail 屬性。
只有使用 Gmail 目前郵件範圍的 Google Workspace 外掛程式專案才需要這個方法。
function handleAddonActionEvent(e) { GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken); const mailMessage = GmailApp.getMessageById(e.messageMetadata.messageId); // Do something with mailMessage }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
access | String | 從 Gmail 外掛程式動作事件物件取得的臨時存取權杖。 |
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
star Message(message)
為這則訊息加上星號,並強制重新整理訊息。
// Stars the first message in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const message = firstThread.getMessages()[0]; GmailApp.starMessage(message);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
message | Gmail | 要加上星號的郵件 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
star Messages(messages)
為這些郵件加上星號,並強制重新整理郵件。
// Stars the first three messages in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const coupleOfMessages = firstThread.getMessages().slice(0, 3); GmailApp.starMessages(coupleOfMessages);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
messages | Gmail | 要加上星號的訊息陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
unstar Message(message)
移除這則訊息的星號,並強制重新整理訊息。
// Unstars the first message in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const message = firstThread.getMessages()[0]; GmailApp.unstarMessage(message);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
message | Gmail | 要移除星號的訊息 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/
另請參閱
unstar Messages(messages)
移除這些郵件的星號,並強制重新整理郵件。
// Unstars the first three messages in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const coupleOfMessages = firstThread.getMessages().slice(0, 3); GmailApp.unstarMessages(coupleOfMessages);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
messages | Gmail | 要取消加星號的訊息陣列 |
回攻員
Gmail:Gmail 服務,適用於串連
授權
使用這個方法的指令碼需要透過下列一或多個範圍或相關 REST API 中的適當範圍授權:
-
https://mail.google.com/