Email Validation and Sanitization Module
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.
To install this module, use the following go get
command:
go get github.com/dmitrymomot/email
First, import the module in your Go code:
import (
"github.com/dmitrymomot/email"
)
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")
}
}
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)
}
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")
}
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.
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes.
- Commit your changes with a descriptive commit message.
- Push your changes to your fork.
- Open a pull request to the main repository.
This module is licensed under the Apache 2.0 License. See the LICENSE file for more details.