> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/pocketbase/pocketbase/llms.txt
> Use this file to discover all available pages before exploring further.

# Email field

> Store and validate email addresses with optional domain restrictions

The email field stores a single email address string and validates it against the standard email format. You can optionally restrict which domains are allowed or blocked.

## Configuration options

<ParamField path="exceptDomains" type="[]string">
  List of domains that are NOT allowed. Email addresses from these domains will fail validation. Cannot be set if `onlyDomains` is specified.
</ParamField>

<ParamField path="onlyDomains" type="[]string">
  List of domains that are exclusively allowed. Email addresses must be from one of these domains. Cannot be set if `exceptDomains` is specified.
</ParamField>

<ParamField path="required" type="bool" default="false">
  When true, requires the field value to be a non-empty, valid email address.
</ParamField>

## Validation rules

The email field validates:

* **Format**: Value must be a valid email address format
* **Domain allowlist**: If `onlyDomains` is set, email domain must be in the list
* **Domain blocklist**: If `exceptDomains` is set, email domain must NOT be in the list
* **Required**: If enabled, value cannot be empty

<Warning>
  You cannot set both `exceptDomains` and `onlyDomains` at the same time. Choose one approach based on your use case.
</Warning>

## Go examples

<Tabs>
  <Tab title="Basic usage">
    ```go theme={null}
    import "github.com/pocketbase/pocketbase/core"

    field := &core.EmailField{
        Name:     "email",
        Required: true,
    }

    collection.Fields.Add(field)

    // Set field value
    record.Set("email", "user@example.com")
    ```
  </Tab>

  <Tab title="Restrict to specific domains">
    ```go theme={null}
    field := &core.EmailField{
        Name:        "workEmail",
        Required:    true,
        OnlyDomains: []string{"company.com", "subsidiary.com"},
    }

    collection.Fields.Add(field)

    // This will validate successfully
    record.Set("workEmail", "john@company.com")

    // This will fail validation
    record.Set("workEmail", "john@gmail.com")
    ```
  </Tab>

  <Tab title="Block specific domains">
    ```go theme={null}
    field := &core.EmailField{
        Name:          "email",
        Required:      true,
        ExceptDomains: []string{"tempmail.com", "throwaway.email"},
    }

    collection.Fields.Add(field)

    // This will validate successfully
    record.Set("email", "user@gmail.com")

    // This will fail validation
    record.Set("email", "user@tempmail.com")
    ```
  </Tab>

  <Tab title="Multiple email fields">
    ```go theme={null}
    // Primary email
    collection.Fields.Add(&core.EmailField{
        Name:     "email",
        Required: true,
    })

    // Optional backup email
    collection.Fields.Add(&core.EmailField{
        Name:     "backupEmail",
        Required: false,
    })

    record.Set("email", "primary@example.com")
    record.Set("backupEmail", "backup@example.com")
    ```
  </Tab>
</Tabs>

## Database column type

```sql theme={null}
TEXT DEFAULT '' NOT NULL
```

## Common use cases

<CodeGroup>
  ```go Corporate email only theme={null}
  field := &core.EmailField{
      Name:        "corporateEmail",
      Required:    true,
      OnlyDomains: []string{"acmecorp.com"},
  }
  ```

  ```go Block disposable emails theme={null}
  field := &core.EmailField{
      Name:     "email",
      Required: true,
      ExceptDomains: []string{
          "guerrillamail.com",
          "10minutemail.com",
          "mailinator.com",
      },
  }
  ```

  ```go Educational institutions theme={null}
  field := &core.EmailField{
      Name:        "studentEmail",
      Required:    true,
      OnlyDomains: []string{"university.edu", "college.edu"},
  }
  ```
</CodeGroup>

## Best practices

<Note>
  * Use `onlyDomains` when you want to restrict to specific organizations or institutions
  * Use `exceptDomains` to block temporary/disposable email services
  * Email validation is case-insensitive for the domain part
  * Consider email verification workflows in addition to format validation
  * Store emails in lowercase for consistency in queries
</Note>

## Zero value

The zero value for email fields is an empty string `""`.
