0

List Attachments

by
Published today

List a message's attachments (id, name, content type, size) without their content. Requires Mail.Read (covered by the default Mail.ReadWrite).

Script ms_outlook Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * List Attachments
5
 * List a message's attachments (id, name, content type, size) without their content. Requires Mail.Read (covered by the default Mail.ReadWrite).
6
 */
7
export async function main(
8
  auth: RT.MsOutlook,
9
  message_id: string,
10
  mailbox: string | undefined
11
) {
12
  const base = `https://graph.microsoft.com/v1.0/${
13
    mailbox != null && mailbox !== ""
14
      ? `users/${encodeURIComponent(mailbox)}`
15
      : "me"
16
  }`
17
  const url = new URL(
18
    `${base}/messages/${encodeURIComponent(message_id)}/attachments`
19
  )
20
  url.searchParams.append(
21
    "$select",
22
    "id,name,contentType,size,isInline,lastModifiedDateTime"
23
  )
24

25
  const response = await fetch(url, {
26
    headers: {
27
      Authorization: `Bearer ${auth.token}`,
28
      Accept: "application/json",
29
    },
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38