forked from matoous/gosmtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
48 lines (43 loc) · 1.03 KB
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package gosmtp
import (
"bytes"
"fmt"
"net"
"net/mail"
"strings"
)
func parseAddress(src string) (*mail.Address, error) {
addr, err := mail.ParseAddress(src)
if err != nil {
return nil, fmt.Errorf("malformed e-mail address: %s", src)
}
return addr, nil
}
func hostname(addr *mail.Address) string {
// if the mail address didn't have @ it would be caught by parseAddress
return string(bytes.Split([]byte(addr.Address), []byte{'@'})[1])
}
// IsFQN checks if email host is full qualified name (MX or A record)
func IsFQN(addr *mail.Address) string {
ok, err := fqn(hostname(addr))
if err != nil {
return Codes.ErrorUnableToResolveHost
} else if !ok {
return Codes.FailUnqalifiedHostName
}
return ""
}
// isFQN checks if domain is FQN (MX or A record) and caches the result
// TODO refactor
func fqn(host string) (bool, error) {
_, err := net.LookupMX(host)
if err != nil {
if strings.HasSuffix(err.Error(), "no such host") {
_, err = net.LookupHost(host)
if err != nil {
return false, err
}
}
}
return true, nil
}