-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add function ValidateHostAndUser to validate host and user prev…
…enting SPAN and BOT
- Loading branch information
1 parent
f9f80cb
commit e32c2e0
Showing
3 changed files
with
81 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,34 +11,42 @@ | |
|
||
### 1. Format | ||
```go | ||
func main() { | ||
err := checkmail.ValidateFormat("ç$€§/[email protected]") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
func main() { | ||
err := checkmail.ValidateFormat("ç$€§/[email protected]") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
``` | ||
output: `invalid format` | ||
|
||
### 2. Domain | ||
```go | ||
func main() { | ||
err := checkmail.ValidateHost("[email protected]") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
func main() { | ||
err := checkmail.ValidateHost("[email protected]") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
``` | ||
output: `unresolvable host` | ||
|
||
### 3. User | ||
### 3. Host and User | ||
|
||
If host is valid, requires valid SMTP `serverHostName` (see to [online validator](https://mxtoolbox.com/SuperTool.aspx)) and `serverMailAddress` to reverse validation | ||
for prevent SPAN and BOTS. | ||
|
||
```go | ||
func main() { | ||
err := checkmail.ValidateHost("[email protected]") | ||
if smtpErr, ok := err.(checkmail.SmtpError); ok && err != nil { | ||
fmt.Printf("Code: %s, Msg: %s", smtpErr.Code(), smtpErr) | ||
} | ||
} | ||
func main() { | ||
var ( | ||
serverHostName = "smtp.myserver.com" | ||
serverMailAddress = "[email protected]" | ||
) | ||
err := checkmail.ValidateHostAndUser(serverHostName, serverMailAddress, "[email protected]") | ||
if smtpErr, ok := err.(checkmail.SmtpError); ok && err != nil { | ||
fmt.Printf("Code: %s, Msg: %s", smtpErr.Code(), smtpErr) | ||
} | ||
} | ||
``` | ||
output: `Code: 550, Msg: 550 5.1.1 The email account that you tried to reach does not exist.` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,31 +37,50 @@ var ( | |
emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") | ||
) | ||
|
||
|
||
func ValidateFormat(email string) error { | ||
if !emailRegexp.MatchString(email) { | ||
return ErrBadFormat | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateHost validate mail host. | ||
func ValidateHost(email string) error { | ||
_, host := split(email) | ||
mx, err := net.LookupMX(host) | ||
if err != nil { | ||
return ErrUnresolvableHost | ||
} | ||
client, err := DialTimeout(fmt.Sprintf("%s:%d", mx[0].Host, 25), forceDisconnectAfter) | ||
if err != nil { | ||
return NewSmtpError(err) | ||
} | ||
client.Close() | ||
return nil | ||
} | ||
|
||
// ValidateHostAndUser validate mail host and user. | ||
// If host is valid, requires valid SMTP [1] serverHostName and serverMailAddress to reverse validation | ||
// for prevent SPAN and BOTS. | ||
// [1] https://mxtoolbox.com/SuperTool.aspx | ||
func ValidateHostAndUser(serverHostName, serverMailAddress, email string) error { | ||
_, host := split(email) | ||
mx, err := net.LookupMX(host) | ||
if err != nil { | ||
return ErrUnresolvableHost | ||
} | ||
client, err := DialTimeout(fmt.Sprintf("%s:%d", mx[0].Host, 25), forceDisconnectAfter) | ||
if err != nil { | ||
return NewSmtpError(err) | ||
} | ||
defer client.Close() | ||
|
||
err = client.Hello("checkmail.me") | ||
err = client.Hello(serverHostName) | ||
if err != nil { | ||
return NewSmtpError(err) | ||
} | ||
err = client.Mail("[email protected]") | ||
err = client.Mail(serverMailAddress) | ||
if err != nil { | ||
return NewSmtpError(err) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters