---
title: "$client->imports"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://openemail.uk/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 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://openemail.uk/docs/api/reference/imports#get-imports); TypeScript [`imports.list()`](https://openemail.uk/docs/sdk/reference/imports#list); Python [`imports.list()`](https://openemail.uk/docs/python/reference/imports#list); Ruby [`imports.list`](https://openemail.uk/docs/ruby/reference/imports#list); CLI [`openemail imports list`](https://openemail.uk/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://openemail.uk/docs/api/reference/imports#get-imports); TypeScript [`imports.listAll()`](https://openemail.uk/docs/sdk/reference/imports#listAll); Python [`imports.list_all()`](https://openemail.uk/docs/python/reference/imports#listAll); Ruby [`imports.list_all`](https://openemail.uk/docs/ruby/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://openemail.uk/docs/api/reference/imports#get-imports); TypeScript [`imports.iterate()`](https://openemail.uk/docs/sdk/reference/imports#iterate); Python [`imports.iterate()`](https://openemail.uk/docs/python/reference/imports#iterate); Ruby [`imports.iterate`](https://openemail.uk/docs/ruby/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 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`, `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://openemail.uk/docs/api/reference/imports#get-imports-id); TypeScript [`imports.get()`](https://openemail.uk/docs/sdk/reference/imports#get); Python [`imports.get()`](https://openemail.uk/docs/python/reference/imports#get); Ruby [`imports.get`](https://openemail.uk/docs/ruby/reference/imports#get); CLI [`openemail imports get`](https://openemail.uk/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://openemail.uk/docs/api/reference/imports#post-imports); TypeScript [`imports.create()`](https://openemail.uk/docs/sdk/reference/imports#create); Python [`imports.create()`](https://openemail.uk/docs/python/reference/imports#create); Ruby [`imports.create`](https://openemail.uk/docs/ruby/reference/imports#create); CLI [`openemail imports create`](https://openemail.uk/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://openemail.uk/docs/api/reference/imports#get-imports-id-upload); TypeScript [`imports.uploadState()`](https://openemail.uk/docs/sdk/reference/imports#uploadState); Python [`imports.upload_state()`](https://openemail.uk/docs/python/reference/imports#uploadState); Ruby [`imports.upload_state`](https://openemail.uk/docs/ruby/reference/imports#uploadState); CLI [`openemail imports upload-state`](https://openemail.uk/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://openemail.uk/docs/api/reference/imports#put-imports-id-files-file-chunks-chunk); TypeScript [`imports.uploadChunk()`](https://openemail.uk/docs/sdk/reference/imports#uploadChunk); Python [`imports.upload_chunk()`](https://openemail.uk/docs/python/reference/imports#uploadChunk); Ruby [`imports.upload_chunk`](https://openemail.uk/docs/ruby/reference/imports#uploadChunk); CLI [`openemail imports upload-chunk`](https://openemail.uk/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 OpenEmail\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://openemail.uk/docs/api/reference/imports#post-imports-id-start); TypeScript [`imports.start()`](https://openemail.uk/docs/sdk/reference/imports#start); Python [`imports.start()`](https://openemail.uk/docs/python/reference/imports#start); Ruby [`imports.start`](https://openemail.uk/docs/ruby/reference/imports#start); CLI [`openemail imports start`](https://openemail.uk/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 OpenEmail\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://openemail.uk/docs/api/reference/imports#post-imports-id-cancel); TypeScript [`imports.cancel()`](https://openemail.uk/docs/sdk/reference/imports#cancel); Python [`imports.cancel()`](https://openemail.uk/docs/python/reference/imports#cancel); Ruby [`imports.cancel`](https://openemail.uk/docs/ruby/reference/imports#cancel); CLI [`openemail imports cancel`](https://openemail.uk/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://openemail.uk/docs/api/reference/imports#get-imports-id-failures); TypeScript [`imports.listFailures()`](https://openemail.uk/docs/sdk/reference/imports#listFailures); Python [`imports.list_failures()`](https://openemail.uk/docs/python/reference/imports#listFailures); Ruby [`imports.list_failures`](https://openemail.uk/docs/ruby/reference/imports#listFailures); CLI [`openemail imports list-failures`](https://openemail.uk/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://openemail.uk/docs/api/reference/imports#delete-imports-id-upload); TypeScript [`imports.deleteUpload()`](https://openemail.uk/docs/sdk/reference/imports#deleteUpload); Python [`imports.delete_upload()`](https://openemail.uk/docs/python/reference/imports#deleteUpload); Ruby [`imports.delete_upload`](https://openemail.uk/docs/ruby/reference/imports#deleteUpload); CLI [`openemail imports delete-upload`](https://openemail.uk/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://openemail.uk/docs/api/reference/imports#post-imports); TypeScript [`imports.importFiles()`](https://openemail.uk/docs/sdk/reference/imports#importFiles); Python [`imports.import_files()`](https://openemail.uk/docs/python/reference/imports#importFiles); Ruby [`imports.import_files`](https://openemail.uk/docs/ruby/reference/imports#importFiles); CLI [`openemail imports import-files`](https://openemail.uk/docs/cli/reference/imports#imports-import-files).
