Class MailApp

MailApp

傳送電子郵件。

這項服務可讓使用者傳送電子郵件,並完全掌控郵件內容。與 GmailApp 不同,MailApp 的唯一用途是傳送電子郵件。MailApp 無法存取使用者的 Gmail 收件匣。

與 MailApp 指令碼相比,使用 GmailApp 編寫的指令碼變更後,更可能觸發使用者的重新授權要求。

方法

方法傳回類型簡短說明
getRemainingDailyQuota()Integer傳回當天剩餘可傳送電子郵件的收件者人數。
sendEmail(message)void傳送電子郵件訊息。
sendEmail(recipient, subject, body)void傳送電子郵件訊息。
sendEmail(recipient, subject, body, options)void傳送電子郵件訊息,並可選擇是否加入引數。
sendEmail(to, replyTo, subject, body)void傳送電子郵件訊息。

內容詳盡的說明文件

getRemainingDailyQuota()

傳回當天剩餘可傳送電子郵件的收件者人數。傳回的值適用於目前的執行作業,且可能因執行作業而異。

配額是根據電子郵件收件者人數計算。如需特定配額資訊,請參閱「Google 服務配額」。

const emailQuotaRemaining = MailApp.getRemainingDailyQuota();
Logger.log(`Remaining email quota: ${emailQuotaRemaining}`);

回攻員

Integer:指令碼可傳送的剩餘電子郵件數。

授權

使用這個方法的指令碼需要下列一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.send_mail

sendEmail(message)

傳送電子郵件訊息。這個方法變化形式的彈性更高,可提供更多選項。

// This code fetches the Google and YouTube logos, inlines them in an email
// and sends the email
function inlineImage() {
  const googleLogoUrl =
      'https://www.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_74x24dp.png';
  const youtubeLogoUrl =
      'https://developers.google.com/youtube/images/YouTube_logo_standard_white.png';
  const googleLogoBlob =
      UrlFetchApp.fetch(googleLogoUrl).getBlob().setName('googleLogoBlob');
  const youtubeLogoBlob =
      UrlFetchApp.fetch(youtubeLogoUrl).getBlob().setName('youtubeLogoBlob');
  MailApp.sendEmail({
    to: 'recipient@example.com',
    subject: 'Logos',
    htmlBody: 'inline Google Logo<img src=\'cid:googleLogo\'> images! <br>' +
        'inline YouTube Logo <img src=\'cid:youtubeLogo\'>',
    inlineImages: {
      googleLogo: googleLogoBlob,
      youtubeLogo: youtubeLogoBlob,
    },
  });
}

參數

名稱類型說明
messageObject代表電子郵件訊息的 JavaScript 物件

進階參數

名稱類型說明
attachmentsBlobSource[]要隨電子郵件傳送的檔案陣列
bccString以逗號分隔的密件副本電子郵件地址清單
bodyString電子郵件內文
ccString以逗號分隔的副本收件者電子郵件地址清單
htmlBodyString如果已設定,能夠算繪 HTML 的裝置會使用這個引數,而非必要的主體引數;如果電子郵件有內嵌圖片,您可以在 HTML 主體中加入選用的 inlineImages 欄位
inlineImagesObjectJavaScript 物件,內含從圖片鍵 (String) 到圖片資料 (BlobSource) 的對應;這假設 htmlBody 參數已使用,且包含格式為 <img src="cid:imageKey" /> 的這些圖片的參照 (請參閱範例)
nameString電子郵件寄件者的名稱。預設為寄件者的使用者名稱。
noReplyBooleantrue 如果電子郵件應從一般不回覆電子郵件地址傳送,以避免收件者回覆電子郵件;這個選項僅適用於 Google Workspace 帳戶,不適用於 Gmail 使用者
replyToString做為預設回覆地址的電子郵件地址 (預設為使用者的電子郵件地址)。如果 noReply 設為 true,系統會忽略 replyTo
subjectString電子郵件主旨
toString收件者的電子郵件地址,或以半形逗號分隔的收件者電子郵件地址清單

授權

使用這個方法的指令碼需要下列一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.send_mail

另請參閱


sendEmail(recipient, subject, body)

傳送電子郵件訊息。

MailApp.sendEmail(
    'recipient@example.com',
    'TPS reports',
    'Where are the TPS reports?',
);

參數

名稱類型說明
recipientString以半形逗號分隔的收件者地址
subjectString主旨行
bodyString電子郵件內文

授權

使用這個方法的指令碼需要下列一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.send_mail

sendEmail(recipient, subject, body, options)

傳送電子郵件訊息,並可選擇是否加入引數。

// Send an email with two attachments: a file from Google Drive (as a PDF) and
// an HTML file.
const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
const blob = Utilities.newBlob(
    'Insert any HTML content here',
    'text/html',
    'my_document.html',
);
MailApp.sendEmail(
    'mike@example.com',
    'Attachment example',
    'Two files are attached.',
    {
      name: 'Automatic Emailer Script',
      attachments: [file.getAs(MimeType.PDF), blob],
    },
);

參數

名稱類型說明
recipientString以半形逗號分隔的收件者地址
subjectString主旨行
bodyString電子郵件內文
optionsObject指定進階參數的 JavaScript 物件,如下所列

進階參數

名稱類型說明
attachmentsBlobSource[]要隨電子郵件傳送的檔案陣列 (請參閱範例)
bccString以逗號分隔的密件副本電子郵件地址清單
ccString以逗號分隔的副本收件者電子郵件地址清單
htmlBodyString如果已設定,能夠算繪 HTML 的裝置會使用這個引數,而非必要的主體引數;如果電子郵件有內嵌圖片,您可以在 HTML 主體中加入選用的 inlineImages 欄位
inlineImagesObjectJavaScript 物件,包含從圖片鍵 (String) 到圖片資料 (BlobSource) 的對應;這假設使用 htmlBody 參數,且該參數包含格式為 <img src="cid:imageKey" /> 的圖片參照
nameString電子郵件寄件者的名稱 (預設為使用者名稱)
noReplyBooleantrue 如果電子郵件應從一般不回覆電子郵件地址傳送,以避免收件者回覆電子郵件;這個選項僅適用於 Google Workspace 帳戶,不適用於 Gmail 使用者
replyToString做為預設回覆地址的電子郵件地址 (預設值:使用者的電子郵件地址)

授權

使用這個方法的指令碼需要下列一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.send_mail

另請參閱


sendEmail(to, replyTo, subject, body)

傳送電子郵件訊息。使用者可以透過這個方法,輕鬆為傳送的郵件指定回覆地址,該地址可以與寄件者不同。

MailApp.sendEmail(
    'recipient@example.com',
    'replies@example.com',
    'TPS report status',
    'What is the status of those TPS reports?',
);

參數

名稱類型說明
toString以半形逗號分隔的收件者地址
replyToString回覆地址
subjectString主旨行
bodyString電子郵件內文 (純文字)

授權

使用這個方法的指令碼需要下列一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.send_mail