File: //usr/local/mailchannels/hooks/PostAccountsCreate.php
<?php
namespace MailChannels;
use Net_DNS2_Exception;
class PostAccountsCreate extends Hook {
public function execute($hookData) {
$domainName = $hookData["data"]["domain"];
$result = true;
$message = "";
if ($this->inboundEnabled() && $this->inboundConfig->isAutomaticDomainProtectionEnabled()) {
list($inboundProvisionResult, $inboundProvisionMsg) = $this->inboundProvision($domainName);
// if inboundProvision fails, return that result
if (!$inboundProvisionResult) {
return array($inboundProvisionResult, $inboundProvisionMsg);
}
$message .= "successfully inbound provisioned $domainName to use MailChannels;";
} else {
$isInboundEnabled = var_export($this->inboundEnabled(), true);
$isInboundAutoProtectionOn = $this->inboundEnabled() ? var_export($this->inboundConfig->isAutomaticDomainProtectionEnabled(), true) : "false";
$message .= "not inbound provisioning $domainName, inbound enabled: $isInboundEnabled, automaticDomainProtectionEnabled: $isInboundAutoProtectionOn;";
}
try {
if ($this->outboundEnabled() && $this->outboundConfig->autoUpdateSPFRecords()) {
$this->addMCSPF($domainName);
$message .= " mailchannels spf record added for $domainName;";
} else {
$isOutbound = var_export($this->outboundEnabled(), true);
$isOutboundAutoUpdateSPFRecordsOn = $this->outboundEnabled() ? var_export($this->outboundConfig->autoUpdateSPFRecords(), true) : "false";
$message .= " not adding the mailchannels spf record for $domainName, outbound enabled: $isOutbound, autoUpdateSPFRecords: $isOutboundAutoUpdateSPFRecordsOn;";
}
} catch (OutboundConfigurationException $e) {
$message .= " An error occurred while checking the outbound status in the PostAccountsCreate hook (domain: " .
"$domainName): " . $e->getMessage() . ";";
return array(false, $message);
}
return array($result, $message);
}
public function inboundProvision($domainName) {
try {
$result = $this->inboundSmtpService->protectDomains($domainName);
if ( !empty($result->getFailedDomains()) ) {
$message = "could not provision all domains for $domainName: ";
foreach ($result->getFailedDomains() as $domain => $error) {
$message .= "$domain $error ";
}
return array(false, $message);
}
} catch (InboundSMTPServiceNotConfiguredException $e) {
$message = "An error occurred while protecting a domain in the PostAccountsCreate (domain: $domainName): " .
$e->getMessage();
return array(false, $message);
}
return array(true, "successful provision");
}
protected function addMCSPF($domainName) {
$e = null;
try {
$outboundSMTPService = $this->getOutboundSMTPService();
$outboundSMTPService->addMCSPFForDomain($domainName);
} catch (DNSRecordException $e) {
} catch (OutboundSMTPServiceInternalErrorException $e) {
} catch (Net_DNS2_Exception $e) {
} catch (MissingZoneException $e) {
} catch (UnknownDNSRecordClass $e) {
}
if ($e !== null) {
$message = "An error occurred while adding the MailChannels SPF term to domain '$domainName': " . $e->getMessage();
return array(false, $message);
}
return array(true, "added the MailChannels SPF record");
}
public static function category() {
return 'Whostmgr';
}
public static function event() {
return 'Accounts::Create';
}
public static function stage() {
return 'post';
}
}