0

Move Email to Folder

by
Published today

Move a message to another folder: a folder id, or a well-known name such as `inbox`, `archive`, `deleteditems`, `drafts`, `junkemail` or `sentitems`. The moved message gets a new id, returned in the response. Requires Mail.ReadWrite (a connection default).

Script outlook Verified

The script

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

3
export type DynSelect_destination_folder = string
4

5
export async function destination_folder(
6
  auth: RT.Outlook,
7
  mailbox: string | undefined
8
) {
9
  const base = `https://graph.microsoft.com/v1.0/${
10
    mailbox != null && mailbox !== ""
11
      ? `users/${encodeURIComponent(mailbox)}`
12
      : "me"
13
  }`
14
  const r = await fetch(`${base}/mailFolders?$top=100&$select=id,displayName`, {
15
    headers: {
16
      Authorization: `Bearer ${auth.token}`,
17
      Accept: "application/json",
18
    },
19
  })
20
  if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
21
  const { value } = (await r.json()) as {
22
    value: { id: string; displayName: string }[]
23
  }
24
  return value.map((f) => ({ value: f.id, label: f.displayName }))
25
}
26

27
/**
28
 * Move Email to Folder
29
 * Move a message to another folder: a folder id, or a well-known name such as `inbox`, `archive`, `deleteditems`, `drafts`, `junkemail` or `sentitems`. The moved message gets a new id, returned in the response. Requires Mail.ReadWrite (a connection default).
30
 */
31
export async function main(
32
  auth: RT.Outlook,
33
  message_id: string,
34
  destination_folder: DynSelect_destination_folder,
35
  mailbox: string | undefined
36
) {
37
  const base = `https://graph.microsoft.com/v1.0/${
38
    mailbox != null && mailbox !== ""
39
      ? `users/${encodeURIComponent(mailbox)}`
40
      : "me"
41
  }`
42
  const response = await fetch(
43
    `${base}/messages/${encodeURIComponent(message_id)}/move`,
44
    {
45
      method: "POST",
46
      headers: {
47
        Authorization: `Bearer ${auth.token}`,
48
        "Content-Type": "application/json",
49
        Accept: "application/json",
50
      },
51
      body: JSON.stringify({ destinationId: destination_folder }),
52
    }
53
  )
54

55
  if (!response.ok) {
56
    throw new Error(`${response.status} ${await response.text()}`)
57
  }
58

59
  return await response.json()
60
}
61