Skip to content

Instantly share code, notes, and snippets.

@podanypepa
Last active February 8, 2021 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podanypepa/8353fd5c0e96d7768294b8c628e960c4 to your computer and use it in GitHub Desktop.
Save podanypepa/8353fd5c0e96d7768294b8c628e960c4 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"regexp"
"strings"
)
var emailRegex = 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 isEmailValid(email string) bool {
if len(email) < 3 || len(email) > 254 {
return false
}
if !emailRegex.MatchString(email) {
return false
}
parts := strings.Split(email, "@")
mx, err := net.LookupMX(parts[1])
if err != nil || len(mx) == 0 {
return false
}
return true
}
func main() {
if e := "test@gophers1.cz"; !isEmailValid(e) {
fmt.Println(e + " isn't a valid email")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment