• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸš€ Configurable Golang πŸ“¨ email validator/verifier. Verify email via Regex, DNS, SMTP and even more. Be sure that email address valid and exists.

Truemail - configurable framework agnostic plain Go email validator

Go Report Card Codecov CircleCI GitHub release (latest by date) PkgGoDev Mentioned in Awesome Go GitHub Contributor Covenant

Configurable Golang πŸ“¨ email validator. Verify email via Regex, DNS, SMTP and even more. Be sure that email address valid and exists. It's Golang port of truemail Ruby gem. Currently ported all validation features only.

Actual and maintainable documentation πŸ“š for developers is living here.

Table of Contents

Synopsis

Email validation is a tricky thing. There are a number of different ways to validate an email address and all mechanisms must conform with the best practices and provide proper validation. The truemail-go package helps you validate emails via regex pattern, presence of DNS records, and real existence of email account on a current email server.

Syntax Checking: Checks the email addresses via regex pattern.

Mail Server Existence Check: Checks the availability of the email address domain using DNS records.

Mail Existence Check: Checks if the email address really exists and can receive email via SMTP connections and email-sending emulation techniques.

Features

  • Configurable validator, validate only what you need
  • Supporting of internationalized emails (EAI)
  • Whitelist/blacklist validation layers
  • Ability to configure different MX/SMTP validation flows
  • Ability to configure DEA validation flow
  • Simple SMTP debugger

Requirements

Golang 1.21+

Installation

Install truemail-go:

go get github.com/truemail-rb/truemail-go
go install -i github.com/truemail-rb/truemail-go

Import truemail-go dependency into your code:

package main

import "github.com/truemail-rb/truemail-go"

Usage

Configuration features

You can use global package configuration or custom independent configuration. Available configuration options:

  • verifier email
  • verifier domain
  • email pattern
  • connection timeout
  • response timeout
  • connection attempts
  • default validation type
  • validation type for domains
  • whitelisted domains
  • whitelist validation
  • blacklisted domains
  • blacklisted mx ip-addresses
  • custom DNS gateway
  • RFC MX lookup flow
  • SMTP port number
  • SMTP error body pattern
  • SMTP fail fast
  • SMTP safe check

Creating configuration

To have an access for library features, you must create configuration struct first. Please use truemail.NewConfiguration() built-in constructor to create a valid configuration as in the example below:

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  ConfigurationAttr{
    // Required parameter. Must be an existing email on behalf of which verification will be
    // performed
    VerifierEmail: "[email protected]",

    // Optional parameter. Must be an existing domain on behalf of which verification will be
    // performed. By default verifier domain based on verifier email
    VerifierDomain: "somedomain.com",

    // Optional parameter. You can override default regex pattern
    EmailPattern: `\A.+@(.+)\z`,

    // Optional parameter. You can override default regex pattern
    SmtpErrorBodyPattern: `.*(user|account).*`,

    // Optional parameter. Connection timeout in seconds.
    // It is equal to 2 by default.
    ConnectionTimeout: 1,

    // Optional parameter. A SMTP server response timeout in seconds.
    // It is equal to 2 by default.
    ResponseTimeout: 1,

    // Optional parameter. Total of connection attempts. It is equal to 2 by default.
    // This parameter uses in mx lookup timeout error and smtp request (for cases when
    // there is one mx server).
    ConnectionAttempts: 1,

    // Optional parameter. You can predefine default validation type for
    // truemail.Validate("[email protected]", configuration) call without type-parameter
    // Available validation types: "regex", "mx", "mx_blacklist", "smtp"
    ValidationTypeDefault: "mx",

    // Optional parameter. You can predefine which type of validation will be used for domains.
    // Also you can skip validation by domain.
    // Available validation types: "regex", "mx", "smtp"
    // This configuration will be used over current or default validation type parameter
    // All of validations for "somedomain.com" will be processed with regex validation only.
    // And all of validations for "otherdomain.com" will be processed with mx validation only.
    // It is equal to empty map of strings by default.
    ValidationTypeByDomain: map[string]string{"somedomain.com": "regex", "otherdomain.com": "mx"},

    // Optional parameter. Validation of email which contains whitelisted domain always will
    // return true. Other validations will not processed even if it was defined in
    // validationTypeByDomain. It is equal to empty slice of strings by default.
    WhitelistedDomains: []string{"somedomain1.com", "somedomain2.com"},

    // Optional parameter. With this option Truemail will validate email which contains whitelisted
    // domain only, i.e. if domain whitelisted, validation will passed to Regex, MX or SMTP
    // validators. Validation of email which not contains whitelisted domain always will return
    // false. It is equal false by default.
    WhitelistValidation: true,

    // Optional parameter. Validation of email which contains blacklisted domain always will
    // return false. Other validations will not processed even if it was defined in
    // validationTypeByDomain. It is equal to empty slice of strings by default.
    BlacklistedDomains: []string{"somedomain3.com", "somedomain4.com"},

    // Optional parameter. With this option Truemail will filter out unwanted mx servers via
    // predefined list of ip addresses. It can be used as a part of DEA (disposable email
    // address) validations. It is equal to empty slice of strings by default.
    BlacklistedMxIpAddresses: []string{"1.1.1.1", "2.2.2.2"},

    // Optional parameter. This option will provide to use custom DNS gateway when Truemail
    // interacts with DNS. Valid port number is in the range 1-65535. If you won't specify
    // nameserver port Truemail will use default DNS TCP/UDP port 53. It means that you can
    // use ip4 addres as DNS gateway, for example "10.0.0.1". By default Truemail uses
    // DNS gateway from system settings and this option is equal to empty string.
    Dns: "10.0.0.1:5300",

    // Optional parameter. This option will provide to use not RFC MX lookup flow.
    // It means that MX and Null MX records will be cheked on the DNS validation layer only.
    // By default this option is disabled and equal to false.
    NotRfcMxLookupFlow: true,

    // Optional parameter. SMTP port number. It is equal to 25 by default.
    // This parameter uses for SMTP session in SMTP validation layer.
    SmtpPort: 2525,

    // Optional parameter. This option will provide to use smtp fail fast behavior. When
    // smtpFailFast = true it means that Truemail ends smtp validation session after first
    // attempt on the first mx server in any fail cases (network connection/timeout error,
    // smtp validation error). This feature helps to reduce total time of SMTP validation
    // session up to 1 second. By default this option is disabled and equal to false.
    SmtpFailFast: true,

    // Optional parameter. This option will be parse bodies of SMTP errors. It will be helpful
    // if SMTP server does not return an exact answer that the email does not exist
    // By default this option is disabled, available for SMTP validation only.
    SmtpSafeCheck: true,
  },
)

Using configuration

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(truemail.ConfigurationAttr{VerifierEmail: "[email protected]"})

truemail.Validate("[email protected]", configuration)
truemail.IsValid("[email protected]", configuration, "regex")

Validation features

Whitelist/Blacklist check

Whitelist/Blacklist check is zero validation level. You can define white and black list domains. It means that validation of email which contains whitelisted domain always will return true, and for blacklisted domain will return false.

Please note, other validations will not processed even if it was defined in ValidationTypeByDomain.

Sequence of domain list check:

  1. Whitelist check
  2. Whitelist validation check
  3. Blacklist check

Example of usage:

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    WhitelistedDomains: []string{"white-domain.com", "somedomain.com"},
    BlacklistedDomains: []string{"black-domain.com", "somedomain.com"},
    ValidationTypeByDomain: map[string]string{"somedomain.com": "mx"},
  },
)
Whitelist case

When email in whitelist, validation type will be redefined. Validation result returns true

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    WhitelistedDomains: []string{"white-domain.com", "somedomain.com"},
    BlacklistedDomains: []string{"black-domain.com", "somedomain.com"},
    ValidationTypeByDomain: map[string]string{"somedomain.com": "mx"},
  },
)

truemail.Validate("[email protected]", configuration) // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration) // returns true
Whitelist validation case

When email domain in whitelist and WhitelistValidation is sets equal to true validation type will be passed to other validators. Validation of email which not contains whitelisted domain always will return false.

Email has whitelisted domain
import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    WhitelistedDomains: []string{"white-domain.com"},
    WhitelistValidation: true,
  },
)

truemail.Validate("[email protected]", configuration, "regex") // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration, "regex") // returns true
Email hasn't whitelisted domain
import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    WhitelistedDomains: []string{"white-domain.com"},
    WhitelistValidation: true,
  },
)

truemail.Validate("[email protected]", configuration, "regex") // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration, "regex") // returns false
Blacklist case

When email in blacklist, validation type will be redefined too. Validation result returns false.

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    WhitelistedDomains: []string{"white-domain.com", "somedomain.com"},
    BlacklistedDomains: []string{"black-domain.com", "somedomain.com"},
    ValidationTypeByDomain: map[string]string{"somedomain.com": "mx"},
  },
)

truemail.Validate("[email protected]", configuration) // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration) // returns false
Duplication case

Validation result for this email returns true, because it was found in whitelisted domains list first. Also ValidatorResult.ValidationType for this case will be redefined.

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    WhitelistedDomains: []string{"white-domain.com", "somedomain.com"},
    BlacklistedDomains: []string{"black-domain.com", "somedomain.com"},
    ValidationTypeByDomain: map[string]string{"somedomain.com": "mx"},
  },
)

truemail.Validate("[email protected]", configuration) // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration) // returns true

Regex validation

Validation with regex pattern is the first validation level. It uses whitelist/blacklist check before running itself.

[Whitelist/Blacklist] -> [Regex validation]

By default this validation not performs strictly following RFC 5322 standard, so you can override truemail default regex pattern if you want.

Example of usage:

With default regex pattern
import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
  },
)

truemail.Validate("[email protected]", configuration, "regex") // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration, "regex") // returns true
With custom regex pattern

You should define your custom regex pattern in a gem configuration before.

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    EmailPattern: `\A(.+)@(.+)\z`,
  },
)

truemail.Validate("[email protected]", configuration, "regex") // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration, "regex") // returns true
truemail.IsValid("not_email", configuration, "regex") // returns false

MX validation

In fact it's DNS validation because it checks not MX records only. DNS validation is the second validation level, historically named as MX validation. It uses Regex validation before running itself. When regex validation has completed successfully then runs itself.

[Whitelist/Blacklist] -> [Regex validation] -> [MX validation]

Please note, truemail MX validator not performs strict compliance of the RFC 5321 standard for best validation outcome.

RFC MX lookup flow

Truemail MX lookup based on RFC 5321. It consists of 3 substeps: MX, CNAME and A record resolvers. The point of each resolver is attempt to extract the mail servers from email domain. If at least one server exists that validation is successful. Iteration is processing until resolver returns true.

Example of usage:

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
  },
)

truemail.Validate("[email protected]", configuration, "mx") // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration, "mx") // returns bool
Not RFC MX lookup flow

Also Truemail has possibility to use not RFC MX lookup flow. It means that will be used only one MX resolver on the DNS validation layer. By default this option is disabled.

Example of usage:

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    NotRfcMxLookupFlow: true,
  },
)

truemail.Validate("[email protected]", configuration, "mx") // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration, "mx") // returns bool

MX blacklist validation

MX blacklist validation is the third validation level. This layer provides checking extracted mail server(s) IP address from MX validation with predefined blacklisted IP addresses list. It can be used as a part of DEA (disposable email address) validations.

[Whitelist/Blacklist] -> [Regex validation] -> [MX validation] -> [MX blacklist validation]

Example of usage:

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    BlacklistedMxIpAddresses: []string{"127.0.1.2", "127.0.1.3"},
  },
)

truemail.Validate("[email protected]", configuration, "mx_blacklist") // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration, "mx_blacklist") // returns bool

SMTP validation

SMTP validation is a final, fourth validation level. This type of validation tries to check real existence of email account on a current email server. This validation runs a chain of previous validations and if they're complete successfully then runs itself.

[Whitelist/Blacklist] -> [Regex validation] -> [MX validation] -> [MX blacklist validation] -> [SMTP validation]

If total count of MX servers is equal to one, truemail SMTP validator will use value from ConnectionAttempts as connection attempts. By default it's equal 2.

By default, you don't need pass with-parameter to use it. Example of usage is specified below:

SMTP fail fast enabled

Truemail can use fail fast behavior for SMTP validation layer. When SmtpFailFast = true it means that truemail ends smtp validation session after first attempt on the first mx server in any fail cases (network connection/timeout error, smtp validation error). This feature helps to reduce total time of SMTP validation session up to 1 second.

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    SmtpFailFast: true,
  },
)

truemail.Validate("[email protected]", configuration) // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration) // returns bool
SMTP safe check disabled

When this feature disabled, it means that SMTP validation will be failed when it consists at least one smtp error.

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
  },
)

truemail.Validate("[email protected]", configuration) // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration) // returns bool
SMTP safe check enabled

When this feature enabled, it means that SMTP validation will be successful for all cases until truemail SMTP validator receive RCPT TO error that matches to SmtpErrorBodyPattern, specified in configuration.

import "github.com/truemail-rb/truemail-go"

configuration := truemail.NewConfiguration(
  truemail.ConfigurationAttr{
    VerifierEmail: "[email protected]",
    SmtpSafeCheck: true,
  },
)

truemail.Validate("[email protected]", configuration) // returns pointer to ValidatorResult with validation details and error
truemail.IsValid("[email protected]", configuration) // returns bool

Truemail helpers

.IsValid()

You can use the .IsValid() helper for quick validation of email address. It returns a boolean:

truemail.IsValid("[email protected]", configuration)

Truemail family

All Truemail solutions: https://truemail-rb.org

Name Type Description
truemail ruby gem Configurable framework agnostic plain Ruby email validator, main core
truemail server ruby app Lightweight rack based web API wrapper for Truemail gem
truemail-rack-docker docker image Lightweight rack based web API dockerized image 🐳 of Truemail server
truemail-ruby-client ruby gem Web API Ruby client for Truemail Server
truemail-crystal-client crystal shard Web API Crystal client for Truemail Server
truemail-java-client java lib Web API Java client for Truemail Server
truemail-rspec ruby gem Truemail configuration, auditor and validator RSpec helpers

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/truemail-rb/truemail-go. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct. Please check the open tickets. Be sure to follow Contributor Code of Conduct below and our Contributing Guidelines.

License

The package is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Truemail project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Credits

Versioning

Truemail uses Semantic Versioning 2.0.0