Draft Email Messages

Allow your users to save draft emails securely

The Email SDK supports the notion of draft email messages. A separate set of APIs are available to handle the lifecycle of drafts.

Creating a Draft Email Message

To create and save a draft email message, call the createDraftEmailMessage method. This method takes in a rfc822Data input property which contains the email message content formatted under RFC 822 as well as the senderEmailAddressId input property which must match the identifier of the email addess from the from field in the RFC 822 data. A call to this method returns metadata of the saved draft. Draft email message data provides the ID of the draft and an updatedAt timestamp representing the last time the draft was saved.

// Obtain the input RFC 822 data and sender email address id however makes sense for your implementation.
const rfc822Data: ArrayBuffer = // ...
const senderEmailAddressId = senderEmailAddress.id
try {
    const draftMetadata = await emailClient.createDraftEmailMessage({
        rfc822Data,
        senderEmailAddressId,
    })
    // `draftMetadata` is the metadata associated with the saved
    // draft email message including the id you can use this to access or
    // update the draft.
} catch (e) {
    // Handle/notify user of errors
}

Updating a Draft Email Message

Use the updateDraftEmailMessage method to update a previously created and saved draft email message. The input requires the id of the previously saved draft to perform the update. It is a requirement that the entire message content be replaced with an updated version as part of the rfc822Data input property. A call to this method returns the id of the draft that was updated which should match the id provided in the input.

// Obtain the input RFC 822 data, sender email address id and existing draft id however makes sense for your implementation.
const id: string = // ...
const rfc822Data: ArrayBuffer = // ...
const senderEmailAddressId = senderEmailAddress.id 
try {
    const draftMetadata = await emailClient.updateDraftEmailMessage({
        id,
        rfc822Data,
        senderEmailAddressId,
    })
    // `draftMetadata` is the metadata associated with the saved
    // draft email message including the id you can use this to access or
    // update the draft.
} catch (e) {
    // Handle/notify user of errors
}

Deleting Draft Email Messages

Draft email messages can be deleted in batches using the deleteDraftEmailMessages method by passing in one or more draft message identifiers and the id of the email address associated with the drafts.

Draft email messages that have been deleted will no longer be available and all traces of the message data and metadata will be deleted permanently.

Draft email messages can only be deleted in batches of 10. Supplying identifiers to the input which exceed this limit will return a LimitExceededError.

A BatchOperationResult type is returned from this method call which contains the status of the batch delete operation. Three possible statuses can be returned:

StatusDefinition

Success

All of the draft email messages succeeded to delete. The return object will include a list of identifiers of messages which succeeded to delete.

Partial

Only a subset of draft email messages succeeded to delete. The return object will include a list of identifiers and error descriptions of draft email messages which failed the delete and a list which succeeded the delete.

Failure

All of the draft email messages failed to delete.

// Obtain the input draft message ids and email address id however makes sense for your implementation.
const ids = arrayOf('draft-msg-id-1', 'draft-msg-id-2')
const emailAddressId = emailAddress.id
try {
    const result = await emailClient.deleteDraftEmailMessages({
        ids,
        emailAddressId,
    })
    // `result` contains the status of the batch delete operation and associated success and failure metadata.
} catch (e) {
    // Handle/notify user of errors
}

Retrieving Draft Email Messages

Draft email messages can be accessed using its identifier or by querying for a list of its identifiers.

If a draft email message has been deleted, it will no longer be available for access.

Single Draft Email Message by Id

To retrieve a single draft email message, use the getDraftEmailMessage method. This method takes in the id of the draft to retrieve as well as the id of the email address that is associated with the draft. This method returns the draft email message data if it exists.

// Obtain the input email address id and draft id however makes sense for
// your implementation.
const emailAddressId = emailAddress.id
try {
    const draftMessage = await emailClient.getDraftEmailMessage({
        id,
        emailAddressId,
    })
    // `draftMessage` containing draft message data and `updatedAt` timestamp
    // will be returned if a draft message corresponding to `id` is found,
    // else `undefined`.
} catch (e) {
    // Handle/notify user of errors
}

List of All Draft Email Messages

To retrieve a list of all draft email messages associated with a user, call the listDraftEmailMessages method.

try {
    const result = await emailClient.listDraftEmailMessages()
    // `result` contains a list of draft messages consisting of the 
    // draft message data and the `updatedAt` timestamp.
} catch {
    // Handle/notify user of errors
}

List of Draft Email Messages for an Email Address

To retrieve a list of multiple draft email messages associated with a certain email address, call the listDraftEmailMessagesForEmailAddressId method by passing in the id of the email address to query.

// Obtain the input email address id however makes sense for your implementation.
const emailAddressId = emailAddress.id
try {
    const result = await emailClient.listDraftEmailMessagesForEmailAddressId(
        emailAddressId
    )
    // `result` contains a list of draft messages consisting of the 
    // draft message data and the `updatedAt` timestamp.
} catch {
    // Handle/notify user of errors
}

List of All Draft Email Message Metadata

Use listDraftEmailMessageMetadata to retrieve a list of all the metadata of the draft email messages associated with a user. The metadata returned in the list can be used to retrieve individual draft email message content, perform an update to an already saved draft, or identify which drafts to perform full retrieval of based on updatedAt timestamp.

try {
    const result = await emailClient.listDraftEmailMessageMetadata()
    // `result` contains a list of all of the draft email messages'
    // metadata.
} catch (e) {
    // Handle/notify user of errors
}

List of Draft Email Message Metadata for an Email Address

To retrieve a list of all the metadata of the draft messages associated with an email address, call the listDraftEmailMessageMetadataForEmailAddressId method by passing in the id of the email address to query.

// Obtain the input email address id however makes sense for your implementation.
const emailAddressId = emailAddress.id
try {
    const result = await emailClient.listDraftEmailMessageMetadataForEmailAddressId(
        emailAddressId
    )
    // `result` contains a list of all of the draft email messages'
    // metadata.
} catch (e) {
    // Handle/notify user of errors
}

Last updated