emailx

package module
v0.23.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2023 License: MIT Imports: 4 Imported by: 1

README

emailx GoDoc Go Report Card Calver v0.YY.Minor

Go package for email address validation and normalization.

Forked from goware/emailx with some breaking changes to make the API more convenient.

Email validation

Simple email format check (not a complicated regexp, this is why).

import "github.com/carlmjohnson/emailx"

func main() {
    if email := "email.@example.com"; !emailx.Valid(email) {
        fmt.Printf("%q is not valid\n", email)
        // "email.@example.com" is not valid
    }
}

Email resolving

Check whether the domain has a valid DNS record:

    if err := emailx.Resolve("My+Email@wrong.example.com"); err != nil {
        fmt.Println("Email is not valid.")

        if errors.Is(err, emailx.ErrUnresolvableHost) {
            fmt.Println("Unresolvable host.")
        }
    }
    // Output:
    // Email is not valid.
    // Unresolvable host.

Email normalization

import "github.com/carlmjohnson/emailx"

func main() {
    fmt.Print(emailx.Normalize(" My+Email@example.com. "))
    // Prints my+email@example.com
}

License

Emailx is licensed under the MIT License.

Documentation

Overview

Package emailx contains helpers for email address validation and normalization.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidFormat is returned when email's format is invalid
	ErrInvalidFormat = errors.New("invalid format")
	// ErrUnresolvableHost is returned when Resolve couldn't resolve email's host
	ErrUnresolvableHost = errors.New("unresolvable host")
)

Functions

func Normalize

func Normalize(email string) string

Normalize an email address.

Example
package main

import (
	"fmt"

	"github.com/carlmjohnson/emailx"
)

func main() {
	fmt.Println(emailx.Normalize(" Email+Me@example.com. "))
}
Output:

email+me@example.com

func Resolvable added in v0.20.0

func Resolvable(email string) bool

Resolvable reports if Resolve succeeds

func Resolve added in v0.20.0

func Resolve(email string) error

Resolve checks the validity of a given address and resolves its host name.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/carlmjohnson/emailx"
)

func main() {
	if err := emailx.Resolve("My+Email@wrong.example.com"); err != nil {
		fmt.Println("Email is not valid.")

		if errors.Is(err, emailx.ErrInvalidFormat) {
			fmt.Println("Wrong format.")
		}

		if errors.Is(err, emailx.ErrUnresolvableHost) {
			fmt.Println("Unresolvable host.")
		}
	}
}
Output:

Email is not valid.
Unresolvable host.

func Split added in v0.20.0

func Split(email string) (user, host string)

Split an address into user and host portions. Split does not perform any validation or normalization.

func Valid added in v0.20.0

func Valid(email string) bool

Valid reports if Validate succeeds.

Example
package main

import (
	"fmt"

	"github.com/carlmjohnson/emailx"
)

func main() {
	if email := "email.@example.com"; !emailx.Valid(email) {
		fmt.Printf("%q is not valid\n", email)
	}
}
Output:

"email.@example.com" is not valid

func Validate

func Validate(email string) error

Validate checks format of a given email.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL