1package mailgun
2
3import (
4	"strings"
5)
6
7// The EmailVerificationParts structure breaks out the basic elements of an email address.
8// LocalPart includes everything up to the '@' in an e-mail address.
9// Domain includes everything after the '@'.
10// DisplayName is no longer used, and will appear as "".
11type EmailVerificationParts struct {
12	LocalPart   string `json:"local_part"`
13	Domain      string `json:"domain"`
14	DisplayName string `json:"display_name"`
15}
16
17// EmailVerification records basic facts about a validated e-mail address.
18// See the ValidateEmail method and example for more details.
19//
20// IsValid indicates whether an email address conforms to IETF RFC standards.
21// Parts records the different subfields of an email address.
22// Address echoes the address provided.
23// DidYouMean provides a simple recommendation in case the address is invalid or
24// Mailgun thinks you might have a typo.
25// DidYouMean may be empty (""), in which case Mailgun has no recommendation to give.
26// The existence of DidYouMean does NOT imply the email provided has anything wrong with it.
27// IsDisposableAddress indicates whether Mailgun thinks the address is from a known
28// disposable mailbox provider.
29// IsRoleAddress indicates whether Mailgun thinks the address is an email distribution list.
30type EmailVerification struct {
31	IsValid             bool                   `json:"is_valid"`
32	Parts               EmailVerificationParts `json:"parts"`
33	Address             string                 `json:"address"`
34	DidYouMean          string                 `json:"did_you_mean"`
35	IsDisposableAddress bool                   `json:"is_disposable_address"`
36	IsRoleAddress       bool                   `json:"is_role_address"`
37}
38
39type addressParseResult struct {
40	Parsed      []string `json:"parsed"`
41	Unparseable []string `json:"unparseable"`
42}
43
44// ValidateEmail performs various checks on the email address provided to ensure it's correctly formatted.
45// It may also be used to break an email address into its sub-components.  (See example.)
46// NOTE: Use of this function requires a proper public API key.  The private API key will not work.
47func (m *MailgunImpl) ValidateEmail(email string) (EmailVerification, error) {
48	r := newHTTPRequest(generatePublicApiUrl(m, addressValidateEndpoint))
49	r.setClient(m.Client())
50	r.addParameter("address", email)
51	r.setBasicAuth(basicAuthUser, m.PublicApiKey())
52
53	var response EmailVerification
54	err := getResponseFromJSON(r, &response)
55	if err != nil {
56		return EmailVerification{}, err
57	}
58
59	return response, nil
60}
61
62// ParseAddresses takes a list of addresses and sorts them into valid and invalid address categories.
63// NOTE: Use of this function requires a proper public API key.  The private API key will not work.
64func (m *MailgunImpl) ParseAddresses(addresses ...string) ([]string, []string, error) {
65	r := newHTTPRequest(generatePublicApiUrl(m, addressParseEndpoint))
66	r.setClient(m.Client())
67	r.addParameter("addresses", strings.Join(addresses, ","))
68	r.setBasicAuth(basicAuthUser, m.PublicApiKey())
69
70	var response addressParseResult
71	err := getResponseFromJSON(r, &response)
72	if err != nil {
73		return nil, nil, err
74	}
75
76	return response.Parsed, response.Unparseable, nil
77}
78