File: //usr/local/mailchannels/hooks/PostUAPIAddSubdomain.php
<?php
namespace MailChannels;
// The UAPI does not return a status to indicate success or failure; we'll try to
// add the subdomain's spf record no matter what. If for some reason the UAPI function
// failed, the subdomain won't exist in the list of domains returned for the user
// and we'll (correctly) do nothing.
class PostUAPIAddSubdomain extends Hook {
public function execute($hookData) {
if ($this->outboundEnabled()) {
if (isset($hookData['data']['args']) && isset($hookData['data']['args']['rootdomain'])) {
if ($this->outboundConfig->autoUpdateSPFRecords()) {
$rootDomain = $hookData['data']['args']['rootdomain'];
$subDomainPrefix = $hookData['data']['args']['domain'];
try {
return $this->addMCSubdomainSPF($rootDomain, $subDomainPrefix);
} catch (\Exception $e) {
$errorMessage = $e->getTraceAsString();
return array(false, "error adding the mailchannels spf record for subdomains of $rootDomain: $errorMessage");
}
}
return array(true, "action not taken; auto updating spf records is not set");
}
App::getLogger()->info("failed addsubdomain hook data: " . var_export($hookData, true));
return array(false, "could not find the rootdomain in the hook data");
}
return array(true, "action not taken; outbound not enabled");
}
protected function addMCSubdomainSPF($rootDomain, $subDomain) {
$e = null;
$subDomainName = $subDomain . "." . $rootDomain;
$domain = (new WHM\DomainBuilder())
->setDomain($subDomainName)
->setParentDomain($rootDomain)
->build();
try {
$this->getOutboundSMTPService()->addMCSPFForSubDomain($domain);
} catch (\Exception $e) {
$message = "error adding the mailchannels spf term to subdomain $subDomainName: " . $e->getMessage();
return array(false, $message);
}
return array(true, "added the MailChannels SPF record to $subDomainName");
}
public static function category() {
return 'Cpanel';
}
public static function event() {
return 'UAPI::SubDomain::addsubdomain';
}
public static function stage() {
return 'post';
}
}