messages API

The messages API first appeared in Thunderbird 66.

Note

When the term messageId is used in these documents, it doesn’t refer to the Message-ID email header. It is an internal tracking number that does not remain after a restart. Nor does it follow an email that has been moved to a different folder.

Warning

Some functions in this API potentially return a lot of messages. Be careful what you wish for! See Working with message lists for more information.

Permissions

messagesDelete

Permanently delete your email messages

messagesImport

Import messages into Thunderbird

messagesMove

Copy or move your email messages (including moving them to the trash folder)

messagesRead

Read your email messages and mark or tag them

messagesTags

Create, modify and delete message tags

sensitiveDataUpload

Transfer sensitive user data (if access has been granted) to a remote server for further processing

Note

The permission messagesRead is required to use messenger.messages.*.

Functions

archive(messageIds)

Archives messages using the current settings. Archiving external messages will throw an ExtensionError.

Parameters

messageIds

(array of MessageId)

The IDs of the messages to archive.

Required permissions

  • messagesRead

  • messagesMove

copy(messageIds, destination)

Copies messages to a specified folder.

Parameters

messageIds

(array of MessageId)

The IDs of the messages to copy.

destination

The folder to copy the messages to.

Required permissions

  • messagesRead

  • accountsRead

  • messagesMove

delete(messageIds, [skipTrash])

Deletes messages permanently, or moves them to the trash folder (honoring the account’s deletion behavior settings). Deleting external messages will throw an ExtensionError. The skipTrash parameter allows immediate permanent deletion, bypassing the trash folder.

Note: Consider using move(messageIds, destination) to manually move messages to the account’s trash folder, instead of requesting the overly powerful permission to actually delete messages. The account’s trash folder can be extracted as follows:

/**
 * Returns the trash folder of the account a given message belongs to. The
 * accountsRead permission is required.
 */
async function getTrashFolderForMessage(msgId) {
    let msg = await messenger.messages.get(msgId);
    let account = await messenger.accounts.get(msg.folder.accountId);
    return account.folders.find(folder => folder.type == "trash");
}

Parameters

messageIds

(array of MessageId)

The IDs of the messages to delete.

[skipTrash]

(boolean, optional)

If true, the message will be deleted permanently, regardless of the account’s deletion behavior settings.

Required permissions

  • messagesRead

  • messagesDelete

get(messageId)

Returns a specified message.

Parameters

messageId

Return type (Promise)

Required permissions

  • messagesRead

getFull(messageId)

Returns a specified message, including all headers and MIME parts. Throws if the message could not be read, for example due to network issues.

Parameters

messageId

Return type (Promise)

Required permissions

  • messagesRead

getRaw(messageId, [options])

– [Added in TB 72, backported to TB 68.7]

Returns the unmodified source of a message. Throws if the message could not be read, for example due to network issues.

Parameters

messageId

[options]

(object, optional)

data_format

(string)

– [Added in TB 117, backported to TB 115.3.2]

The message can either be returned as a DOM File or as a binary string. The historic default is to return a binary string (kept for backward compatibility). However, it is now recommended to use the File format, because the DOM File object can be used as-is with the downloads API and has useful methods to access the content, like File.text() and File.arrayBuffer(). Working with binary strings is error prone and needs special handling:

/**
 * Decodes a binary string using the given encoding format and returns a
 * JavaScript string. Produces mangled output if used with anything but a binary
 * input string.
 */
function decodeBinaryString(binaryString, inputEncoding = "utf-8") {
    const buffer = new Uint8Array(binaryString.length);
    for (let i = 0; i < binaryString.length; i++) {
        buffer[i] = binaryString.charCodeAt(i) & 0xFF;
    }
    let decoder = new TextDecoder(inputEncoding);
    return decoder.decode(buffer);
}

(see MDN for supported input encodings).

Supported values:

File

BinaryString

Return type (Promise)

string or File

Required permissions

  • messagesRead

import(file, destination, [properties])

– [Added in TB 106]

Imports a message into a local Thunderbird folder. To import a message into an IMAP folder, add it to a local folder first and then move it to the IMAP folder.

Parameters

file

(File)

destination

The folder to import the messages into.

[properties]

(MessageProperties, optional)

Return type (Promise)

Required permissions

  • messagesRead

  • accountsRead

  • messagesImport

list(folder)

Gets all messages in a folder.

Parameters

folder

Return type (Promise)

Required permissions

  • messagesRead

  • accountsRead

move(messageIds, destination)

Moves messages to a specified folder. If the messages cannot be removed from the source folder, they will be copied instead of moved. Moving external messages will throw an ExtensionError.

Parameters

messageIds

(array of MessageId)

The IDs of the messages to move.

destination

The folder to move the messages to.

Required permissions

  • messagesRead

  • accountsRead

  • messagesMove

query([queryInfo])

– [Added in TB 69, backported to TB 68.2]

Gets all messages that have the specified properties, or all messages if no properties are specified.

Parameters

[queryInfo]

(object, optional)

[attachment]

(boolean, optional)

– [Added in TB 96, backported to TB 91.4.1]

If specified, returns only messages with or without attachments.

[author]

