0

Get Message

by
Published 20 days ago

Get one message by id, with its body as HTML or plain text. Requires Mail.Read (covered by the default Mail.ReadWrite) (a shared mailbox needs Mail.Read.Shared, not a default).

Script ms_outlook Verified

The script

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

3
/**
4
 * Get Message
5
 * Get one message by id, with its body as HTML or plain text. Requires Mail.Read (covered by the default Mail.ReadWrite) (a shared mailbox needs Mail.Read.Shared, not a default).
6
 */
7
export async function main(
8
  auth: RT.MsOutlook,
9
  message_id: string,
10
  select: string | undefined,
11
  body_format: "html" | "text" = "html",
12
  mailbox: string | undefined
13
) {
14
  const base = `https://graph.microsoft.com/v1.0/${
15
    mailbox != null && mailbox !== ""
16
      ? `users/${encodeURIComponent(mailbox)}`
17
      : "me"
18
  }`
19
  const url = new URL(`${base}/messages/${encodeURIComponent(message_id)}`)
20
  if (select != null && select !== "") {
21
    url.searchParams.append("$select", select)
22
  }
23

24
  const response = await fetch(url, {
25
    headers: {
26
      Authorization: `Bearer ${auth.token}`,
27
      Accept: "application/json",
28
      Prefer: `outlook.body-content-type="${body_format}"`,
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