# Importing mail and using Microsoft 365 or Google Workspace

> Pull mail from Gmail or an old host into a Berth mailbox over IMAP through a fair per-customer queue, or move a domain's mail to Microsoft 365 or Google Workspace in one step with all DNS records set for you.

Source: https://www.berthpanel.app/library/mail-import-and-external-providers/ · Category: mail · Updated: 2026-10-29

Two situations come up all the time when a customer's mail changes hands. Either the mail is coming *to* Berth from another provider, and the existing messages should come along; or the mail is going *away* to Microsoft 365 or Google Workspace, while the website stays on Berth. Both are handled from the domain's *Mail* tab.

Moving a whole server from Plesk, cPanel or DirectAdmin is a different route: mailboxes come along with their passwords and message UIDs, without IMAP. See [Migration overview](https://www.berthpanel.app/library/migration-overview/).

## Importing a mailbox over IMAP

The import copies a mailbox from any IMAP server (Gmail, an old hosting provider, an Exchange server with IMAP enabled) into a mailbox on this server. Folders, flags and read/unread state come along. Running it again fetches only what is new, so you can import days before the switch and run it once more just before you change the MX record.

### Before you start

- Create the target mailbox first; see [Mailboxes](https://www.berthpanel.app/library/mailboxes/#creating-a-mailbox).
- **Gmail**: switch IMAP on in Gmail's settings and create an app password in the Google account (Security). Use that instead of the normal password.
- **Microsoft 365** usually no longer accepts sign-in with a plain password over IMAP, so importing from it this way often fails.

### Starting an import

1. On the domain's *Mail* tab, open *More settings → Import mail via IMAP*.
2. Under **Source**, enter the IMAP server (for example `imap.gmail.com`), the security (SSL/TLS on port 993 or STARTTLS on port 143), and the username and password at the source. Leave the port empty for the standard one.
3. Optional switches:
   - *Don't verify the source certificate*: only for an old server with a self-signed certificate.
   - *Include virtual folders*: Gmail's "All Mail" and "Starred" are copies of mail in other folders, so they are skipped unless you switch this on.
4. Under **Target**, pick the mailbox here and click **Start import**.

```bash
berth mail import --domain example.com --local info \
  --host imap.gmail.com --user info.example@gmail.com --password-file - < pw.txt
berth mail import --domain example.com              # list the domain's imports
```

The command waits until the import is done unless you pass `--wait=false`. Other flags cover `--security starttls`, `--port`, `--skip-tls-verify`, `--all-folders` and `--list-prefix` for a source that keeps everything under one folder.

### The import queue

Imports run in the background and are scheduled fairly. Each import runs in rounds; after a round, an unfinished import goes to the back of the queue, and the next turn goes to whoever has waited longest. One customer's imports take turns with each other, so a customer with a very large mailbox cannot hold up everyone else, and the server never runs more imports at once than it can carry.

The table on the card shows each import's target, source, who started it and when, and its state: waiting (with how many rounds it has done), running, done or failed. The queue survives a restart of the panel, and the result of every import is recorded in the activity log.

The source password is only used for the import. While an import waits in the queue, the job is stored encrypted; once the import is done or has failed, the password is gone. An import can never point at this server itself.

Under the hood the import uses Dovecot's own synchronisation (`doveadm sync` with the IMAP client backend), which is what makes repeated runs incremental.

## Mail at Microsoft 365 or Google Workspace

When a customer moves their mail to Microsoft 365 or Google Workspace but keeps the website on Berth, the domain needs a different set of DNS records and local mail must be off, or mail sent from the website to the domain's own addresses would stay on this server. The panel does both in one step.

1. On the domain's *Mail* tab, open **Mail at Microsoft 365 or Google Workspace**.
2. Choose the **Provider**.
3. Paste the **Verification** TXT record the provider gave you, `MS=ms…` or `google-site-verification=…`, one per line.
4. Under **MX and DKIM**:
   - **Microsoft 365**: enter the **tenant name**, the part before `.onmicrosoft.com`. The panel then adds the two DKIM CNAME records for `selector1` and `selector2`. Newer tenants sometimes show different targets in the admin centre; enter those in the two fields for your own DKIM targets.
   - **Google Workspace**: paste the DKIM key from the admin console (Apps → Google Workspace → Gmail → Authenticate email), starting with `v=DKIM1;`.
   - Leave the MX target empty for the usual value (`example-com.mail.protection.outlook.com` or `smtp.google.com`), unless the admin centre shows something else.
5. If mail is on here, confirm *Mail on this server is switched off*, then click **Mail to the provider**.

```bash
berth mail provider --domain example.com --set m365 --tenant contoso \
  --verify MS=ms12345678 --confirm
berth mail provider --domain example.com --set google \
  --dkim 'v=DKIM1; k=rsa; p=MIIB…' --verify google-site-verification=abc123 --confirm
berth mail provider --domain example.com             # show
berth mail provider --domain example.com --off       # back to this server
```

### Which records are set

| Record | Microsoft 365 | Google Workspace |
|---|---|---|
| MX | the tenant's `mail.protection.outlook.com` host | `smtp.google.com` |
| SPF | `include:spf.protection.outlook.com`, plus this server | `include:_spf.google.com`, plus this server |
| Autodiscover | CNAME to `autodiscover.outlook.com` | not needed |
| DKIM | CNAMEs for `selector1` and `selector2` | TXT on `google._domainkey` |
| Verification | the `MS=` TXT record | the `google-site-verification=` TXT record |
| DMARC | `p=none` to start measuring | `p=none` to start measuring |

SPF keeps this server in the record so that forms and notifications sent by the website still pass. A stricter DMARC policy that already exists is kept. Once the provider is sending DKIM-signed mail, switch DKIM on in its admin console.

Where the records go depends on the domain's [DNS mode](https://www.berthpanel.app/library/dns/): straight into the zone when this server runs the domain's DNS, through the API when a DNS provider is linked, or shown with copy buttons when DNS is elsewhere.

### What happens to the old mailboxes

The mailboxes and aliases leave the configuration, but their content stays on disk. Move the mail to the new provider first, for example with the provider's own IMAP migration tool pointed at this server.

To come back, use *Back to this server → No longer at …* or `--off`. The provider's records are removed, after which you can switch mail on here again and the old mailboxes reappear. Mail only arrives here once the MX points to this server again.

## MX warnings

Independently of the above, the mail page compares the domain's MX record with this server. If mail is off here and the MX points elsewhere, it advises leaving mail off. If mail is on here while the MX points elsewhere, it shows a clear warning: mail from outside would never arrive in these mailboxes.
