I'm working on a project with the cPanel API, and my current task is implementing a way to automatically add/remove MX entries. (Specifically this is for the automation of setting up domains for google apps).
The main problem I am having is removing the existing MX entries. For some reason, my code will remove only remove zone of the MX records, even though I can clearly see it stepping through each one, and I get a valid status response from the API. Here is the code I am using to accomplish this:
The cpdo() function returns an array from based on the query. Here is what the output looks like when attempting to remove some gmail MX records.
And here is the resulting MX entries in the zone file after this was ran:
One thing I had thought may be happening. Is after the MX record gets removed, the line numbers change. But I'm not 100% sure how to avoid this. Maybe I can try to add the MX entries to an array, reverse the order after the foreach loop, and then go through and delete them? That would make sure the line numbers wouldn't change I believe. Any help with this would be greatly appreciated.
The main problem I am having is removing the existing MX entries. For some reason, my code will remove only remove zone of the MX records, even though I can clearly see it stepping through each one, and I get a valid status response from the API. Here is the code I am using to accomplish this:
Code:
$query = "xml-api/dumpzone?domain=$domain";
$zdata = cpdo($whmip, $whmhash, $query);
foreach ($zdata['result']['record'] as $key => $val) {
if ($val['type'] == "MX") {
$line = $val['Line'];
$exchange = $val['exchange'];
$q = "xml-api/removezonerecord?zone=$domain&Line=$line";
$mxdata = cpdo($whmip, $whmhash, $q);
if ($mxdata['result']['status'] == "0") {
print "Removal of $exchange failed. -- ";
print $mxdata['result']['statusmsg'];
print "\n";
} else {
print "$exchange removed.\n";
print_r($mxdata);
}
}
Code:
aspmx.l.google.com removed.
Array
(
[result] => Array
(
[status] => 1
[statusmsg] => Bind reload skipped on TEST (nameserver disabled)
)
)
alt1.aspmx.l.google.com removed.
Array
(
[result] => Array
(
[status] => 1
[statusmsg] => Bind reload skipped on TEST (nameserver disabled)
)
)
alt2.aspmx.l.google.com removed.
Array
(
[result] => Array
(
[status] => 1
[statusmsg] => Bind reload skipped on TEST (nameserver disabled)
)
)
aspmx2.googlemail.com removed.
Array
(
[result] => Array
(
[status] => 1
[statusmsg] => Bind reload skipped on TEST (nameserver disabled)
)
)
aspmx3.googlemail.com removed.
Array
(
[result] => Array
(
[status] => 1
[statusmsg] => Bind reload skipped on TEST (nameserver disabled)
)
)
aspmx4.googlemail.com removed.
Array
(
[result] => Array
(
[status] => 1
[statusmsg] => Bind reload skipped on TEST (nameserver disabled)
)
)
aspmx5.googlemail.com removed.
Array
(
[result] => Array
(
[status] => 1
[statusmsg] => Bind reload skipped on TEST (nameserver disabled)
)
)
Code:
testingaso123.com. 900 IN MX 0 aspmx.l.google.com.
testingaso123.com. 900 IN MX 10 alt2.aspmx.l.google.com.
testingaso123.com. 900 IN MX 20 aspmx3.googlemail.com.
testingaso123.com. 900 IN MX 20 aspmx5.googlemail.com.
Last edited: