Skip to content

Commit

Permalink
Merge pull request #14 from shuler/master
Browse files Browse the repository at this point in the history
Improve disconnect from SMTP server after 10 seconds
  • Loading branch information
badoux committed Nov 22, 2018
2 parents c8e7ea8 + 1019f05 commit 2c0de5c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion checkmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func ValidateHost(email string) error {
return ErrUnresolvableHost
}

client, err := smtp.Dial(fmt.Sprintf("%s:%d", mx[0].Host, 25))
client, err := DialTimeout(fmt.Sprintf("%s:%d", mx[0].Host, 25), forceDisconnectAfter)
if err != nil {
return NewSmtpError(err)
}
Expand All @@ -75,6 +75,21 @@ func ValidateHost(email string) error {
return nil
}

// DialTimeout returns a new Client connected to an SMTP server at addr.
// The addr must include a port, as in "mail.example.com:smtp".
func DialTimeout(addr string, timeout time.Duration) (*smtp.Client, error) {
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}

t := time.AfterFunc(timeout, func() { conn.Close() })
defer t.Stop()

host, _, _ := net.SplitHostPort(addr)
return smtp.NewClient(conn, host)
}

func split(email string) (account, host string) {
i := strings.LastIndexByte(email, '@')
account = email[:i]
Expand Down

0 comments on commit 2c0de5c

Please sign in to comment.