#!/usr/bin/env python3
"""
Email validation script using Xeams API
Validates an email address using the /email/validate endpoint
"""
import os
import sys
import requests
def validate_email():
# Read configuration from environment variables
api_url = os.getenv('XEAMS_URL')
auth_key = os.getenv('XEAMS_AUTH_KEY')
secret = os.getenv('XEAMS_SECRET')
# Validate environment variables
if not api_url:
print("Error: XEAMS_URL environment variable is not set")
sys.exit(1)
if not auth_key:
print("Error: XEAMS_AUTH_KEY environment variable is not set")
sys.exit(1)
if not secret:
print("Error: XEAMS_SECRET environment variable is not set")
sys.exit(1)
# Prompt user for recipient's email address
recipient_email = input("Enter recipient's email address (re): ").strip()
if not recipient_email:
print("Error: Recipient's email address cannot be empty")
sys.exit(1)
# Prompt for sender's email address (required by API)
sender_email = input("Enter sender's email address (se): ").strip()
if not sender_email:
print("Error: Sender's email address cannot be empty")
sys.exit(1)
# Optional: prompt for deep check
deep_check = input("Perform deep check? (y/n, default: y): ").strip().lower()
deep = True if deep_check != 'n' else False
# Construct the API endpoint URL
endpoint = f"{api_url.rstrip('/')}/email/validate"
# Prepare query parameters
params = {
'auth-key': auth_key,
'secret': secret,
're': recipient_email,
'se': sender_email,
'deep': str(deep).lower()
}
try:
# Make the API request
print(f"\nValidating email: {recipient_email}...")
response = requests.get(endpoint, params=params)
# Parse response
if response.status_code == 200:
data = response.json()
code = data.get('code', 0)
description = data.get('description', 'No description')
print(f"\nValidation Result:")
print(f" Code: {code}")
print(f" Description: {description}")
# Interpret the code
if code == 1:
print(" Status: ✓ Email is valid")
elif code == 2:
print(" Status: ✗ Email address is invalid")
elif code == 3:
print(" Status: ✗ Domain is invalid")
elif code == 4:
print(" Status: ✗ User does not exist")
elif code == 5:
print(" Status: ? Inconclusive (server not listening or greylisted)")
else:
print(f" Status: Unknown code")
elif response.status_code == 400:
print(f"Error: Bad request - {response.json().get('description', 'Invalid input')}")
sys.exit(1)
elif response.status_code == 401:
print("Error: Invalid auth-key or secret")
sys.exit(1)
else:
print(f"Error: Unexpected response (HTTP {response.status_code})")
print(response.text)
sys.exit(1)
except requests.exceptions.RequestException as e:
print(f"Error: Failed to connect to API - {e}")
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
validate_email()