跳到主要内容

Mail from code: QubApp.mail

QubApp.mail lets code work with the mail of its owner as that person: read the mailboxes they read — their own connected ones and the company mailboxes given to them — mark, move and delete letters, and send letters. Reading and acting follow the shape of a common JavaScript library for reading mail, sending the shape of a common library for sending it; every method and field is listed below.

The shortest example​

JavaScript
function main() {
const box = QubApp.mail.connect('[email protected]');
box.mailboxOpen('INBOX');
const uids = box.search({ seen: false, from: '[email protected]', since: '2026-11-01' });
box.fetch(uids).forEach(function (m) {
console.log(m.envelope.subject, m.text.slice(0, 200));
});
box.messageFlagsAdd(uids, ['\\Seen']);
box.sendMail({ to: '[email protected]', subject: 'New letters from the partner', text: uids.length + ' letters' });
}

Mailboxes​

MethodWhat it does
QubApp.mail.mailboxes()the mailboxes the owner reads: id, address, company; an own mailbox carries its state, a company one its level — read (reading only) or send
QubApp.mail.connect(mailbox)a client of one of them, by its address or id; another mailbox is refused

The client​

MethodWhat it does
list()the folders: path, name, specialUse (\Inbox, \Sent, \Drafts, \Junk, \Trash, \Archive, \All), status: { messages, unseen }
mailboxOpen(path)opens a folder for the methods below and answers { path, specialUse, exists, unseen }; until it is called, the folder is INBOX
search(query)the uids of the open folder that match, the newest first
fetchOne(uid), fetch(uids)letters: uid, flags, envelope (date, subject, messageId, from, to, cc, replyTo), and, in addition to that shape, text, html, attachments (part, filename, contentType, size) and truncated
download(uid, part)one attachment: { meta: { filename, contentType, size }, content }, where content is its bytes in base64
messageFlagsAdd(uids, flags), messageFlagsRemove(uids, flags)puts or takes off \Seen and \Flagged
messageMove(uids, path)moves letters to another folder
messageDelete(uids)deletes letters, as the mail window does
sendMail(letter)sends a letter from this mailbox and answers { messageId, sentCopy }

Reading a letter leaves it unread, as fetch does in such libraries; to mark it read, put \Seen. The raw source of a letter is not given: a letter comes with its text, its HTML and its attachments.

search takes seen, flagged, from, subject, since and before; from and subject match a part of the text, ignoring case, and dates are a Date or YYYY-MM-DD. It reads the whole open folder page by page from the newest letter: since and before only pick letters and do not stop the reading, because the order of a folder is the order the letters arrived in, not their dates.

Marks, moves and deletion are done at the mail provider first, as in the mail window. A company mailbox given for reading only refuses them.

Sending​

sendMail takes a letter in the shape common to such libraries: to, cc, bcc — an address, addresses separated by commas, or a list of addresses or { address }; a display name ("Ann" <[email protected]>, { name, address }) is refused, because the letter carries addresses only; subject, text, html; inReplyTo and references to answer a letter; attachments — { filename, content, contentType }, where content is a text or, with encoding: 'base64', bytes in base64. An attachment by path or href is refused. The letter with its attachments is at most 16 MiB of text in one call; content in base64 counts as its text. The letter goes from the mailbox the client was connected to, under its owner's name. from is one address: the mailbox's own, or — for a company mailbox — one of its switched-on aliases or an address the domain's catch-all delivers to it, as the mail window offers in From; any other address is refused by the sending itself. A copy lands in Sent; when it could not, sentCopy is false and sentCopyProblem says why — the letter has gone all the same.

Whose rights a call has​

The calls go as the owner of the code, with the same rights and refusals as the mail window. Sending, marks, moves and deletion are refused when the Qubix plan does not include Mail.

Where the code runsWhose rights the calls have
Scripts: on a schedule and with the 立即运行 buttonthe script's owner

As with QubApp.mcp, the code must be last saved by its owner or by an administrator.

Limit of calls​

One run of code may make as many calls as the QubApp.mail calls per run field allows — the Mail from code group on the JavaScript tab of System; 100 by default. connect makes one call, mailboxOpen one, search one however many pages of the folder it reads, fetch one per letter, each flag and each hundred letters of an action one, sendMail one.

When a call fails​

A refused call throws an exception with the reason in words; a mistake in the parameters — a flag other than \Seen and \Flagged, a search condition the mailbox is not searched by, a field of a letter or an attachment that sendMail does not know, an attachment without content, an empty list of uids — is a TypeError. An exception that is not caught stops the run with an error.

What's next​