(string, optional)

Returns only messages with this value matching the author. The search value is a single email address, a name or a combination (e.g.: Name <user@domain.org>). The address part of the search value (if provided) must match the author’s address completely. The name part of the search value (if provided) must match the author’s name partially. All matches are done case-insensitive.

[body]

(string, optional)

Returns only messages with this value in the body of the mail.

[flagged]

(boolean, optional)

Returns only flagged (or unflagged if false) messages.

[folder]

(MailFolder, optional)

Returns only messages from the specified folder. The accountsRead permission is required.

[fromDate]

(Date, optional)

Returns only messages with a date after this value.

[fromMe]

(boolean, optional)

Returns only messages with the author’s address matching any configured identity.

[fullText]

(string, optional)

Returns only messages with this value somewhere in the mail (subject, body or author).

[headerMessageId]

(string, optional)

– [Added in TB 85]

Returns only messages with a Message-ID header matching this value.

[includeSubFolders]

(boolean, optional)

– [Added in TB 91]

Search the folder specified by queryInfo.folder recursively.

[recipients]

(string, optional)

Returns only messages whose recipients match all specified addresses. The search value is a semicolon separated list of email addresses, names or combinations (e.g.: Name <user@domain.org>). For a match, all specified addresses must equal a recipient’s address completely and all specified names must match a recipient’s name partially. All matches are done case-insensitive.

[subject]

(string, optional)

Returns only messages with this value matching the subject.

[tags]

(TagsDetail, optional)

– [Added in TB 74]

Returns only messages with the specified tags. For a list of available tags, call the listTags() method.

[toDate]

(Date, optional)

Returns only messages with a date before this value.

[toMe]

(boolean, optional)

Returns only messages with at least one recipient address matching any configured identity.

[unread]

(boolean, optional)

Returns only unread (or read if false) messages.

Return type (Promise)

Required permissions

  • messagesRead

update(messageId, newProperties)

Marks or unmarks a message as junk, read, flagged, or tagged. Updating external messages will throw an ExtensionError.

Parameters

messageId

newProperties

Required permissions

  • messagesRead

continueList(messageListId)

Returns the next chunk of messages in a list. See Working with message lists for more information.

Parameters

messageListId

(string)

Return type (Promise)

Required permissions

  • messagesRead

getAttachmentFile(messageId, partName)

– [Added in TB 88]

Gets the content of a MessageAttachment as a File object.

Parameters

messageId

partName

(string)

Return type (Promise)

Required permissions

  • messagesRead

The most simple way to get the content of an attachment is to use the text() method of the returned File object:

let attachments = await browser.messages.listAttachments(messageId);
for (let att of attachments) {
    let file = await browser.messages.getAttachmentFile(
        messageId,
        att.partName
    );
    let content = await file.text();
}

listAttachments(messageId)

– [Added in TB 88]

Lists the attachments of a message.

Parameters

messageId

Return type (Promise)

Required permissions

  • messagesRead

openAttachment(messageId, partName, tabId)

– [Added in TB 114]

Opens the specified attachment

Parameters

messageId

partName

(string)

tabId

(integer)

The ID of the tab associated with the message opening.

Required permissions

  • messagesRead

createTag(key, tag, color)

– [Added in TB 102]

Creates a new message tag. Tagging a message will store the tag’s key in the user’s message. Throws if the specified tag key is used already.

Parameters

key

(string)

