Manage Email Folders

Allow your users the flexibility to manage their email address with email folders

Email Folders

When an email address is provisioned through the Email SDK, a set of standard folders are also created and associated with the provisioned email address. The following table describes the standard folders:

Folder
Function

INBOX

Inbound messages are assigned to the INBOX folder.

SENT

Outbound messages which have completed sending are assigned to the SENT folder.

OUTBOX

Reserved for future use.

TRASH

Messages can be moved to the TRASH folder. Messages in this folder will automatically be deleted after a set period of time depending on the application's configuration.

Retrieving Email Folders

Email folders associated with an email address are available in the folders attribute of that email address and are retrieved along with the email address.

Email folders associated with an email address can also be retrieved directly through the listEmailFoldersForEmailAddressId method by passing in the id of the email address to query. This returns a ListOutput object with the list of email folder objects which contain information used to identify each folder and to facilitate moving of messages between folders.

The EmailFolder object contains useful information such as the name of the folder, the total size of all messages assigned to the folder and the total count of unseen messages assigned to the folder.

// Obtain the input email address id however makes sense for your implementation.
const emailAddressId = emailAddress.id
try {
    const listOutput = await emailClient.listEmailFoldersForEmailAddressId({
        emailAddressId,
        cachePolicy: CachePolicy.RemoteOnly,
        nextToken,
    })
    // `listOutput` contains the list of items matching the input. 
    // Page through the results if listOutput.nextToken != nil.
} catch (e) {
    // Handle/notify user of errors
}

Move Email Messages between Folders

Email messages can also be moved between folders. For example an email message stored in the "inbox" folder can be moved to the "trash" folder. This is done through the update email messages API as described here. The email message object contains a previousFolderId property which specifies the identifier of the folder that the message was previously in. An example implementation of using the updateEmailMessages method to move a message to another folder is:

// Obtain the input message ids and email address id however makes sense for your implementation.
const emailAddressId = emailAddress.id
const ids = ['message-id-1', 'message-id-2']
try {
   // List the email folders and find the "trash" folder.
   const trashFolder = await emailClient.listEmailFoldersForEmailAddressId({
     emailAddressId,
     cachePolicy: CachePolicy.RemoteOnly
   }).then((result) => result.items.find(folder) => folder.folderName === 'TRASH')
 
   // Update an email message using the "trash" folder id.
   await emailClient.updateEmailMessages({
        ids,
        values: { folderId: trashFolder.id },
    })
   // Each of the input messages should now be assigned to the "trash" folder.
} catch (e) {
    // Handle/notify user of errors
}

Creating Custom Email Folders

Users can create custom folders for whatever purpose suits them. These behave in much the same way as the standard folders. To create a custom email folder, call the createCustomEmailFolder method with the name of the custom folder and the id of the email address the folder should be associated with. Duplicate folder names will not be allowed. Once a folder has been created, messages can be moved in and out of them the same way as described above.

// Obtain the email address id however makes sense for your implementation.
const emailAddressId = emailAddress.id
const customFolderName = "Custom"
try {
   // Create a custom email folder
   const customFolder = await emailClient.createCustomEmailFolder({
        emailAddressId,
        customFolderName,
    })
   // The customEmailFolder will contain the newly created email folder and all of its metadata
} catch (e) {
    // Handle/notify user of errors
}

Deleting Custom Email Folders

Users can also delete any custom email folders they have created. The standard folders associated with the email address cannot be deleted. To delete a custom email folder, call the deleteCustomEmailFolder method with the id of the custom email folder and the id of its associated email address. The method will return a copy of the deleted folder, or nothing if the folder could not be found. When a custom email folder is deleted, any messages in that folder will be moved to the TRASH folder.

// Obtain the folder id and email address id however makes sense for your implementation.
const customFolderId = customEmailFolder.id
const emailAddressId = emailAddress.id
try {
   // Delete a custom email folder
   const customFolder = await emailClient.deleteCustomEmailFolder({
        emailFolderId: customFolderId,
        emailAddressId,
   })
   // The customEmailFolder will contain the deleted email folder or undefined
} catch (e) {
    // Handle/notify user of errors
}

Updating Custom Email Folders

Custom email folders can be updated via the updateCustomEmailFolder method. At this point, only the name of a custom folder can be updated.

// Obtain the folder id and email address id however makes sense for your implementation
const emailAddressId = emailAddress.id
const customFolderId = customFolder.id
try {
    const inputValues: CustomEmailFolderUpdateValuesInput = {
          customFolderName: "new-folder-name",
    }
    
    const updatedFolder = await instanceUnderTest.updateCustomEmailFolder({
        emailAddressId: emailAddressId,
        emailFolderId: customFolderId,
        values: inputValues,
    })
    // updatedFolder will contain the folder with its updated values
} catch (e) {
    // Handle/notify user of errors
}

Last updated