|
| 1 | +package mailify |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// GetMailServers retrieves the mail servers (MX records) for a given domain. |
| 12 | +// It uses a custom DNS resolver that queries Google's public DNS server (8.8.8.8). |
| 13 | + |
| 14 | +// Parameters: |
| 15 | +// - domain: The domain name for which to look up MX records. |
| 16 | + |
| 17 | +// Returns: |
| 18 | +// - A slice of strings containing the mail server hostnames. |
| 19 | +// - An error if there was an issue looking up the MX records. |
| 20 | + |
| 21 | +// Example: |
| 22 | +// mailServers, err := GetMailServers("example.com") |
| 23 | +// if err != nil { |
| 24 | +// log.Fatalf("Failed to get mail servers: %v", err) |
| 25 | +// } |
| 26 | +// fmt.Println("Mail servers:", mailServers) |
| 27 | + |
| 28 | +func(c *Client) GetMailServers(domain string) ([]string, error) { |
| 29 | + // Use custom DNS resolver to query Google's public DNS server |
| 30 | + |
| 31 | + resolver := net.Resolver{ |
| 32 | + PreferGo: true, |
| 33 | + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { |
| 34 | + d := net.Dialer{} |
| 35 | + return d.DialContext(ctx, network, "8.8.8.8:53") // Use Google DNS |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + // Lookup MX records for the domain |
| 40 | + mx, err := resolver.LookupMX(context.Background(), domain) |
| 41 | + // mx, err := net.LookupMX(domain) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("error looking up MX records: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Extract mail server hostnames |
| 47 | + var mailServers []string |
| 48 | + for _, record := range mx { |
| 49 | + mailServers = append(mailServers, strings.TrimSuffix(record.Host, ".")) |
| 50 | + } |
| 51 | + |
| 52 | + // Print mail servers |
| 53 | + // fmt.Printf("Found mail servers for %s: %v\n", domain, mailServers) |
| 54 | + return mailServers, nil |
| 55 | +} |
| 56 | + |
| 57 | +// GetSMTPServer attempts to find an available SMTP server for the given mail server. |
| 58 | +// It performs a DNS lookup to get all IP addresses (both IPv4 and IPv6) associated with the mail server, |
| 59 | +// and then tries to connect to common SMTP ports (587, 25, 465) on each IP address. |
| 60 | +// |
| 61 | +// If a connection is successfully established, it returns the SMTP server details including |
| 62 | +// the server name, port, protocol, and IP address. If no available SMTP servers are found, |
| 63 | +// it returns an error. |
| 64 | +// |
| 65 | +// Parameters: |
| 66 | +// - mailServer: The domain name of the mail server to look up. |
| 67 | +// |
| 68 | +// Returns: |
| 69 | +// - *SMTPDetails: A struct containing the details of the SMTP server if found. |
| 70 | +// - error: An error if no available SMTP servers are found or if there is a lookup failure. |
| 71 | +func(c *Client) GetSMTPServer(mailServer string) (*SMTPDetails, error) { |
| 72 | + // Get all IPs (both IPv4 and IPv6) |
| 73 | + ips, err := net.LookupIP(mailServer) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to lookup IP for %s: %v", mailServer, err) |
| 76 | + } |
| 77 | + |
| 78 | + // Try each IP address |
| 79 | + for _, ip := range ips { |
| 80 | + // Try common SMTP ports |
| 81 | + ports := []string{"587", "25", "465"} |
| 82 | + for _, port := range ports { |
| 83 | + // Format address based on IP version |
| 84 | + var address string |
| 85 | + if ip.To4() != nil { |
| 86 | + // IPv4 |
| 87 | + address = fmt.Sprintf("%s:%s", ip.String(), port) |
| 88 | + } else { |
| 89 | + // IPv6 - wrap in square brackets |
| 90 | + address = fmt.Sprintf("[%s]:%s", ip.String(), port) |
| 91 | + } |
| 92 | + |
| 93 | + // Set timeout for connection |
| 94 | + smtpTimeout := time.Duration(time.Second * 5) |
| 95 | + |
| 96 | + // Try to connect |
| 97 | + conn, err := net.DialTimeout("tcp", address, smtpTimeout) |
| 98 | + if err != nil { |
| 99 | + continue |
| 100 | + } |
| 101 | + defer conn.Close() |
| 102 | + |
| 103 | + return &SMTPDetails{ |
| 104 | + Server: mailServer, |
| 105 | + Port: port, |
| 106 | + Protocol: "SMTP", |
| 107 | + IPAddress: ip.String(), |
| 108 | + }, nil |
| 109 | + } |
| 110 | + } |
| 111 | + return nil, fmt.Errorf("no available SMTP servers found for %s", mailServer) |
| 112 | +} |
| 113 | + |
| 114 | +// GetMailServersFromReceipientEmail extracts the domain from the given email address |
| 115 | +// and retrieves the mail servers associated with that domain. |
| 116 | +// |
| 117 | +// Parameters: |
| 118 | +// email (string): The recipient's email address. |
| 119 | +// |
| 120 | +// Returns: |
| 121 | +// []string: A slice of mail server addresses. |
| 122 | +// error: An error object if there was an issue extracting the domain or retrieving the mail servers. |
| 123 | +func(c *Client) GetMailServersFromReceipientEmail(email string) ([]string, error) { |
| 124 | + // Extract domain from email address |
| 125 | + domain,err := c.ExtractDomainFromEmailAddress(email) |
| 126 | + if err != nil { |
| 127 | + return nil, fmt.Errorf("error extracting domain from email address: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + return c.GetMailServers(domain) |
| 131 | +} |
0 commit comments