Skip to content

Core\Imap::open

Opens a mailbox on an IMAP server (the usual protocol to read mail) and signs in. The server and the sign-in are set in an [imap.<name>] block of nvs.toml. Your program passes only the name.

The block for a mail server has host, user, and password or password_file. The connection always uses TLS (the encryption that https uses), so the password is never sent as plain text. The name must also be listed in [capabilities.imap] connect. Otherwise open throws a RuntimeError. If a TLS connection fails, the error names the cause and the fix (TLS connections).

A block can also be a directory block (a folder on disk that works like a mailbox, for tests and local work). You write it as directory = "mailbox". Each subdirectory is a mail folder. Each <uid>.eml file in it is one message. The number in the file name is its UID, the number the server gives each message in a folder. The examples on this page use such a block.

Good to know: the connection closes when the request or the script ends. You do not close it yourself.

Parameters

  • $endpointstringThe name of the block, such as inbox for [imap.inbox]. It must be listed in [capabilities.imap] connect.

Return value

Returns Core\Imap\Mailbox. The open mailbox.

Errors

  • RuntimeErrorThe name is not listed in [capabilities.imap] connect, no block has that name, or the block has no sign-in.
  • IOErrorThe server cannot be reached, its certificate is not trusted, or it does not accept the sign-in.

Changelog

VersionChange
0.0.6A failed TLS connection names its cause, who can fix it and how, as TLS connection errors explains. For an expired certificate, the message gives the date it expired and the time on this host's clock. For a wrong pin, it gives the sha256// hash of the server's public key.
0.0.6Core\Imap reads a mailbox over IMAP (the protocol a program uses to read a mailbox on a server). A program opens an [imap.<name>] block of nvs.toml by its name, and then searches, fetches, moves, flags and deletes messages. For tests, a directory block reads a folder of .eml files instead of a server.

Examples

The examples below run with this configuration file.

nvs.toml
# The `inbox` block reads the folder `mailbox` beside this file. It needs no mail server.
[imap.inbox]
directory = "mailbox"
# A block for a real server. It has no `user` and `password`, so `open` throws for it.
[imap.office]
host = "imap.example.com"
# The `archive` block is not listed under `connect`, so the program may not open it.
[imap.archive]
directory = "mailbox"
# The program may open the `inbox` and `office` blocks.
[capabilities.imap]
connect = ["inbox", "office"]

Open a mailbox and count the messages

<?nvs
// Shows how to open a mailbox and count the messages in its inbox.
use Core\Arr;
use Core\Imap;
// `nvs.toml` is the settings file of the program.
// `open` takes the name of its block `[imap.inbox]`.
var $mailbox = Imap::open('inbox');
var $inbox = $mailbox->folder('INBOX');
// This prints "3 messages, 2 not read yet".
echo Arr::count($inbox->search()), ' messages, ';
echo Arr::count($inbox->search({ unseen: true })), " not read yet\n";
Output
3 messages, 2 not read yet

A block that cannot be opened

<?nvs
// Shows the errors `open` throws for a block that the program cannot open.
use Core\Imap;
// `[capabilities.imap] connect` is the list of blocks the program may open.
// `[imap.archive]` exists, but that list does not have it.
try {
Imap::open('archive');
} catch (RuntimeError $notGranted) {
echo $notGranted->message, "\n";
}
// `[imap.office]` has a server, but no `user` and `password`.
try {
Imap::open('office');
} catch (RuntimeError $noSignIn) {
echo $noSignIn->message, "\n";
}
Output
Core\Imap::open needs the capability `imap.connect` for archive, which is not granted
help: add `connect = ["archive"]` under `[capabilities.imap]` in nvs.toml
Core\Imap::open: `[imap.office]` sets no `user` and `password`, and a mailbox always needs a sign-in

A scheduled script reports new mail

<?nvs
// Shows a script that runs every few minutes and reports the new messages in each folder.
use Core\Arr;
use Core\Imap;
// The script opens the mailbox once. The connection closes when the script ends.
var $mailbox = Imap::open('inbox');
// A `tainted string` is text from outside your program.
// The server writes the folder names, so each name is a `tainted string`.
foreach ($mailbox->folders() as tainted string $name) {
var $folder = $mailbox->folder($name);
uint $new = Arr::count($folder->search({ unseen: true }));
// This prints "INBOX: 2 new" and "Sent: 0 new".
echo $name, ': ', $new, " new\n";
}
Output
INBOX: 2 new
Sent: 0 new