Skip to content

Helpers collection to parse, sanitize, and validate email address

License

Notifications You must be signed in to change notification settings

dmitrymomot/email

Repository files navigation

email

GitHub tag (latest SemVer) Go Reference License

Tests CodeQL Analysis GolangCI Lint Go Report Card

Email Validation and Sanitization Module

Summary

This Go module provides comprehensive utilities for validating and sanitizing email addresses. It includes functions to validate email formats, check domain blacklists, sanitize email addresses, and more. The module is designed to be flexible and extendable, allowing you to add custom validators and sanitizers as needed.

Installation

To install this module, use the following go get command:

go get github.com/dmitrymomot/email

Usage

Importing the Module

First, import the module in your Go code:

import (
    "github.com/dmitrymomot/email"
)

Validating Email Addresses

You can validate email addresses using the ValidateEmail function. This function accepts the email address and a list of custom validators.

package main

import (
    "fmt"
    "github.com/dmitrymomot/email"
)

func main() {
    emailAddr := "test@example.com"
    err := email.ValidateEmail(emailAddr, email.ValidateUsernameFormat, email.ValidateIcanSuffix, email.IsAddressBlacklisted)
    if err != nil {
        fmt.Println("Invalid email:", err)
    } else {
        fmt.Println("Valid email")
    }
}

Sanitizing Email Addresses

You can sanitize email addresses using the SanitizeEmail function. This function accepts the email address and a list of custom sanitizers.

package main

import (
    "fmt"
    "github.com/dmitrymomot/email"
)

func main() {
    emailAddr := " Test+spam@example.com "
    sanitizedEmail := email.SanitizeEmail(emailAddr, email.TrimSpace, email.ToLower, email.RemoveAfterPlus)
    fmt.Println("Sanitized email:", sanitizedEmail)
}

Managing Blacklisted Domains

You can manage blacklisted domains using the provided functions:

package main

import (
    "fmt"
    "github.com/dmitrymomot/email"
)

func main() {
    // Add a domain to the blacklist
    email.AddBlacklist("example.com")

    // Check if a domain is blacklisted
    isBlacklisted := email.IsDomainBlacklisted("example.com")
    fmt.Println("Is example.com blacklisted?", isBlacklisted)

    // Remove a domain from the blacklist
    email.RemoveBlacklist("example.com")
}

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you want to contribute code, please fork the repository and submit a pull request.

Steps to Contribute

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Make your changes.
  4. Commit your changes with a descriptive commit message.
  5. Push your changes to your fork.
  6. Open a pull request to the main repository.

License

This module is licensed under the Apache 2.0 License. See the LICENSE file for more details.

About

Helpers collection to parse, sanitize, and validate email address

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages