Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type Mx struct {

// CheckMX will return the DNS MX records for the given domain name sorted by preference.
func (v *Verifier) CheckMX(domain string) (*Mx, error) {
if !v.mxCheckEnabled {
return &Mx{
HasMXRecord: false,
}, nil
}

domain = domainToASCII(domain)
mx, err := net.LookupMX(domain)
if err != nil && len(mx) == 0 {
Expand Down
15 changes: 15 additions & 0 deletions verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// Verifier is an email verifier. Create one by calling NewVerifier
type Verifier struct {
mxCheckEnabled bool // MX record check enabled or disabled (enabled by default)
smtpCheckEnabled bool // SMTP check enabled or disabled (disabled by default)
catchAllCheckEnabled bool // SMTP catchAll check enabled or disabled (enabled by default)
domainSuggestEnabled bool // whether suggest a most similar correct domain or not (disabled by default)
Expand Down Expand Up @@ -50,6 +51,7 @@ func init() {
// NewVerifier creates a new email verifier
func NewVerifier() *Verifier {
return &Verifier{
mxCheckEnabled: true,
fromEmail: defaultFromEmail,
helloName: defaultHelloName,
catchAllCheckEnabled: true,
Expand Down Expand Up @@ -167,6 +169,19 @@ func (v *Verifier) DisableSMTPCheck() *Verifier {
return v
}

// EnableMXCheck enables MX record check by dns,
// we check MX record by default
func (v *Verifier) EnableMXCheck() *Verifier {
v.mxCheckEnabled = true
return v
}

// DisableMXCheck disables MX record check by dns

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth documenting the scope here: this flag can't skip the MX lookup when the SMTP check is
on. newSMTPClient does its own net.LookupMX and needs the result to know where to
connect, with no A-record fallback:

email-verifier/smtp.go

Lines 116 to 124 in fee1dc6

func newSMTPClient(domain, proxyURI string, connectTimeout, operationTimeout time.Duration) (*smtp.Client, *net.MX, error) {
domain = domainToASCII(domain)
mxRecords, err := net.LookupMX(domain)
if err != nil {
return nil, nil, err
}
if len(mxRecords) == 0 {
return nil, nil, errors.New("No MX records found")

So DisableMXCheck().EnableSMTPCheck() still pays for the DNS round trip, and reports
has_mx_records: false next to a possibly-true smtp.host_exists. That's inherent rather
than fixable, but saying so here would save the next reader a trip through smtp.go:

Suggested change
// DisableMXCheck disables MX record check by dns
// DisableMXCheck disables the standalone MX record check performed by Verify.
// Note this does not prevent the MX lookup inside the SMTP check, which needs
// MX records to locate a mail server.

func (v *Verifier) DisableMXCheck() *Verifier {
v.mxCheckEnabled = false
return v
}

// EnableCatchAllCheck enables catchAll check by smtp
// for most ISPs block outgoing catchAll requests through port 25, to prevent spam,
// we don't check catchAll by default
Expand Down
29 changes: 29 additions & 0 deletions verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,35 @@ func TestCheckEmail_DisabledSMTPCheck(t *testing.T) {
assert.Equal(t, &expected, ret)
}

func TestCheckEmail_DisabledMXCheck(t *testing.T) {
var (
// trueVal = true
username = "email_username"
domain = "randomain.com"
address = username + "@" + domain
email = address
)

verifier := NewVerifier().DisableMXCheck()
ret, err := verifier.Verify(email)
expected := Result{
Email: email,
Syntax: Syntax{
Username: username,
Domain: domain,
Valid: true,
},
HasMxRecords: false,
Disposable: false,
RoleAccount: false,
Reachable: reachableUnknown,
Free: false,
SMTP: nil,
}
assert.NoError(t, err)
assert.Equal(t, &expected, ret)
}

func TestNewVerifierOK_AutoUpdateDisposable(t *testing.T) {
verifier.EnableAutoUpdateDisposable()
}
Expand Down