If it helps someone we wrote a PHP script to enable DKIM system wide for any domain that had the "hostingcp._domainkey" entry but DKIM was turned off.
There is also a script that you need to run after it has been enabled to check and Validate DKIM.
By default the scripts will only run for the first 100 websites (and all of the domains for them) but you can edit line 11 and change the offset to 100 after it has run the first time and just keep increasing it by 100 each time.
Disclaimer:
- This will generate NEW DKIM keys the same as if you were to enable it using the GUI after the update to v11
- I do not accept any responsibility for problems or data loss as a result of running this script - it worked for us but please check and understand what the code is doing before you run it yourself.
- This code was written at 03:00 in the morning so it's not pretty but it worked
Happy DKIMing
Start of the enable script (dkim-enable.php)
<?php
// Script to enable DKIM for every domain on the platform
$enhance_url = 'https://{your control panel URL}/api';
$orgId = '{your orgId from Access tokens}';
$enhance_api_key = '{your API key}';
// Function to retrieve all organizations from Enhance
function get_all_organizations($enhance_url, $enhance_api_key, $orgId) {
$url = "$enhance_url/orgs/$orgId/customers?offset=0&limit=100&sortBy=name";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['items'];
}
// Function to retrieve all domains from Enhance for a given organization
function get_all_websites($enhance_url, $enhance_api_key, $org_id) {
$url = "$enhance_url/orgs/$org_id/websites";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['items'];
}
// Function to retrieve all domains from Enhance for a given organization
function get_all_domains($enhance_url, $enhance_api_key, $org_id, $website_id) {
$url = "$enhance_url/orgs/$org_id/websites/$website_id/domains";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['items'];
}
// Function to retrieve DNS Zone from Enhance for a given domain
function get_dnszone($enhance_url, $enhance_api_key, $org_id, $website_id, $domain_id) {
$url = "$enhance_url/orgs/$org_id/websites/$website_id/domains/$domain_id/dns-zone";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['records'];
}
// Function to check DKIM for a given domain
function check_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id) {
$url = "$enhance_url/orgs/$org_id/domains/$domain_id/email-auth";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Function to enable DKIM for a given domain
function enable_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id) {
$url = "$enhance_url/orgs/$org_id/domains/$domain_id/email-auth";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$data = [
'dkim' => true
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // Using PUT method as per OpenAPI specification
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if($response === FALSE) {
$result = 'Unable to connect: ' . curl_errno($curl) . ' - ' . curl_error($curl);
}
else
{
$result = 'success';
}
curl_close($ch);
return $result;
}
// Main script
$organizations = get_all_organizations($enhance_url, $enhance_api_key, $orgId);
foreach ($organizations as $org) {
echo "Checking {$org['name']}<br>";
flush();
$org_id = $org['id'];
$websites = get_all_websites($enhance_url, $enhance_api_key, $org_id);
foreach ($websites as $website) {
$website_id = $website['id'];
$domains = get_all_domains($enhance_url, $enhance_api_key, $org_id, $website_id);
foreach ($domains as $domain) {
$domain_id = $domain['domainId'];
$dkimresult = check_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id);
if ($dkimresult['dkim'] == 'true')
{} else {
$dnszone = get_dnszone($enhance_url, $enhance_api_key, $org_id, $website_id, $domain_id);
$dkimdnsrecord = false;
foreach ($dnszone as $zone) {
if ($zone['name']=='hostingcp._domainkey') $dkimdnsrecord = true;
}
if ($dkimdnsrecord)
{
$result = enable_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id);
if ($result == 'success')
{
echo "DKIM re-enabled for domain: {$domain['domain']}<br>";
flush();
} else {
echo "Failed to re-enable DKIM for domain: {$domain['domain']}<br>";
flush();
}
}
}
}
}
}
echo "DKIM enablement process completed.\n";
?>
End of the enable script (dkim-enable.php)
Start of the validation script (dkim-validate.php)
<?php
// Script to validate DKIM for every domain on the platform
$enhance_url = 'https://{your control panel URL}/api';
$orgId = '{your orgId from Access tokens}';
$enhance_api_key = '{your API key}';
// Function to retrieve all organizations from Enhance
function get_all_organizations($enhance_url, $enhance_api_key, $orgId) {
$url = "$enhance_url/orgs/$orgId/customers?offset=0&limit=100&sortBy=name";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['items'];
}
// Function to retrieve all domains from Enhance for a given organization
function get_all_websites($enhance_url, $enhance_api_key, $org_id) {
$url = "$enhance_url/orgs/$org_id/websites";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['items'];
}
// Function to retrieve all domains from Enhance for a given organization
function get_all_domains($enhance_url, $enhance_api_key, $org_id, $website_id) {
$url = "$enhance_url/orgs/$org_id/websites/$website_id/domains";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['items'];
}
// Function to retrieve DNS Zone from Enhance for a given domain
function get_dnszone($enhance_url, $enhance_api_key, $org_id, $website_id, $domain_id) {
$url = "$enhance_url/orgs/$org_id/websites/$website_id/domains/$domain_id/dns-zone";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true)['records'];
}
// Function to check DKIM for a given domain
function check_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id) {
$url = "$enhance_url/orgs/$org_id/domains/$domain_id/email-auth";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Function to check DKIM for a given domain
function validate_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id) {
$url = "$enhance_url/orgs/$org_id/domains/$domain_id/email-auth/validate";
$headers = [
"Authorization: Bearer $enhance_api_key",
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Main script
$organizations = get_all_organizations($enhance_url, $enhance_api_key, $orgId);
foreach ($organizations as $org) {
echo "Checking {$org['name']}<br>";
flush();
$org_id = $org['id'];
$websites = get_all_websites($enhance_url, $enhance_api_key, $org_id);
foreach ($websites as $website) {
$website_id = $website['id'];
$domains = get_all_domains($enhance_url, $enhance_api_key, $org_id, $website_id);
foreach ($domains as $domain) {
$domain_id = $domain['domainId'];
$dkimresult = check_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id);
if ($dkimresult['dkim'] == 'true')
{
$validateresult = validate_dkim($enhance_url, $enhance_api_key, $org_id, $domain_id);
if ($validateresult['dkimIsValid'])
{
echo "DKIM validated for domain: {$domain['domain']}<br>";
flush();
} else {
echo "DKIM NOT validated for domain: {$domain['domain']}<br>";
flush();
}
}
}
}
}
echo "DKIM validation process completed.\n";
?>
End of the validation script (dkim-validate.php)