forked from AfterShip/email-verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp_by_api_gmail.go
53 lines (46 loc) · 1.17 KB
/
smtp_by_api_gmail.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
49
50
51
52
53
package emailverifier
import (
"context"
"fmt"
"net/http"
"strings"
"time"
)
const (
glxuPageFormat = "https://mail.google.com/mail/gxlu?email=%s"
)
// See the link below to know why we can use this way to check if a gmail exists.
// https://blog.0day.rocks/abusing-gmail-to-get-previously-unlisted-e-mail-addresses-41544b62b2
func newGmailAPIVerifier(client *http.Client) smtpAPIVerifier {
if client == nil {
client = http.DefaultClient
}
return gmail{
client: client,
}
}
type gmail struct {
client *http.Client
}
func (g gmail) isSupported(host string) bool {
return strings.HasSuffix(host, ".google.com.")
}
func (g gmail) check(domain, username string) (*SMTP, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
email := fmt.Sprintf("%s@%s", username, domain)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf(glxuPageFormat, email), nil)
if err != nil {
return nil, err
}
resp, err := g.client.Do(request)
if err != nil {
return &SMTP{}, err
}
defer resp.Body.Close()
emailExists := len(resp.Cookies()) > 0
return &SMTP{
HostExists: true,
Deliverable: emailExists,
}, nil
}