triggerfishab / lisa-cli

0 stars 0 forks source link

Sendgrid verify domain name #93

Open chrillep opened 1 year ago

chrillep commented 1 year ago

Suggestion: Automate the verification of domain names, print DNS records in the output.


refs

chrillep commented 1 year ago

@lissmeister sugen ?

lissmeister commented 4 months ago

Example in python for achieving this (https://chatgpt.com/c/a2f88698-7166-42dc-874c-b9eb0ed410eb)

To set up domain authentication (DKIM and SPF) for a domain via the SendGrid API, you'll need to follow these steps:

  1. Create an API Key: Make sure you have an API key with sufficient permissions.
  2. Authenticate Domain: Use the SendGrid API to authenticate your domain by setting up DKIM and SPF records.

Here are the detailed steps and sample code to accomplish this:

Step 1: Create an API Key

Log in to your SendGrid account and create an API key with the required permissions (Full Access or at least the Mail Send and Settings permissions).

Step 2: Authenticate Domain

You need to use the SendGrid API to authenticate your domain. The process involves sending a POST request to the /v3/whitelabel/domains endpoint.

Here’s an example using Python and the requests library:

Prerequisites

Python Code

import requests

# Your SendGrid API key
API_KEY = 'YOUR_SENDGRID_API_KEY'

# Headers for the API request
headers = {
    'Authorization': f'Bearer {API_KEY}',
    'Content-Type': 'application/json'
}

# Domain authentication data
data = {
    "domain": "example.com", # Your domain
    "subdomain": "email", # Subdomain you want to use for email (typically "email" or "mail")
    "username": "user@example.com", # Optional: Your email address
    "automatic_security": True # This enables DKIM key management by SendGrid
}

# Make the API request to authenticate the domain
response = requests.post(
    'https://api.sendgrid.com/v3/whitelabel/domains',
    headers=headers,
    json=data
)

# Check the response status
if response.status_code == 201:
    print("Domain authentication initiated successfully.")
    print("Response data:", response.json())
else:
    print(f"Failed to authenticate domain. Status code: {response.status_code}")
    print("Error message:", response.json())

Step 3: Set Up DNS Records

After successfully initiating the domain authentication, SendGrid will provide you with the necessary DNS records (DKIM and SPF) that you need to add to your DNS provider.

  1. Retrieve DNS Records: You can get the DNS records from the response of the previous API call, or by making a GET request to the /v3/whitelabel/domains/{domain_id} endpoint.
domain_id = response.json()['id'] # Retrieve the domain ID from the response

# Fetch DNS records
dns_response = requests.get(
    f'https://api.sendgrid.com/v3/whitelabel/domains/{domain_id}',
    headers=headers
)

if dns_response.status_code == 200:
    dns_records = dns_response.json().get('dns', {})
    print("DNS Records:")
    for record in dns_records.get('mail_cname', []):
        print(f"Type: CNAME, Hostname: {record['host']}, Value: {record['data']}")
    for record in dns_records.get('dkim1', []):
        print(f"Type: CNAME, Hostname: {record['host']}, Value: {record['data']}")
    for record in dns_records.get('dkim2', []):
        print(f"Type: CNAME, Hostname: {record['host']}, Value: {record['data']}")
else:
    print(f"Failed to retrieve DNS records. Status code: {dns_response.status_code}")
    print("Error message:", dns_response.json())
  1. Add DNS Records: Log in to your DNS provider's management console and add the provided CNAME records for DKIM and SPF.

Step 4: Verify DNS Records

Once the DNS records are added, you need to verify them via the SendGrid API.

# Verify the DNS records
verify_response = requests.post(
    f'https://api.sendgrid.com/v3/whitelabel/domains/{domain_id}/verify',
    headers=headers
)

if verify_response.status_code == 200:
    print("Domain verification successful.")
else:
    print(f"Failed to verify domain. Status code: {verify_response.status_code}")
    print("Error message:", verify_response.json())

After completing these steps, your domain should be authenticated with DKIM and SPF records set up via the SendGrid API.