Open chrillep opened 1 year ago
@lissmeister sugen ?
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:
Here are the detailed steps and sample code to accomplish this:
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).
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:
Install the requests
library if you haven't already:
pip install requests
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())
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.
/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())
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.
Suggestion: Automate the verification of domain names, print DNS records in the output.
refs