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 的设备将使用此参数,而不是必需的 body 实参;如果您为电子邮件添加了内嵌图片,则可以在 HTML 正文中添加可选的 inlineImages 字段
inlineImagesObject一个 JavaScript 对象,包含从图片键 (String) 到图片数据 (BlobSource) 的映射;这假定使用了 htmlBody 参数,并且其中包含对这些图片的引用,格式为 <img src="cid:imageKey" />
nameString电子邮件发送者的姓名(默认值:用户的姓名)
replyToString用作默认“回复”地址的电子邮件地址 (默认值:用户的电子邮件地址)

返回

GmailDraft - 新更新的草稿

授权

使用此方法的脚本需要使用以下一个或多个 范围相关 REST API 中的相应范围进行授权:

  • https://mail.google.com/

另请参阅