---
title: "client.imports"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://nextmail.sh/docs/ruby/reference/imports"
area: "Ruby"
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

```ruby
list(address_id: nil, limit: nil, cursor: nil, api_key: nil) -> Nextmail::Page
```

Returns one page of the imports in the workspace, newest first. Nothing is dropped from the history, so following `next_cursor` while `has_more?` is true reaches the very first import, and `list_all` and `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**

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

**Returns**

The `Nextmail::Page` of import Hashes, with `items`, `has_more?` and `next_cursor`.

**Example**

```ruby
page = client.imports.list(limit: 50)

page.items.each do |item|
  puts "#{item[:address]} #{item[:status]} #{item[:counts][:imported]}"
end
```

**Notes**

- A GET is retried automatically on network failure and on 408, 500, 502, 503 and 504 responses, up to the client's `max_retries`, and on a 429 only when it carries a `Retry-After` of a minute or less.

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); PHP [`imports->list`](https://nextmail.sh/docs/php/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.list_all`

Collect every mailbox import into one array

```ruby
list_all(address_id: nil, limit: nil, cursor: nil, api_key: nil) -> Array<Hash>
```

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

Scopes: `threads:read`.

**Parameters**

- `address_id` (`String`): Only imports into this address.
- `limit` (`Integer`): 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.
- `api_key` (`String`): Overrides the client API key for every page of this walk.

**Returns**

An Array of import Hashes holding every import across all pages.

**Example**

```ruby
imports = client.imports.list_all

failed = imports.select { |item| item[:status] == "failed" }

puts "#{failed.length} of #{imports.length} imports failed"
```

**Notes**