Unique tag identifier (will be converted to lower case). Must not include ()<>{/%*” or spaces.

tag

(string)

Human-readable tag name.

color

(string)

Tag color in hex format (i.e.: #000080 for navy blue)

Required permissions

  • messagesRead

  • messagesTags

deleteTag(key)

– [Added in TB 102]

Deletes a message tag, removing it from the list of known tags. Its key will not be removed from tagged messages, but they will appear untagged. Recreating a deleted tag, will make all former tagged messages appear tagged again.

Parameters

key

(string)

Unique tag identifier (will be converted to lower case). Must not include ()<>{/%*” or spaces.

Required permissions

  • messagesRead

  • messagesTags

listTags()

Returns a list of tags that can be set on messages, and their human-friendly name, colour, and sort order.

Return type (Promise)

array of MessageTag

Required permissions

  • messagesRead

updateTag(key, updateProperties)

– [Added in TB 102]

Updates a message tag.

Parameters

key

(string)

Unique tag identifier (will be converted to lower case). Must not include ()<>{/%*” or spaces.

updateProperties

(object)

[color]

(string, optional)

Tag color in hex format (i.e.: #000080 for navy blue).

[tag]

(string, optional)

Human-readable tag name.

Required permissions

  • messagesRead

  • messagesTags

Events

onCopied

– [Added in TB 91]

Fired when messages have been copied.

Parameters for onCopied.addListener(listener)

listener(originalMessages, copiedMessages)

A function that will be called when this event occurs.

Parameters passed to the listener function

originalMessages

copiedMessages

Required permissions

  • messagesRead

  • accountsRead

onDeleted

– [Added in TB 91]

Fired when messages have been permanently deleted.

Parameters for onDeleted.addListener(listener)

listener(messages)

A function that will be called when this event occurs.

Parameters passed to the listener function

messages

Required permissions

  • messagesRead

  • accountsRead

onMoved

– [Added in TB 91]

Fired when messages have been moved.

Parameters for onMoved.addListener(listener)

listener(originalMessages, movedMessages)

A function that will be called when this event occurs.

Parameters passed to the listener function

originalMessages

movedMessages

Required permissions

  • messagesRead

  • accountsRead

onNewMailReceived

– [Added in TB 75]

Fired when a new message is received, and has been through junk classification and message filters.

Parameters for onNewMailReceived.addListener(listener)

listener(folder, messages)

A function that will be called when this event occurs.

Parameters passed to the listener function

folder

messages

Required permissions

  • messagesRead

  • accountsRead

onUpdated

– [Added in TB 91]

Fired when one or more properties of a message have been updated.

Parameters for onUpdated.addListener(listener)

listener(message, changedProperties)

A function that will be called when this event occurs.

Parameters passed to the listener function

message

changedProperties

Required permissions

  • messagesRead

Types

MessageAttachment

Represents an attachment in a message.

object

contentType

(string)

The content type of the attachment.

name

(string)

The name, as displayed to the user, of this attachment. This is usually but not always the filename of the attached file.

partName

(string)

Identifies the MIME part of the message associated with this attachment.

size

(integer)

The size in bytes of this attachment.

[message]

(MessageHeader, optional)

A MessageHeader, if this attachment is a message.

MessageHeader

Basic information about a message.

object

author

(string)

bccList

(array of string)

The Bcc recipients. Not populated for news/nntp messages.

ccList

(array of string)

The Cc recipients. Not populated for news/nntp messages.

date

(Date)

external

(boolean)

– [Added in TB 106]

Whether this message is a real message or an external message (opened from a file or from an attachment).

flagged

(boolean)

Whether this message is flagged (a.k.a. starred).

headerMessageId

(string)

– [Added in TB 85]

The message-id header of the message.

headersOnly

(boolean)

– [Added in TB 102]

Some account types (for example pop3) allow to download only the headers of the message, but not its body. The body of such messages will not be available.

id

junk

(boolean)

– [Added in TB 74]

Whether the message has been marked as junk. Always false for news/nntp messages and external messages.

junkScore

(integer)

– [Added in TB 74]

The junk score associated with the message. Always 0 for news/nntp messages and external messages.

new

(boolean)

– [Added in TB 106]

Whether the message has been received recently and is marked as new.

recipients

(array of string)

The To recipients. Not populated for news/nntp messages.

size

(integer)

– [Added in TB 90]

The total size of the message in bytes.

subject

(string)

The subject of the message.

tags

(array of string)

Tags associated with this message. For a list of available tags, call the listTags method.

[folder]

(MailFolder, optional)

The accountsRead permission is required for this property to be included. Not available for external or attached messages.

[read]

(boolean, optional)

Whether the message has been marked as read. Not available for external or attached messages.

MessageId

A unique id representing a MessageHeader and the associated message. This id doesn’t refer to the Message-ID email header. It is an internal tracking number that does not remain after a restart. Nor does it follow an email that has been moved to a different folder.

integer

MessageList

See Working with message lists for more information.

object

messages

(array of MessageHeader)

[id]

(string, optional)

MessagePart

Represents an email message “part”, which could be the whole message

object

[body]

(string, optional)

The content of the part

[contentType]

(string, optional)

[headers]

(object, optional)

A dictionary object of part headers as key-value pairs, with the header name as key, and an array of headers as value

[name]

(string, optional)

Name of the part, if it is a file

[partName]

(string, optional)

The identifier of this part, used in getAttachmentFile(messageId, partName)

[parts]

(array of MessagePart, optional)

Any sub-parts of this part

[size]

(integer, optional)

The size of this part. The size of message/* parts is not the actual message size (on disc), but the total size of its decoded body parts, excluding headers.

MessageProperties

Message properties used in update(messageId, newProperties) and import(file, destination, [properties]). They can also be monitored by onUpdated.

object

[flagged]

(boolean, optional)

Whether the message is flagged (a.k.a starred).

[junk]

(boolean, optional)

Whether the message is marked as junk. Only supported in update(messageId, newProperties)

[new]

(boolean, optional)

– [Added in TB 106]

Whether the message is marked as new. Only supported in import(file, destination, [properties])

[read]

(boolean, optional)

Whether the message is marked as read.

[tags]

(array of string, optional)

Tags associated with this message. For a list of available tags, call the listTags method.

MessageTag

object

color

(string)

Tag color.

key

(string)

Unique tag identifier.

ordinal

(string)

Custom sort string (usually empty).

tag

(string)

Human-readable tag name.

TagsDetail

Used for filtering messages by tag in various methods. Note that functions using this type may have a partial implementation.

object

mode

(string)

Whether all of the tag filters must apply, or any of them.

Supported values:

all

any

tags

(object)

A dictionary object with one or more filter condition as key-value pairs, the key being the tag to filter on, and the value being a boolean expression, requesting whether a message must include (true) or exclude (false) the tag. For a list of available tags, call the listTags() method.