Skip to content

Repository files navigation

go-emailvalidation

Build GoDoc Go Report Card FOSSA Status

Purpose

Simple email validation package. Package supports either fast email validation via RFC compliant regex OR slower recursive DNS lookup. DNS validation checks for valid NS & MX records using the local DNS settings.

Supported versions

The current version is v3 and requires Go 1.26 or later.

Please import via go modules github.com/cameronnewman/go-emailvalidation/v3.

Usage

package main

import (
    "context"
    "fmt"
    "time"

    email "github.com/cameronnewman/go-emailvalidation/v3"
)

func main() {

    emailAddress := "John.Snow@gmaiiiiiiillllll.com"

    // Run all checks, including validating the format along with DNS lookups which
    // may be slower depending on your DNS server performance
    err := email.Validate(emailAddress)
    if err != nil {
        fmt.Println(err)
    }

    // Same as above, with a timeout on the DNS lookups
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    err = email.ValidateContext(ctx, emailAddress)
    if err != nil {
        fmt.Println(err)
    }

    // Checks the format - this function performs no network
    // operations and is very fast
    err = email.ValidateFormat(emailAddress)
    if err != nil {
        fmt.Println(err)
    }

    // Checks domain NS & MX, along with format validation
    err = email.ValidateDomainRecords(emailAddress)
    if err != nil {
        fmt.Println(err)
    }

    // Normalize email address for storage
    address := email.Normalize(emailAddress)
    fmt.Println(address)

    // Split an email address into the username and domain parts
    username, domain := email.Split(emailAddress)
    fmt.Println(username, domain)
}

Security

See SECURITY.md for the supported versions and how to report a vulnerability. Dependencies and code are scanned in CI with Trivy and CodeQL. The Trivy scan policy is defined in trivy.yaml, and suppressed findings are tracked in .trivyignore.

License

MIT Licensed

Releases

Packages

Used by

Contributors

Languages