@@ -50,9 +50,6 @@ type EmailVerification struct {
50
50
Result string `json:"result"`
51
51
// Engagement results are a macro-level view that explain an email recipient’s propensity to engage.
52
52
// https://documentation.mailgun.com/docs/inboxready/mailgun-validate/validate_engagement/
53
- //
54
- // Only for v4. To use the /v4 version of validations define MG_URL in the environment variable
55
- // as `https://api.mailgun.net/v4` or set `v.SetAPIBase("https://api.mailgun.net/v4")`
56
53
Engagement * EngagementData `json:"engagement,omitempty"`
57
54
}
58
55
@@ -93,9 +90,11 @@ type EmailValidatorImpl struct {
93
90
apiKey string
94
91
}
95
92
96
- // Creates a new validation instance.
97
- // * If a public key is provided, uses the public validation endpoints
98
- // * If a private key is provided, uses the private validation endpoints
93
+ // NewEmailValidator creates a new validation instance.
94
+ //
95
+ // * For ValidateEmail use private key
96
+ //
97
+ // * For ParseAddresses use public key
99
98
func NewEmailValidator (apiKey string ) * EmailValidatorImpl {
100
99
isPublicKey := false
101
100
@@ -105,16 +104,19 @@ func NewEmailValidator(apiKey string) *EmailValidatorImpl {
105
104
}
106
105
107
106
return & EmailValidatorImpl {
107
+ // TODO(vtopc): Don’t use http.DefaultClient - https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
108
108
client : http .DefaultClient ,
109
109
isPublicKey : isPublicKey ,
110
- apiBase : APIBase ,
110
+ apiBase : "https://api.mailgun.net/v4" ,
111
111
apiKey : apiKey ,
112
112
}
113
113
}
114
114
115
115
// NewEmailValidatorFromEnv returns a new EmailValidator using environment variables
116
- // If MG_PUBLIC_API_KEY is set, assume using the free validation subject to daily usage limits
117
- // If only MG_API_KEY is set, assume using the /private validation routes with no daily usage limits
116
+ //
117
+ // * For ValidateEmail set MG_API_KEY
118
+ //
119
+ // * For ParseAddresses set MG_PUBLIC_API_KEY
118
120
func NewEmailValidatorFromEnv () (* EmailValidatorImpl , error ) {
119
121
apiKey := os .Getenv ("MG_PUBLIC_API_KEY" )
120
122
if apiKey == "" {
@@ -167,27 +169,15 @@ func (m *EmailValidatorImpl) getAddressURL(endpoint string) string {
167
169
// ValidateEmail performs various checks on the email address provided to ensure it's correctly formatted.
168
170
// It may also be used to break an email address into its sub-components. If user has set the
169
171
func (m * EmailValidatorImpl ) ValidateEmail (ctx context.Context , email string , mailBoxVerify bool ) (EmailVerification , error ) {
170
- if strings . HasSuffix ( m . APIBase (), "/v4" ) {
171
- return m . validateV4 ( ctx , email , mailBoxVerify )
172
+ if m . isPublicKey {
173
+ return EmailVerification {}, errors . New ( "ValidateEmail: public key is not supported anymore, use private key" )
172
174
}
173
- return m .validateV3 (ctx , email , mailBoxVerify )
174
- }
175
175
176
- func (m * EmailValidatorImpl ) validateV3 (ctx context.Context , email string , mailBoxVerify bool ) (EmailVerification , error ) {
177
- r := newHTTPRequest (m .getAddressURL ("validate" ))
178
- r .setClient (m .Client ())
179
- r .addParameter ("address" , email )
180
- if mailBoxVerify {
181
- r .addParameter ("mailbox_verification" , "true" )
176
+ if strings .HasSuffix (m .APIBase (), "/v4" ) {
177
+ return m .validateV4 (ctx , email , mailBoxVerify )
182
178
}
183
- r .setBasicAuth (basicAuthUser , m .APIKey ())
184
179
185
- var res EmailVerification
186
- err := getResponseFromJSON (ctx , r , & res )
187
- if err != nil {
188
- return EmailVerification {}, err
189
- }
190
- return res , nil
180
+ return EmailVerification {}, errors .New ("ValidateEmail: only v4 is supported" )
191
181
}
192
182
193
183
func (m * EmailValidatorImpl ) validateV4 (ctx context.Context , email string , mailBoxVerify bool ) (EmailVerification , error ) {
@@ -221,6 +211,11 @@ func (m *EmailValidatorImpl) validateV4(ctx context.Context, email string, mailB
221
211
222
212
// ParseAddresses takes a list of addresses and sorts them into valid and invalid address categories.
223
213
// NOTE: Use of this function requires a proper public API key. The private API key will not work.
214
+ //
215
+ // NOTE: Only for v3. To use the /v3 version of validations define MG_URL in the environment variable
216
+ // as `https://api.mailgun.net/v3` or set `v.SetAPIBase("https://api.mailgun.net/v3")`
217
+ //
218
+ // Deprecated: /v3/address/parse is deprecated use ValidateEmail instead.
224
219
func (m * EmailValidatorImpl ) ParseAddresses (ctx context.Context , addresses ... string ) ([]string , []string , error ) {
225
220
r := newHTTPRequest (m .getAddressURL ("parse" ))
226
221
r .setClient (m .Client ())
0 commit comments