- A failure on any page raises 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); PHP [`imports->listAll`](https://nextmail.sh/docs/php/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

```ruby
iterate(address_id: nil, limit: nil, cursor: nil, api_key: nil, &block) -> Enumerator<Hash>
```

Returns an Enumerator that yields imports individually, newest first, or yields each one to a block when given one, and requests the next page only once the current one is drained. Breaking out of the loop stops the requests.

Scopes: `threads:read`.

**Parameters**

- `address_id` (`String`): Only imports into this address.
- `limit` (`Integer`): 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.
- `api_key` (`String`): Overrides the client API key for every page of this walk.

**Returns**

An Enumerator of import Hashes (or yields each one to a block), one import per step.

**Example**

```ruby
running = client.imports.iterate(address_id: "addr_2b7e").find { |item| item[:status] == "running" }

puts running[:id], running[:counts][:imported] if running
```

**Notes**

- The Enumerator 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); PHP [`imports->iterate`](https://nextmail.sh/docs/php/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

```ruby
get(id, api_key: nil) -> Hash
```

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` `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 `resume` sends a new password. An import from files has `source` `file`, `remote` nil 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.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

A Hash with `id`, `source`, `remote`, `children`, `addressId`, `address`, `status`, `options`, `files`, `formats`, `chunkBytes`, `totalBytes`, `processedBytes`, `counts`, `labelsCreated`, `labelsSkipped`, `lastError`, `uploadDeleted`, `createdAt`, `startedAt` and `finishedAt`. `counts` holds `seen`, `imported`, `duplicate`, `filtered`, `failed` and `threads`. `remote` is nil or a Hash with `provider`, `host`, `username`, `foldersTotal`, `foldersDone`, `messagesTotal`, `pauseReason`, `resumeAt`, `rerunOf` and `rerunnable`, and each of `children` is a Hash with `id`, `kind`, `source`, `status`, `imported`, `skipped` and `total`.

**Example**

```ruby
item = client.imports.get("imp_3f9c2a7b1e4d8f60a5c7b92d")

puts "#{item[:status]} #{item[:processedBytes]} / #{item[:totalBytes]}"
```

**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); PHP [`imports->get`](https://nextmail.sh/docs/php/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

```ruby
create(body = nil, api_key: nil, **fields) -> Hash
```

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`, `files[i][:chunks]` of them, through `upload_chunk`, and `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.

`import_files` 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<Hash>`, required): Each file as a Hash with `name` and `bytes`, its size, 1 to 50 of them, each at most 100 GB.
- `options.keepInbox` (`Boolean`): Default true: mail that was in the old inbox lands in Inbox with its unread state. False files everything under Archive. It goes inside the `options` Hash, as do the next two.
- `options.includeSpam` (`Boolean`): Default false.
- `options.includeTrash` (`Boolean`): Default false.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

A Hash for the import, shaped like the one `get` returns, with `status` set to `uploading`, `chunkBytes` and each file's `chunks`.

**Example**

```ruby
item = client.imports.create(
  addressId: "addr_2b7e",
  files: [{name: "takeout-001.zip", bytes: 2_147_483_648}],
  options: {keepInbox: true, includeSpam: false}
)

puts item[:chunkBytes], item[:files][0][:chunks]
```

**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); PHP [`imports->create`](https://nextmail.sh/docs/php/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.upload_state`

See which parts of the upload have arrived

```ruby
upload_state(id, api_key: nil) -> Hash
```

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.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

A Hash with `id`, `status` and `received`, one sorted Array of part indexes per file.

**Example**

```ruby
state = client.imports.upload_state("imp_3f9c2a7b1e4d8f60a5c7b92d")

puts state[:received][0].length
```

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); PHP [`imports->uploadState`](https://nextmail.sh/docs/php/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.upload_chunk`

Upload one part of an import file

```ruby
upload_chunk(id, file, chunk, data, api_key: nil) -> Hash
```

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` (`Integer`, required): The file index, in the order given to `create`.
- `chunk` (`Integer`, required): The part index, from 0.
- `data` (`String, IO or Pathname`, required): The part as a binary String, or an IO or a Pathname whose whole content is the part. It is sent as it is, so slice the part out of the file first.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

A Hash echoing `file`, `chunk` and the `bytes` stored.

**Example**

```ruby
size = File.size("takeout-001.zip")
item = client.imports.create(addressId: "addr_2b7e", files: [{name: "takeout-001.zip", bytes: size}])
part = File.binread("takeout-001.zip", item[:chunkBytes], 0)

client.imports.upload_chunk(item[:id], 0, 0, part)
```

**Notes**

- Retried automatically on network failure and on 408, 500, 502, 503 and 504 responses, and on a 429 only when it carries a `Retry-After` of a minute or less, 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); PHP [`imports->uploadChunk`](https://nextmail.sh/docs/php/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

```ruby
start(id, api_key: nil) -> Hash
```

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.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

The import Hash, shaped like the one `get` returns, with `status` set to `queued`.

**Example**

```ruby
queued = client.imports.start("imp_3f9c2a7b1e4d8f60a5c7b92d")

puts queued[:status]
```

**Notes**

- Retried automatically on network failure and on 408, 500, 502, 503 and 504 responses, and on a 429 only when it carries a `Retry-After` of a minute or less, since calling it again returns the import unchanged.

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); PHP [`imports->start`](https://nextmail.sh/docs/php/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

```ruby
cancel(id, api_key: nil) -> Hash
```

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.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

The import Hash, shaped like the one `get` returns, with `status` set to `cancelled`.

**Example**

```ruby
client.imports.cancel("imp_3f9c2a7b1e4d8f60a5c7b92d")
```

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); PHP [`imports->cancel`](https://nextmail.sh/docs/php/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.list_failures`

List what an import could not bring across

```ruby
list_failures(id, after: nil, limit: nil, api_key: nil) -> Hash
```

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` (`Integer`): The `nextCursor` of the previous page.
- `limit` (`Integer`): At most 100, the default.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

A Hash, not `Nextmail::Page`, with `data`, an Array of Hashes with `id`, `seq`, `reason`, `subject`, `sender`, `messageId`, `source`, `bytes`, `detail` and `createdAt`, and `nextCursor`, an Integer or nil on the last page.

**Example**

```ruby
failures = client.imports.list_failures("imp_3f9c2a7b1e4d8f60a5c7b92d")

failures[:data].each { |failure| puts "#{failure[:reason]} #{failure[:subject]}" }
```

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); PHP [`imports->listFailures`](https://nextmail.sh/docs/php/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.delete_upload`

Delete the files uploaded for an import

```ruby
delete_upload(id, api_key: nil) -> Hash
```

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.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

The import Hash, shaped like the one `get` returns, with `uploadDeleted` set to true.

**Example**

```ruby
client.imports.delete_upload("imp_3f9c2a7b1e4d8f60a5c7b92d")
```

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); PHP [`imports->deleteUpload`](https://nextmail.sh/docs/php/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.import_files`

Create, upload and start an import in one call

```ruby
import_files(input = nil, api_key: nil, **fields, &block) -> Hash
```

Creates the import, uploads every file part by part and starts it, reporting progress to a block or through `onProgress`. Pass each file's `data` as a binary String, an IO or a Pathname; parts are sliced from it, so a Pathname or an open File is never read into memory at once.

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

Scopes: `threads:write`.

**Parameters**

- `addressId` (`String`, required): The address the mail belongs to.
- `files` (`Array<Hash>`, required): Each file as a Hash with `name` and `data`, where `data` is a binary String, an IO or a Pathname.
- `options` (`Hash`): `keepInbox`, `includeSpam` and `includeTrash`, as for `create`.
- `onProgress` (`Proc`): Anything that responds to `call`, called after each part with the bytes uploaded and the total. A block given to the call does the same job and is used instead when both are given.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

The import Hash, shaped like the one `get` returns, with `status` set to `queued`.

**Example**

```ruby
files = [{name: "takeout-001.zip", data: Pathname("takeout-001.zip")}]

item = client.imports.import_files(addressId: "addr_2b7e", files:) do |done, total|
  puts "#{(done * 100.0 / total).round}%"
end

puts item[:id], item[:status]
```

**Notes**

- If a part fails after its retries, the import is left in `uploading`; `upload_state` then tells you which parts to send before calling `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); PHP [`imports->importFiles`](https://nextmail.sh/docs/php/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.discover_mailbox`

Find the mail server for an address

```ruby
discover_mailbox(email, api_key: nil) -> Hash
```

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 nil when nothing was found: ask for the host and port and pass them to `check_mailbox`. `signIn` says what the mailbox takes. `app-password` and `password` are what `check_mailbox` and `connect_mailbox` 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.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

A Hash with `object`, `email`, `provider`, one of `Nextmail::MAILBOX_PROVIDERS`, `signIn`, one of `Nextmail::MAILBOX_SIGN_INS`, `server`, `source`, `username`, and `contacts` and `calendars`, whether those can come across too. `server` is nil or a Hash with `host`, `port` and `security`, and `source` says where the server was found, one of `Nextmail::MAILBOX_SETTINGS_SOURCES` or nil.

**Example**

```ruby
settings = client.imports.discover_mailbox("ada@gmail.com")

if settings[:signIn] == "microsoft"
  puts "Connect this mailbox from the Migrations page of the app"
else
  puts settings[:provider], settings[:signIn], settings.dig(:server, :host)
end
```

**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 `max_retries`, 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); PHP [`imports->discoverMailbox`](https://nextmail.sh/docs/php/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.check_mailbox`

Check a mailbox before importing it

```ruby
check_mailbox(body = nil, api_key: nil, **fields) -> Hash
```

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 `discover_mailbox` does. Only port 993 with TLS, or port 143 with STARTTLS, is accepted. Each person may make 10 sign-in attempts an hour across `check_mailbox`, `connect_mailbox` and `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 `discover_mailbox` answers `signIn` `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` (`Integer`): 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::MAILBOX_SECURITIES`, 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 `discover_mailbox` answers, usually the address.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

A Hash with `object`, `provider`, the `server` and `username` that worked, `folders`, `messages`, the total an import would read, and `contacts` and `calendars`. `folders` holds up to 200 Hashes with `name`, `role`, one of `Nextmail::MAILBOX_FOLDER_ROLES`, `messages` and `included`, whether an import with the default options reads the folder.

**Example**

```ruby
check = client.imports.check_mailbox(email: "ada@gmail.com", password: ENV["OLD_MAILBOX_PASSWORD"])

included = check[:folders].count { |folder| folder[:included] }

puts "#{check[:messages]} messages in #{included} folders"
```

**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); PHP [`imports->checkMailbox`](https://nextmail.sh/docs/php/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.connect_mailbox`

Import a live mailbox

```ruby
connect_mailbox(body = nil, api_key: nil, **fields) -> Hash
```

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 `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 `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 as that import. 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 `discover_mailbox` answers `signIn` `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` (`Integer`): 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::MAILBOX_SECURITIES`, 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 `discover_mailbox` answers, usually the address.
- `options.keepInbox` (`Boolean`): Default true: mail that was in the old inbox lands in Inbox with its unread state. False files everything under Archive. It goes inside the `options` Hash, as do the next two.
- `options.includeSpam` (`Boolean`): Default false.
- `options.includeTrash` (`Boolean`): Default false.
- `contacts` (`Boolean`): True brings the address book across too, where the provider offers it. Needs `contacts:write`.
- `calendars` (`Boolean`): 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.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

The import Hash, shaped like the one `get` returns, with `status` set to `queued`, `source` set to `imap`, the `remote` mailbox it reads and the `children` it started.

**Example**

```ruby
item = client.imports.connect_mailbox(
  addressId: "addr_2b7e",
  email: "ada@gmail.com",
  password: ENV["OLD_MAILBOX_PASSWORD"],
  contacts: true,
  calendars: true
)

puts item[:status], item.dig(:remote, :host), item[:children].size
```

**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 `check_mailbox` 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 `get`. `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); PHP [`imports->connectMailbox`](https://nextmail.sh/docs/php/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

```ruby
resume(id, body = nil, api_key: nil, **fields) -> Hash
```

Carries on an import from a live mailbox that is waiting. One with `status` `parked` is queued again at once, without a password. One with `status` `needs-password` needs the new `password`, 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`.
- `api_key` (`String`): Overrides the client API key for this call only.

**Returns**

The import Hash, shaped like the one `get` returns, with `status` set to `queued`.

**Example**

```ruby
parked = client.imports.resume("imp_3f9c2a7b1e4d8f60a5c7b92d")

puts parked[:status]

signed_in = client.imports.resume("imp_9d2c7b5a06f8d4e1b7a2c9f3", password: ENV["OLD_MAILBOX_PASSWORD"])

puts signed_in[:status]
```

**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 `check_mailbox` and `connect_mailbox` 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); PHP [`imports->resume`](https://nextmail.sh/docs/php/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).
