From 9481f674c0e99c2da6c077f98f960da91cca0ada Mon Sep 17 00:00:00 2001 From: Jordan Ibrahim Date: Thu, 28 Nov 2024 21:59:23 -0800 Subject: [PATCH] ZeroBounce intergration with new util file and updated login file --- backend/internal/utils/zerobounce.go | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 backend/internal/utils/zerobounce.go diff --git a/backend/internal/utils/zerobounce.go b/backend/internal/utils/zerobounce.go new file mode 100644 index 0000000..9d1205d --- /dev/null +++ b/backend/internal/utils/zerobounce.go @@ -0,0 +1,46 @@ +package utils + +import ( + "encoding/json" + "fmt" + "net/http" + "os" +) + +type ZeroBounceResponse struct { + Status string `json:"status"` +} + +// VerifyEmailWithZeroBounce checks if an email address is valid using the ZeroBounce API +func VerifyEmailWithZeroBounce(email string) (string, error) { + + apiKey := os.Getenv("ZEROBOUNCE_API_KEY") + if apiKey == "" { + + return "", fmt.Errorf("ZeroBounce API key not set in environment variables") + } + + url := fmt.Sprintf("https://api.zerobounce.net/v2/validate?email=%s&api_key=%s", email, apiKey) + + resp, err := http.Get(url) + if err != nil { + + return "", fmt.Errorf("failed to connect to ZeroBounce API: %v", err) + } + + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + + return "", fmt.Errorf("ZeroBounce API returned status: %s", resp.Status) + } + + var result ZeroBounceResponse + + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + + return "", fmt.Errorf("error decoding ZeroBounce API response: %v", err) + } + + return result.Status, nil +}