本文整理汇总了Golang中net.LookupMX函数的典型用法代码示例。如果您正苦于以下问题:Golang LookupMX函数的具体用法?Golang LookupMX怎么用?Golang LookupMX使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LookupMX函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DomainGetMX
func DomainGetMX(domain string) ([]*net.MX, error) {
var (
record []*net.MX
err error
)
if Config.DnsCache {
mx.Lock()
defer mx.Unlock()
if _, ok := mx.stor[domain]; !ok || time.Since(mx.stor[domain].update) > 15*time.Minute {
record, err = net.LookupMX(domain)
if err == nil {
mx.stor[domain] = mxStor{
records: record,
update: time.Now(),
}
} else if _, ok := mx.stor[domain]; ok {
record = mx.stor[domain].records
}
} else {
record = mx.stor[domain].records
}
} else {
record, err = net.LookupMX(domain)
}
return record, err
}
示例2: Send
func (e *Email) Send() error {
var err error
var servers = make([]string, 0)
mailTokens := strings.Split(inboxAddress, "@")
domain := mailTokens[len(mailTokens)-1]
mxServers, err := net.LookupMX(domain)
if err != nil {
return err
}
for _, server := range mxServers {
servers = append(servers, fmt.Sprintf("%s:25", strings.TrimRight(server.Host, ".")))
}
for _, server := range servers {
msg, err := e.ConstructMessage()
if err == nil {
log.Printf("Attempting send to: %s, smtp_from: %s, rcpt_to: %s, message: %s\n", server, outboundSender, inboxAddress, string(msg))
err = smtp.SendMail(
server,
nil,
outboundSender,
[]string{inboxAddress},
msg,
)
if err == nil {
break
} else {
log.Printf("Received error from mx server: %s\n", err.Error())
}
}
}
return err
}
示例3: mxLookUp
// Looks up the SMTP server for an email host
// For example, mxLookUp("gmail.com") returns "gmail-smtp-in.l.google.com"
func mxLookUp(host string) (string, error) {
cachedServer := mxCache[host]
if cachedServer != "" {
return cachedServer, nil
}
log.Printf("looking up smtp server (MX record) for %s\n", host)
mxs, err := net.LookupMX(host)
if err != nil {
log.Printf("lookup failed for %s\n", host)
return "", err
}
bestServer, bestPref := "", 1<<16
for _, mx := range mxs {
if int(mx.Pref) < bestPref {
bestPref = int(mx.Pref)
bestServer = mx.Host
}
}
// trailing dot means full domain name
if strings.HasSuffix(bestServer, ".") {
bestServer = bestServer[:len(bestServer)-1]
} else {
bestServer = bestServer + "." + host
}
mxCache[host] = bestServer
return bestServer, nil
}
示例4: VerifyEmail
// VerifyEmail checks an email by using SMTP Callback Verification method
//
// Reference:
// http://en.wikipedia.org/wiki/Callback_verification
func VerifyEmail(email string) (isValid bool, err error) {
mx, err := net.LookupMX(parseDomain(email))
if err != nil {
return
}
c, err := smtp.Dial(mx[0].Host + ":25")
if err != nil {
return
}
err = c.Hello("verify-email.org")
if err != nil {
return
}
err = c.Mail("[email protected]")
if err != nil {
return
}
if err := c.Rcpt(email); err == nil {
isValid = true
}
err = c.Quit()
return
}
示例5: Validate
// Validate checks format of a given email and resolves its host name.
func Validate(email string) error {
if len(email) < 6 || len(email) > 254 {
return ErrInvalidFormat
}
at := strings.LastIndex(email, "@")
if at <= 0 || at > len(email)-3 {
return ErrInvalidFormat
}
user := email[:at]
host := email[at+1:]
if len(user) > 64 {
return ErrInvalidFormat
}
if !userRegexp.MatchString(user) || !hostRegexp.MatchString(host) {
return ErrInvalidFormat
}
if _, err := net.LookupMX(host); err != nil {
if _, err := net.LookupIP(host); err != nil {
// Only fail if both MX and A records are missing - any of the
// two is enough for an email to be deliverable
return ErrUnresolvableHost
}
}
return nil
}
示例6: sendMail
func sendMail(file string, ss *Settings) {
env := loadEnvelope(file, ss)
if env == nil {
return
}
defer env.flush(true)
msg, err := os.Open(env.content)
if err != nil {
env.recErr("", err.Error(), false)
return
}
defer msg.Close()
var mxs []string
if len(ss.Gateways) > 0 {
mxs = ss.Gateways
} else {
mxrs, err := net.LookupMX(env.domain)
if err != nil {
env.recErr("", err.Error(), true)
return
}
mxs = make([]string, 0, len(mxrs))
for _, mxr := range mxrs {
mxs = append(mxs, mxr.Host)
}
}
for _, mx := range mxs {
if len(env.Recipients) == 0 {
break
}
msg.Seek(0, 0)
send(mx, env, msg)
}
}
示例7: lookupMX
func lookupMX(email string) ([]*net.MX, error) {
tokens := strings.Split(email, "@")
if len(tokens) != 2 {
return nil, fmt.Errorf("invalid email address: %v", email)
}
return net.LookupMX(tokens[1])
}
示例8: getMXForSender
func getMXForSender(sender *mail.Address) ([]*net.MX, error) {
domain, err := getDomain(sender)
if err != nil {
return nil, fmt.Errorf("could not get domain for sender: %v", err)
}
mxs, err := net.LookupMX(domain)
if err != nil {
return nil, fmt.Errorf("failed to lookup MX for domain (%s): %v", domain, err)
}
return mxs, nil
}
示例9: findMailServers
// Attempt to find the mail servers for the specified host. MX records are
// checked first. If one or more were found, the records are converted into an
// array of strings (sorted by priority). If none were found, the original host
// is returned.
func (h *Host) findMailServers(host string) []string {
r, err := net.LookupMX(host)
if err != nil {
return []string{host}
}
servers := make([]string, len(r))
for i, r := range r {
servers[i] = strings.TrimSuffix(r.Host, ".")
}
return servers
}
示例10: Prepare
//Prepare prepares the sending (gets MX records if no hostport is given)
func (o *EmailOutput) Prepare() error {
if o.hostport == "" {
var (
i int
ok bool
host string
err error
tos []string
mxs []*net.MX
)
o.byHost = make(map[string][]string, len(o.To))
for _, tos := range o.To {
i = strings.Index(tos, "@")
host = tos[i+1:]
o.byHost[host] = append(o.byHost[host], tos)
}
for host, tos = range o.byHost {
mxAddrsLock.Lock()
if mxs, ok = mxAddrs[host]; !ok {
if mxs, err = net.LookupMX(host); err != nil {
return fmt.Errorf("error looking up MX record for %s: %s", host, err)
}
mxAddrs[host] = mxs
}
mxAddrsLock.Unlock()
ok = false
for _, mx := range mxs {
log.Printf("test sending with %s to %s", mx.Host, tos)
err = testMail(mx.Host+":25", nil, o.From, tos, 10*time.Second,
o.tlsConfig)
log.Printf("test send with %s to %s result: %s", mx.Host, tos, err)
if err == nil {
ok = true
break
}
}
if !ok {
return fmt.Errorf("error test sending mail from %s to %s with %s: %s",
o.From, tos, mxs, err)
}
}
return nil
}
o.byHost = make(map[string][]string, 1)
log.Printf("test sending with %s to %s", o.hostport, o.To)
err := testMail(o.hostport, o.auth, o.From, o.To, 10*time.Second, o.tlsConfig)
log.Printf("test send with %s to %s result: %s", o.hostport, o.To, err)
if err == nil {
o.byHost[""] = o.To
}
return err
}
示例11: mx
func (d Domain) mx() string {
addrs, err := net.LookupMX(d.domain)
if err != nil {
return "\nMX records\n" +
"--\n" + err.Error()
}
records := "\nMX records\n" +
"--\n"
for _, val := range addrs {
records += val.Host + "\n"
}
return records
}
示例12: findMDA
// Find Mail Delivery Agent based on DNS MX record
func findMDA(host string) (string, error) {
results, err := net.LookupMX(host)
if err != nil {
return "", err
}
if len(results) == 0 {
return "", errors.New("No MX records found")
}
// todo: support for multiple MX records
return results[0].Host, nil
}
示例13: procMsg
func procMsg(msg ClientMessage, datasource *datasource.DataSource) {
rcpt := make([]string, 0, 100)
toHost := strings.Split(msg.To, "@")[1]
if isAllowedHost(toHost) {
user, err := datasource.UserByEmail(msg.To)
if err == nil {
rcpt = append(rcpt, user.InboxAddr)
} else {
group, err := datasource.GroupByEmail(msg.To)
if err == nil {
for _, member := range group.Members {
user, err = datasource.UserByEmail(member)
if err == nil {
rcpt = append(rcpt, user.InboxAddr)
}
}
} else {
logln(1, "Can't find such user or group")
return
}
}
}
logln(1, fmt.Sprintf("%s", msg.From))
fromHost := strings.Split(msg.From, "@")[1]
if isAllowedHost(fromHost) {
if msg.Auth == false || msg.Username != msg.From {
logln(1, fmt.Sprintf("%s %s", msg.Username, msg.From))
logln(1, "Not authenticated")
return
}
}
for _, email := range rcpt {
logln(1, email)
host := strings.Split(email, "@")[1]
nss, err := net.LookupMX(host)
if err == nil {
for _, ns := range nss {
logln(1, fmt.Sprintf("%s %d", ns.Host, ns.Pref))
}
curMsg := msg
curMsg.To = email
sendMsg(nss[0], curMsg)
} else {
logln(1, "Error in lookup MX")
}
}
}
示例14: mx
func mx(host string) (data []byte, err error) {
mxs, err := net.LookupMX(host)
if err != nil {
err = os.ErrNotExist
return
}
buf := bytes.NewBuffer(make([]byte, 0, len(mxs)*20))
for _, mx := range mxs {
buf.WriteString(fmt.Sprintf("%d %s\n", mx.Pref, mx.Host))
}
data = buf.Bytes()
return
}
示例15: lookupMailServer
func lookupMailServer(domain string, errorCount int) (string, error) {
mxList, err := net.LookupMX(domain)
if err != nil {
return "", err
}
mx, err := getRoundElement(mxList, errorCount)
if err != nil {
return "", err
}
if len(mx.Host) == 0 {
return "", errors.New(fmt.Sprintf("incorrect MX record for domain %s - %v;", domain, mx))
}
return mx.Host[:len(mx.Host)-1] + ":25", nil
}