---
title: "$client->imports"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://nextmail.sh/docs/php/reference/imports"
area: "PHP"
category: "Reference"
---

# $client->imports

Every method in this namespace: its signature, its parameters, what it returns and an example.

## Methods

Bring an old mailbox across, straight from its mail server or from a Google Takeout, .mbox, .eml, .zip or .tgz export, with progress and a list of what did not come through.

### `imports->list`

List one page of mailbox imports

```php
list(
    ?string $addressId = null,
    ?int $limit = null,
    ?string $cursor = null,
    ?string $apiKey = null,
): Page
```

Returns one page of the imports in the workspace, newest first. Nothing is dropped from the history, so following `nextCursor` while `hasMore` is true reaches the very first import, and `imports->listAll` and `imports->iterate` do that walk for you. Each carries its status, the bytes read so far out of the total, and running counts of messages seen, imported, skipped as duplicates, left out by your options and not imported.

A key limited to particular addresses sees only imports into those addresses.

Scopes: `threads:read`.

**Parameters**

- `addressId` (`string`): Only imports into this address.
- `limit` (`int`): Rows per page, a whole number from 1 to 100. The server defaults to 25.
- `cursor` (`string`): The `nextCursor` from the previous page, an import id. One that names no import this key can see is a 400 `invalid_cursor`.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

A `Page` of import arrays, with `items`, `hasMore` and `nextCursor`.

**Example**

```php
$page = $client->imports->list(addressId: 'addr_5d1c9e3a7b2f4e60a8c1d3b9', limit: 10);

foreach ($page as $import) {
    echo $import['id'], ' ', $import['status'], ': ', $import['counts']['imported'], ' imported, ', $import['counts']['duplicate'], ' duplicates', PHP_EOL;
}
```

**Notes**

- A GET is retried automatically on network failure and on 408, 429, 500, 502, 503 and 504 responses, up to the client's `maxRetries`.

Also available in: API [`GET /imports`](https://nextmail.sh/docs/api/reference/imports#get-imports); TypeScript [`nextmail.imports.list()`](https://nextmail.sh/docs/sdk/reference/imports#list); Python [`imports.list()`](https://nextmail.sh/docs/python/reference/imports#list); Ruby [`imports.list`](https://nextmail.sh/docs/ruby/reference/imports#list); Go [`Imports.List`](https://nextmail.sh/docs/go/reference/imports#list); Java [`imports().list`](https://nextmail.sh/docs/java/reference/imports#list); C# [`Imports.ListAsync`](https://nextmail.sh/docs/csharp/reference/imports#list); CLI [`nextmail imports list`](https://nextmail.sh/docs/cli/reference/imports#imports-list).

### `imports->listAll`

Collect every mailbox import into one array

```php
listAll(
    ?string $addressId = null,
    ?int $limit = null,
    ?string $cursor = null,
    ?string $apiKey = null,
): array
```

Follows `nextCursor` from page to page and returns every import in the workspace, newest first, narrowed to one address when `addressId:` is given. A key limited to particular addresses sees only imports into those addresses.

Scopes: `threads:read`.

**Parameters**

- `addressId` (`string`): Only imports into this address.
- `limit` (`int`): Page size per request, from 1 to 100, defaulting to 25 on the server.
- `cursor` (`string`): An import id to start after, skipping everything newer.
- `apiKey` (`string`): Overrides the client's API key for every page of this walk.

**Returns**

A list of import arrays holding every import across all pages.

**Example**

```php
$imports = $client->imports->listAll();

$imported = array_sum(array_map(static fn(array $import): int => (int) $import['counts']['imported'], $imports));

echo $imported, ' messages imported across ', count($imports), ' imports', PHP_EOL;
```

**Notes**

- A failure on any page throws out of the whole call, and none of the pages already read are returned.

Also available in: API [`GET /imports`](https://nextmail.sh/docs/api/reference/imports#get-imports); TypeScript [`nextmail.imports.listAll()`](https://nextmail.sh/docs/sdk/reference/imports#listAll); Python [`imports.list_all()`](https://nextmail.sh/docs/python/reference/imports#listAll); Ruby [`imports.list_all`](https://nextmail.sh/docs/ruby/reference/imports#listAll); Go [`Imports.ListAll`](https://nextmail.sh/docs/go/reference/imports#listAll); Java [`imports().listAll`](https://nextmail.sh/docs/java/reference/imports#listAll); C# [`Imports.ListAllAsync`](https://nextmail.sh/docs/csharp/reference/imports#listAll).

### `imports->iterate`

Stream mailbox imports one at a time

```php
iterate(
    ?string $addressId = null,
    ?int $limit = null,
    ?string $cursor = null,
    ?string $apiKey = null,
): Generator
```

Returns a `Generator` that yields imports one at a time, newest first, and requests the next page only once the current one is used up. Breaking out of the `foreach` stops the requests.

Scopes: `threads:read`.

**Parameters**

- `addressId` (`string`): Only imports into this address.
- `limit` (`int`): Page size per request, from 1 to 100, defaulting to 25 on the server.
- `cursor` (`string`): An import id to start after, skipping everything newer.
- `apiKey` (`string`): Overrides the client's API key for every page of this walk.

**Returns**

A `Generator` that yields one import array per step.

**Example**

```php
foreach ($client->imports->iterate(addressId: 'addr_5d1c9e3a7b2f4e60a8c1d3b9') as $import) {
    if ($import['status'] === 'failed') {
        echo $import['id'], ' failed: ', $import['lastError'], PHP_EOL;
    }
}
```

**Notes**

- The generator is lazy, so an abandoned loop costs only the pages you consumed.

Also available in: API [`GET /imports`](https://nextmail.sh/docs/api/reference/imports#get-imports); TypeScript [`nextmail.imports.iterate()`](https://nextmail.sh/docs/sdk/reference/imports#iterate); Python [`imports.iterate()`](https://nextmail.sh/docs/python/reference/imports#iterate); Ruby [`imports.iterate`](https://nextmail.sh/docs/ruby/reference/imports#iterate); Go [`Imports.Iterate`](https://nextmail.sh/docs/go/reference/imports#iterate); Java [`imports().iterate`](https://nextmail.sh/docs/java/reference/imports#iterate); C# [`Imports.IterateAsync`](https://nextmail.sh/docs/csharp/reference/imports#iterate).

### `imports->get`

Read one mailbox import

```php
get(string $id, ?string $apiKey = null): array
```

Returns one import with its status, byte progress and counts. Poll it while `status` is `queued` or `running`. It settles on `completed`, `failed` or `cancelled`.

An import from a live mailbox has `source` set to `imap` or `microsoft` and carries `remote`, with the folders done out of the total and why it waits, and `children`, the contacts and calendar imports that came with it. Such an import can also wait: `parked` carries on by itself at `remote.resumeAt`, and `needs-password` stays until `imports->resume` sends a new password. An import from files has `source` set to `file`, `remote` null and no `children`.

An id from another workspace answers exactly like one that never existed, with 404.

Scopes: `threads:read`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array with `id`, `source`, `remote`, `children`, `addressId`, `address`, `status`, `options`, `files`, `formats`, `chunkBytes`, `totalBytes`, `processedBytes`, `counts`, `labelsCreated`, `labelsSkipped`, `lastError`, `uploadDeleted`, `createdAt`, `startedAt` and `finishedAt`.

**Example**

```php
$import = $client->imports->get('imp_4f8a2c6e1b9d3a7f5c0e2b84');

while (in_array($import['status'], ['queued', 'running'], true)) {
    echo $import['processedBytes'], ' of ', $import['totalBytes'], ' bytes read, ', $import['counts']['imported'], ' messages in', PHP_EOL;

    sleep(10);

    $import = $client->imports->get($import['id']);
}

echo $import['status'], ' ', $import['lastError'] ?? '', PHP_EOL;
```

**Notes**

- `lastError` is set only when `status` is `failed`.

Also available in: API [`GET /imports/{id}`](https://nextmail.sh/docs/api/reference/imports#get-imports-id); TypeScript [`nextmail.imports.get()`](https://nextmail.sh/docs/sdk/reference/imports#get); Python [`imports.get()`](https://nextmail.sh/docs/python/reference/imports#get); Ruby [`imports.get`](https://nextmail.sh/docs/ruby/reference/imports#get); Go [`Imports.Get`](https://nextmail.sh/docs/go/reference/imports#get); Java [`imports().get`](https://nextmail.sh/docs/java/reference/imports#get); C# [`Imports.GetAsync`](https://nextmail.sh/docs/csharp/reference/imports#get); CLI [`nextmail imports get`](https://nextmail.sh/docs/cli/reference/imports#imports-get).

### `imports->create`

Create a mailbox import and get its upload plan

```php
create(array $body, ?string $apiKey = null): array
```

Creates an import into one address of the workspace and returns it with `status` set to `uploading`. Each file is uploaded in parts of `chunkBytes` bytes, `$import['files'][$i]['chunks']` of them, through `imports->uploadChunk`, and `imports->start` then queues the import.

The import reads Google Takeout archives, .mbox files from Apple Mail, Thunderbird and most desktop apps, .eml files, and .zip or .tgz archives holding any of those, up to 100 GB a file and 50 files an import. Threads, dates and labels come across. Imported mail is quiet: it runs no rules, forwards, notifications, summaries or webhooks.

`imports->importFiles` does create, upload and start in one call.

Scopes: `threads:write`.

**Parameters**

- `addressId` (`string`, required): The address the mail belongs to. It must be an address this workspace owns and this key may act for.
- `files` (`array`, required): Each file as an array with `name` and `bytes`, its size, 1 to 50 of them, each at most 100 GB.
- `options.keepInbox` (`bool`): Default true: mail that was in the old inbox lands in Inbox with its unread state. False files everything under Archive.
- `options.includeSpam` (`bool`): Default false.
- `options.includeTrash` (`bool`): Default false.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array for the import with `status` set to `uploading`, `chunkBytes` and each file's `chunks`.

**Example**

```php
$import = $client->imports->create([
    'addressId' => 'addr_5d1c9e3a7b2f4e60a8c1d3b9',
    'files' => [['name' => 'mailbox.mbox', 'bytes' => filesize('mailbox.mbox')]],
    'options' => ['keepInbox' => true, 'includeSpam' => false],
]);

echo $import['id'], ' takes ', $import['files'][0]['chunks'], ' parts of ', $import['chunkBytes'], ' bytes', PHP_EOL;
```

**Notes**

- An address with an import already queued or running is 409 `already_running`.
- Not retried automatically.

Also available in: API [`POST /imports`](https://nextmail.sh/docs/api/reference/imports#post-imports); TypeScript [`nextmail.imports.create()`](https://nextmail.sh/docs/sdk/reference/imports#create); Python [`imports.create()`](https://nextmail.sh/docs/python/reference/imports#create); Ruby [`imports.create`](https://nextmail.sh/docs/ruby/reference/imports#create); Go [`Imports.Create`](https://nextmail.sh/docs/go/reference/imports#create); Java [`imports().create`](https://nextmail.sh/docs/java/reference/imports#create); C# [`Imports.CreateAsync`](https://nextmail.sh/docs/csharp/reference/imports#create); CLI [`nextmail imports create`](https://nextmail.sh/docs/cli/reference/imports#imports-create).

### `imports->uploadState`

See which parts of the upload have arrived

```php
uploadState(string $id, ?string $apiKey = null): array
```

For each file, the indexes of the parts already stored, so an interrupted upload sends only what is missing. Empty once the import has left the `uploading` state.

Scopes: `threads:write`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array with `received`, one sorted list of part indexes per file.

**Example**

```php
$import = $client->imports->get('imp_4f8a2c6e1b9d3a7f5c0e2b84');
$received = $client->imports->uploadState($import['id'])['received'][0] ?? [];

for ($chunk = 0; $chunk < $import['files'][0]['chunks']; $chunk++) {
    if (!in_array($chunk, $received, true)) {
        $part = file_get_contents('mailbox.mbox', false, null, $chunk * $import['chunkBytes'], $import['chunkBytes']);

        $client->imports->uploadChunk($import['id'], 0, $chunk, (string) $part);
    }
}

$client->imports->start($import['id']);
```

Also available in: API [`GET /imports/{id}/upload`](https://nextmail.sh/docs/api/reference/imports#get-imports-id-upload); TypeScript [`nextmail.imports.uploadState()`](https://nextmail.sh/docs/sdk/reference/imports#uploadState); Python [`imports.upload_state()`](https://nextmail.sh/docs/python/reference/imports#uploadState); Ruby [`imports.upload_state`](https://nextmail.sh/docs/ruby/reference/imports#uploadState); Go [`Imports.UploadState`](https://nextmail.sh/docs/go/reference/imports#uploadState); Java [`imports().uploadState`](https://nextmail.sh/docs/java/reference/imports#uploadState); C# [`Imports.UploadStateAsync`](https://nextmail.sh/docs/csharp/reference/imports#uploadState); CLI [`nextmail imports upload-state`](https://nextmail.sh/docs/cli/reference/imports#imports-upload-state).

### `imports->uploadChunk`

Upload one part of an import file

```php
uploadChunk(string $id, int $file, int $chunk, mixed $data, ?string $apiKey = null): array
```

Stores one part of file `file`: the bytes from `chunk * chunkBytes` up to the next part. Every part is exactly `chunkBytes` long except the last, and a part of the wrong length is 400 `bad_chunk`. Sending a part again replaces it, so a failed part can simply be retried.

Scopes: `threads:write`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `file` (`int`, required): The file index, in the order given to `imports->create`.
- `chunk` (`int`, required): The part index, from 0.
- `data` (`string|resource|SplFileInfo|StreamInterface`, required): The part as a string of bytes, a stream resource, an `SplFileInfo` or a PSR-7 stream. All of it is sent, so pass the part alone rather than the whole file.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array echoing `file`, `chunk` and the `bytes` stored.

**Example**

```php
$import = $client->imports->create([
    'addressId' => 'addr_5d1c9e3a7b2f4e60a8c1d3b9',
    'files' => [['name' => 'mailbox.mbox', 'bytes' => filesize('mailbox.mbox')]],
]);

for ($chunk = 0; $chunk < $import['files'][0]['chunks']; $chunk++) {
    $part = file_get_contents('mailbox.mbox', false, null, $chunk * $import['chunkBytes'], $import['chunkBytes']);

    $stored = $client->imports->uploadChunk($import['id'], 0, $chunk, (string) $part);

    echo 'Part ', $stored['chunk'], ': ', $stored['bytes'], ' bytes', PHP_EOL;
}

$client->imports->start($import['id']);
```

**Notes**

- Retried automatically on network failure and on 408, 429, 500, 502, 503 and 504 responses, since a repeated part replaces itself.

Also available in: API [`PUT /imports/{id}/files/{file}/chunks/{chunk}`](https://nextmail.sh/docs/api/reference/imports#put-imports-id-files-file-chunks-chunk); TypeScript [`nextmail.imports.uploadChunk()`](https://nextmail.sh/docs/sdk/reference/imports#uploadChunk); Python [`imports.upload_chunk()`](https://nextmail.sh/docs/python/reference/imports#uploadChunk); Ruby [`imports.upload_chunk`](https://nextmail.sh/docs/ruby/reference/imports#uploadChunk); Go [`Imports.UploadChunk`](https://nextmail.sh/docs/go/reference/imports#uploadChunk); Java [`imports().uploadChunk`](https://nextmail.sh/docs/java/reference/imports#uploadChunk); C# [`Imports.UploadChunkAsync`](https://nextmail.sh/docs/csharp/reference/imports#uploadChunk); CLI [`nextmail imports upload-chunk`](https://nextmail.sh/docs/cli/reference/imports#imports-upload-chunk).

### `imports->start`

Queue an import once its files are uploaded

```php
start(string $id, ?string $apiKey = null): array
```

Checks that every part of every file has arrived, recognises each file's format and queues the import. A file still missing parts is 412 `missing_chunks`, and a file that is not an archive or a mailbox is 400 `unsupported_file`. Calling it on an import that has already left `uploading` returns it unchanged.

Scopes: `threads:write`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array for the import with `status` set to `queued`.

**Example**

```php
use Nextmail\Exception\ApiException;

try {
    $import = $client->imports->start('imp_4f8a2c6e1b9d3a7f5c0e2b84');

    echo $import['status'], ', read as ', implode(', ', $import['formats']), PHP_EOL;
} catch (ApiException $error) {
    if ($error->errorCode !== 'missing_chunks') {
        throw $error;
    }

    print_r($client->imports->uploadState('imp_4f8a2c6e1b9d3a7f5c0e2b84')['received']);
}
```

Also available in: API [`POST /imports/{id}/start`](https://nextmail.sh/docs/api/reference/imports#post-imports-id-start); TypeScript [`nextmail.imports.start()`](https://nextmail.sh/docs/sdk/reference/imports#start); Python [`imports.start()`](https://nextmail.sh/docs/python/reference/imports#start); Ruby [`imports.start`](https://nextmail.sh/docs/ruby/reference/imports#start); Go [`Imports.Start`](https://nextmail.sh/docs/go/reference/imports#start); Java [`imports().start`](https://nextmail.sh/docs/java/reference/imports#start); C# [`Imports.StartAsync`](https://nextmail.sh/docs/csharp/reference/imports#start); CLI [`nextmail imports start`](https://nextmail.sh/docs/cli/reference/imports#imports-start).

### `imports->cancel`

Cancel a mailbox import

```php
cancel(string $id, ?string $apiKey = null): array
```

Stops an import at its next checkpoint. Mail already imported stays in the mailbox. A finished import is 409 `not_cancellable`.

Scopes: `threads:write`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array for the import with `status` set to `cancelled`.

**Example**

```php
use Nextmail\Exception\ConflictException;

try {
    $import = $client->imports->cancel('imp_4f8a2c6e1b9d3a7f5c0e2b84');

    echo $import['status'], ', ', $import['counts']['imported'], ' messages stay', PHP_EOL;
} catch (ConflictException) {
    echo 'The import already finished', PHP_EOL;
}
```

Also available in: API [`POST /imports/{id}/cancel`](https://nextmail.sh/docs/api/reference/imports#post-imports-id-cancel); TypeScript [`nextmail.imports.cancel()`](https://nextmail.sh/docs/sdk/reference/imports#cancel); Python [`imports.cancel()`](https://nextmail.sh/docs/python/reference/imports#cancel); Ruby [`imports.cancel`](https://nextmail.sh/docs/ruby/reference/imports#cancel); Go [`Imports.Cancel`](https://nextmail.sh/docs/go/reference/imports#cancel); Java [`imports().cancel`](https://nextmail.sh/docs/java/reference/imports#cancel); C# [`Imports.CancelAsync`](https://nextmail.sh/docs/csharp/reference/imports#cancel); CLI [`nextmail imports cancel`](https://nextmail.sh/docs/cli/reference/imports#imports-cancel).

### `imports->listFailures`

List what an import could not bring across

```php
listFailures(string $id, ?int $after = null, ?int $limit = null, ?string $apiKey = null): array
```

Each message or archive entry that did not come across, with the reason: `too-large` (over 50 MB), `unparseable`, `no-date`, `storage-error`, `unreadable-entry`, `encrypted-entry` or `archive-limit`. Page with `after:`, passing the `nextCursor` of the previous page.

Scopes: `threads:read`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `after` (`int`): The `nextCursor` of the previous page.
- `limit` (`int`): At most 100, the default.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array with `data`, a list of failures, and `nextCursor`, the number to pass as `after:` for the next page, or null after the last one.

**Example**

```php
$failures = $client->imports->listFailures('imp_4f8a2c6e1b9d3a7f5c0e2b84', limit: 50);

foreach ($failures['data'] as $failure) {
    echo $failure['reason'], ': ', $failure['subject'] ?? $failure['source'], PHP_EOL;
}

if ($failures['nextCursor'] !== null) {
    $more = $client->imports->listFailures('imp_4f8a2c6e1b9d3a7f5c0e2b84', after: $failures['nextCursor'], limit: 50);

    echo count($more['data']), ' more on the next page', PHP_EOL;
}
```

Also available in: API [`GET /imports/{id}/failures`](https://nextmail.sh/docs/api/reference/imports#get-imports-id-failures); TypeScript [`nextmail.imports.listFailures()`](https://nextmail.sh/docs/sdk/reference/imports#listFailures); Python [`imports.list_failures()`](https://nextmail.sh/docs/python/reference/imports#listFailures); Ruby [`imports.list_failures`](https://nextmail.sh/docs/ruby/reference/imports#listFailures); Go [`Imports.ListFailures`](https://nextmail.sh/docs/go/reference/imports#listFailures); Java [`imports().listFailures`](https://nextmail.sh/docs/java/reference/imports#listFailures); C# [`Imports.ListFailuresAsync`](https://nextmail.sh/docs/csharp/reference/imports#listFailures); CLI [`nextmail imports list-failures`](https://nextmail.sh/docs/cli/reference/imports#imports-list-failures).

### `imports->deleteUpload`

Delete the files uploaded for an import

```php
deleteUpload(string $id, ?string $apiKey = null): array
```

Removes the uploaded archive. Mail already imported stays in the mailbox. An import still uploading is cancelled at the same time, and one that is queued or running is 409 `still_running`.

Scopes: `threads:write`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array for the import with `uploadDeleted` set to true.

**Example**

```php
$import = $client->imports->deleteUpload('imp_4f8a2c6e1b9d3a7f5c0e2b84');

echo $import['uploadDeleted'] ? 'The uploaded files are gone' : 'The files are still stored', ', status ', $import['status'], PHP_EOL;
```

Also available in: API [`DELETE /imports/{id}/upload`](https://nextmail.sh/docs/api/reference/imports#delete-imports-id-upload); TypeScript [`nextmail.imports.deleteUpload()`](https://nextmail.sh/docs/sdk/reference/imports#deleteUpload); Python [`imports.delete_upload()`](https://nextmail.sh/docs/python/reference/imports#deleteUpload); Ruby [`imports.delete_upload`](https://nextmail.sh/docs/ruby/reference/imports#deleteUpload); Go [`Imports.DeleteUpload`](https://nextmail.sh/docs/go/reference/imports#deleteUpload); Java [`imports().deleteUpload`](https://nextmail.sh/docs/java/reference/imports#deleteUpload); C# [`Imports.DeleteUploadAsync`](https://nextmail.sh/docs/csharp/reference/imports#deleteUpload); CLI [`nextmail imports delete-upload`](https://nextmail.sh/docs/cli/reference/imports#imports-delete-upload).

### `imports->importFiles`

Create, upload and start an import in one call

```php
importFiles(array $input, ?string $apiKey = null): array
```

Creates the import, uploads every file part by part and starts it, reporting progress through `onProgress`. Pass each file's `data` as a string of bytes, a stream resource, an `SplFileInfo` or a PSR-7 stream. Parts are sliced from it, so an `SplFileInfo` or a seekable stream is read one part at a time and never held in memory whole.

It returns once the import is queued. Poll `imports->get` to follow it.

Scopes: `threads:write`.

**Parameters**

- `addressId` (`string`, required): The address the mail belongs to.
- `files` (`array`, required): Each file as an array with `name` and `data`. A file whose `data` is not one of the byte sources above throws an `InvalidArgumentException` before anything is sent.
- `options` (`array`): `keepInbox`, `includeSpam` and `includeTrash`, as for `imports->create`.
- `onProgress` (`callable`): Called after each part with two integers: the bytes uploaded so far and the total.
- `apiKey` (`string`): Overrides the client's API key for every request of this call.

**Returns**

An array for the import with `status` set to `queued`.

**Example**

```php
$import = $client->imports->importFiles([
    'addressId' => 'addr_5d1c9e3a7b2f4e60a8c1d3b9',
    'files' => [
        ['name' => 'takeout.tgz', 'data' => new \SplFileInfo('takeout.tgz')],
        ['name' => 'mailbox.mbox', 'data' => fopen('mailbox.mbox', 'rb')],
    ],
    'options' => ['keepInbox' => false],
    'onProgress' => static function (int $uploaded, int $total): void {
        echo $uploaded, ' of ', $total, ' bytes uploaded', PHP_EOL;
    },
]);

echo $import['id'], ' is ', $import['status'], PHP_EOL;
```

**Notes**

- If a part still fails after its retries, the exception is thrown and the import is left in `uploading`. `imports->uploadState` then tells you which parts to send before calling `imports->start`.

Also available in: API [`POST /imports`](https://nextmail.sh/docs/api/reference/imports#post-imports); TypeScript [`nextmail.imports.importFiles()`](https://nextmail.sh/docs/sdk/reference/imports#importFiles); Python [`imports.import_files()`](https://nextmail.sh/docs/python/reference/imports#importFiles); Ruby [`imports.import_files`](https://nextmail.sh/docs/ruby/reference/imports#importFiles); Go [`Imports.ImportFiles`](https://nextmail.sh/docs/go/reference/imports#importFiles); Java [`imports().importFiles`](https://nextmail.sh/docs/java/reference/imports#importFiles); C# [`Imports.ImportFilesAsync`](https://nextmail.sh/docs/csharp/reference/imports#importFiles); CLI [`nextmail imports import-files`](https://nextmail.sh/docs/cli/reference/imports#imports-import-files).

### `imports->discoverMailbox`

Find the mail server for an address

```php
discoverMailbox(string $email, ?string $apiKey = null): array
```

Looks up where the mailbox behind an address lives, so an import can connect to it. It tries the providers Nextmail knows, then who receives mail for the domain, then Mozilla's public list of mail settings, then the domain's own `_imaps._tcp` record. Nothing is stored and no sign-in is attempted.

`server` is null when nothing was found: ask for the host and port and pass them to `imports->checkMailbox`. `signIn` says what the mailbox takes. `app-password` and `password` are what `imports->checkMailbox` and `imports->connectMailbox` send. An Outlook.com or Microsoft 365 mailbox answers `microsoft`, which only the Microsoft sign-in on the Migrations page of the app can open.

Scopes: `threads:write`.

**Parameters**

- `email` (`string`, required): The address of the old mailbox.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array with `object` set to `mailbox_settings`, `email`, `provider`, `signIn`, `server`, `source`, `username`, and `contacts` and `calendars`, which say whether those can come across too. `server` is an array with `host`, `port` and `security`, or null. `provider`, `signIn` and `source` take the values of `MailboxProviders`, `MailboxSignIns` and `MailboxSettingsSources` in `Nextmail\Constants`.

**Example**

```php
use Nextmail\Constants\MailboxSignIns;

$settings = $client->imports->discoverMailbox('ada@gmail.com');

if ($settings['signIn'] === MailboxSignIns::MICROSOFT) {
    echo 'Connect this mailbox from the Migrations page of the app', PHP_EOL;
} elseif ($settings['server'] === null) {
    echo 'Ask for the IMAP host and port of ', $settings['email'], PHP_EOL;
} else {
    echo $settings['provider'], ' takes ', $settings['signIn'], ' at ', $settings['server']['host'], ':', $settings['server']['port'], PHP_EOL;
}
```

**Notes**

- Something that is not an email address is a 400 `mailbox_address_invalid`.
- Too many lookups in an hour is a 429 `mailbox_checks_limited`.
- A GET is retried automatically on network failure and on 408, 500, 502, 503 and 504 responses, up to the client's `maxRetries`, and on a 429 only when it carries a `Retry-After` of a minute or less.

Also available in: API [`GET /imports/mailbox-settings`](https://nextmail.sh/docs/api/reference/imports#get-imports-mailbox-settings); TypeScript [`nextmail.imports.discoverMailbox()`](https://nextmail.sh/docs/sdk/reference/imports#discoverMailbox); Python [`imports.discover_mailbox()`](https://nextmail.sh/docs/python/reference/imports#discoverMailbox); Ruby [`imports.discover_mailbox`](https://nextmail.sh/docs/ruby/reference/imports#discoverMailbox); Go [`Imports.DiscoverMailbox`](https://nextmail.sh/docs/go/reference/imports#discoverMailbox); Java [`imports().discoverMailbox`](https://nextmail.sh/docs/java/reference/imports#discoverMailbox); C# [`Imports.DiscoverMailboxAsync`](https://nextmail.sh/docs/csharp/reference/imports#discoverMailbox); CLI [`nextmail imports discover-mailbox`](https://nextmail.sh/docs/cli/reference/imports#imports-discover-mailbox).

### `imports->checkMailbox`

Check a mailbox before importing it

```php
checkMailbox(array $body, ?string $apiKey = null): array
```

Signs in to the old mailbox over IMAP, lists its folders and counts the messages, then signs out. Nothing is stored, the password is not kept and the old mailbox is not changed. Use it to show what an import would bring before starting one.

Leave `host` out to have the server looked up from the address, as `imports->discoverMailbox` does. Only port 993 with TLS, or port 143 with STARTTLS, is accepted. Each person may make 10 sign-in attempts an hour across `imports->checkMailbox`, `imports->connectMailbox` and `imports->resume`, and every refused sign-in reads the same, whatever the reason.

Scopes: `threads:write`.

**Parameters**

- `email` (`string`, required): The address of the old mailbox.
- `password` (`string`, required): The password of the old mailbox, or an app password where `imports->discoverMailbox` answers `signIn` as `app-password`.
- `host` (`string`): The IMAP host, such as `imap.example.com`. It has to be a public host. Left out, the server is looked up from the address.
- `port` (`int`): 993 or 143, read only with `host`. Left out, it follows `security`: 993 for `tls` and 143 for `starttls`.
- `security` (`string`): `tls` or `starttls`, the values of `Nextmail\Constants\MailboxSecurities`, read only with `host`. Left out, it follows `port`, and is `tls` when both are left out.
- `username` (`string`): The name to sign in with. Left out, it is the `username` that `imports->discoverMailbox` answers, usually the address.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array with `object` set to `mailbox_check`, `provider`, the `server` and `username` that worked, `folders`, `messages`, the total an import would read, and whether `contacts` and `calendars` can come across too. `folders` holds up to 200 arrays with `name`, `role`, one of `Nextmail\Constants\MailboxFolderRoles`, `messages` and `included`, which says whether an import with the default options reads the folder.

**Example**

```php
$check = $client->imports->checkMailbox([
    'email' => 'ada@gmail.com',
    'password' => (string) getenv('OLD_MAILBOX_PASSWORD'),
]);

$included = array_filter($check['folders'], static fn(array $folder): bool => $folder['included']);

echo $check['messages'], ' messages in ', count($included), ' folders on ', $check['server']['host'], PHP_EOL;
```

**Notes**

- A sign-in the mail server does not accept is a 400 `mailbox_login_failed`, with no more detail than that.
- No server found for the address is a 400 `mailbox_settings_unknown`: send `host`. A host that is not public, or a port other than 993 or 143, is a 400 `mailbox_host_not_allowed`.
- A mailbox that only opens through the Microsoft sign-in is a 400 `mailbox_uses_microsoft`.
- A mail server that does not answer is a 502 `mailbox_unreachable`, and too many sign-in attempts in an hour is a 429 `mailbox_checks_limited`.
- Spam and bin folders come back with `included` false, since an import reads them only when asked to.
- Not retried automatically.

Also available in: API [`POST /imports/mailbox/check`](https://nextmail.sh/docs/api/reference/imports#post-imports-mailbox-check); TypeScript [`nextmail.imports.checkMailbox()`](https://nextmail.sh/docs/sdk/reference/imports#checkMailbox); Python [`imports.check_mailbox()`](https://nextmail.sh/docs/python/reference/imports#checkMailbox); Ruby [`imports.check_mailbox`](https://nextmail.sh/docs/ruby/reference/imports#checkMailbox); Go [`Imports.CheckMailbox`](https://nextmail.sh/docs/go/reference/imports#checkMailbox); Java [`imports().checkMailbox`](https://nextmail.sh/docs/java/reference/imports#checkMailbox); C# [`Imports.CheckMailboxAsync`](https://nextmail.sh/docs/csharp/reference/imports#checkMailbox); CLI [`nextmail imports check-mailbox`](https://nextmail.sh/docs/cli/reference/imports#imports-check-mailbox).

### `imports->connectMailbox`

Import a live mailbox

```php
connectMailbox(array $body, ?string $apiKey = null): array
```

Starts copying an old mailbox into one address, straight from its mail server. It signs in once to check the password, keeps the password sealed until the import finishes and for 30 days at most, and queues the import. Poll `imports->get` for progress.

The old mailbox is only ever read. Folders become labels, read and starred state come across, and a message the address already holds is skipped, so running an import twice adds nothing. A Gmail mailbox is read from All Mail, with its labels. An import waits by itself when the old provider slows it down, such as Gmail's daily download limit, and carries on from the same folder afterwards: `status` is `parked` and `remote.resumeAt` says when. If the password stops working, `status` is `needs-password` until `imports->resume` sends a new one.

`contacts` and `calendars` bring those across too where the provider offers them, which needs `contacts:write` and `calendar:write` as well. They arrive as contacts and calendar imports of their own, listed in `children`. `rerunOf` names a finished import of the same mailbox, at most 30 days old, so only mail that arrived since is fetched. Leave `host` out on such a run and it connects to the same server. One import may run into an address at a time.

Scopes: `threads:write`.

**Parameters**

- `addressId` (`string`, required): The address the mail belongs to. It must be an address this workspace owns and this key may act for.
- `email` (`string`, required): The address of the old mailbox.
- `password` (`string`, required): The password of the old mailbox, or an app password where `imports->discoverMailbox` answers `signIn` as `app-password`.
- `host` (`string`): The IMAP host, such as `imap.example.com`. It has to be a public host. Left out, the server is looked up from the address.
- `port` (`int`): 993 or 143, read only with `host`. Left out, it follows `security`: 993 for `tls` and 143 for `starttls`.
- `security` (`string`): `tls` or `starttls`, the values of `Nextmail\Constants\MailboxSecurities`, read only with `host`. Left out, it follows `port`, and is `tls` when both are left out.
- `username` (`string`): The name to sign in with. Left out, it is the `username` that `imports->discoverMailbox` answers, usually the address.
- `options.keepInbox` (`bool`): Default true: mail that was in the old inbox lands in Inbox with its unread state. False files everything under Archive.
- `options.includeSpam` (`bool`): Default false.
- `options.includeTrash` (`bool`): Default false.
- `contacts` (`bool`): True brings the address book across too, where the provider offers it. Needs `contacts:write`.
- `calendars` (`bool`): True brings the calendars across too, where the provider offers them. Needs `calendar:write`.
- `rerunOf` (`string`): The id of a finished import of the same mailbox, at most 30 days old. Only mail that arrived since is fetched.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array for the import with `status` set to `queued`, `source` set to `imap`, the `remote` mailbox it reads and the `children` it started. `remote` is an array with `provider`, `host`, `username`, `foldersTotal`, `foldersDone`, `messagesTotal`, `pauseReason`, `resumeAt`, `rerunOf` and `rerunnable`, and each of `children` an array with `id`, `kind`, `source`, `status`, `imported`, `skipped` and `total`.

**Example**

```php
$import = $client->imports->connectMailbox([
    'addressId' => 'addr_5d1c9e3a7b2f4e60a8c1d3b9',
    'email' => 'ada@gmail.com',
    'password' => (string) getenv('OLD_MAILBOX_PASSWORD'),
    'contacts' => true,
    'calendars' => true,
]);

echo $import['id'], ' is ', $import['status'], ', reading ', $import['remote']['host'] ?? 'the old mailbox', PHP_EOL;

foreach ($import['children'] as $child) {
    echo 'The ', $child['kind'], ' come across as ', $child['id'], PHP_EOL;
}
```

**Notes**

- An address with an import already under way is 409 `already_running`, and an import named in `rerunOf` that cannot be continued is 409 `rerun_not_available`.
- An address outside the workspace is 404 `address_not_found`, and one the key may not act for is 403 `address_not_allowed`.
- The sign-in is checked before anything is queued, so it fails as `imports->checkMailbox` does: 400 `mailbox_login_failed`, `mailbox_settings_unknown`, `mailbox_host_not_allowed` or `mailbox_uses_microsoft`, 502 `mailbox_unreachable` and 429 `mailbox_checks_limited`.
- `children` is filled here and by `imports->get`. `imports->list` returns it empty.
- Not retried automatically.

Also available in: API [`POST /imports/mailbox`](https://nextmail.sh/docs/api/reference/imports#post-imports-mailbox); TypeScript [`nextmail.imports.connectMailbox()`](https://nextmail.sh/docs/sdk/reference/imports#connectMailbox); Python [`imports.connect_mailbox()`](https://nextmail.sh/docs/python/reference/imports#connectMailbox); Ruby [`imports.connect_mailbox`](https://nextmail.sh/docs/ruby/reference/imports#connectMailbox); Go [`Imports.ConnectMailbox`](https://nextmail.sh/docs/go/reference/imports#connectMailbox); Java [`imports().connectMailbox`](https://nextmail.sh/docs/java/reference/imports#connectMailbox); C# [`Imports.ConnectMailboxAsync`](https://nextmail.sh/docs/csharp/reference/imports#connectMailbox); CLI [`nextmail imports connect-mailbox`](https://nextmail.sh/docs/cli/reference/imports#imports-connect-mailbox).

### `imports->resume`

Resume a waiting mailbox import

```php
resume(string $id, ?array $body = null, ?string $apiKey = null): array
```

Carries on an import from a live mailbox that is waiting. One with `status` set to `parked` is queued again at once, with the id alone. One with `status` set to `needs-password` needs the new `password`, passed in an array as the second argument, which is checked against the old mailbox before the import is queued. It picks up at the folder and message it stopped at.

An import that came from a Microsoft sign-in is resumed by signing in again on the Migrations page of the app.

Scopes: `threads:write`.

**Parameters**

- `id` (`string`, required): Import id, `imp_` followed by 24 hex characters.
- `password` (`string`): The new password of the old mailbox. Required when `status` is `needs-password`.
- `apiKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An array for the import with `status` set to `queued`.

**Example**

```php
$parked = $client->imports->resume('imp_3f9c2a7b1e4d8f60a5c7b92d');

echo $parked['id'], ' is ', $parked['status'], PHP_EOL;

$signedIn = $client->imports->resume('imp_9d2c7b5a06f8d4e1b7a2c9f3', ['password' => (string) getenv('OLD_MAILBOX_PASSWORD')]);

echo $signedIn['id'], ' is ', $signedIn['status'], PHP_EOL;
```

**Notes**

- An import that is not waiting, or that reads uploaded files, is 409 `not_resumable`.
- Leaving `password` out for an import that needs one is 400 `password_required`, and a password the mail server does not accept is 400 `mailbox_login_failed`.
- A new password counts towards the 10 sign-in attempts an hour that `imports->checkMailbox` and `imports->connectMailbox` share.
- A parked import carries on by itself at `remote.resumeAt`, so calling this only tries sooner.
- Not retried automatically.

Also available in: API [`POST /imports/{id}/resume`](https://nextmail.sh/docs/api/reference/imports#post-imports-id-resume); TypeScript [`nextmail.imports.resume()`](https://nextmail.sh/docs/sdk/reference/imports#resume); Python [`imports.resume()`](https://nextmail.sh/docs/python/reference/imports#resume); Ruby [`imports.resume`](https://nextmail.sh/docs/ruby/reference/imports#resume); Go [`Imports.Resume`](https://nextmail.sh/docs/go/reference/imports#resume); Java [`imports().resume`](https://nextmail.sh/docs/java/reference/imports#resume); C# [`Imports.ResumeAsync`](https://nextmail.sh/docs/csharp/reference/imports#resume); CLI [`nextmail imports resume`](https://nextmail.sh/docs/cli/reference/imports#imports-resume).
