This commit is contained in:
2023-12-11 15:48:31 +01:00
parent 3cf5ca84f5
commit e40614aa0e
9 changed files with 299 additions and 153 deletions

View File

@ -18,10 +18,7 @@ namespace D3\MailConfigChecker\Application\Controller\Admin;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException;
use Mika56\SPFCheck\DNS\DNSRecordGetter;
use Mika56\SPFCheck\Model\Query;
use Mika56\SPFCheck\Model\Result;
use Mika56\SPFCheck\SPFCheck;
use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Email;
@ -30,6 +27,7 @@ use OxidEsales\Eshop\Core\Registry;
class MailConfigCheck extends AdminDetailsController
{
protected $_sThisTemplate = 'mailConfigCheck.tpl';
protected $testMailAddress = 'test@example.com';
public function render()
{
@ -51,9 +49,6 @@ class MailConfigCheck extends AdminDetailsController
->that( $shop->getFieldData( 'oxsmtp' ), $lang->translateString('SHOP_MAIN_SMTPSERVER') )
->notBlank( $lang->translateString('D3_ASSERTIONS_NOTSET') )
->regex( '/.*:(587|2525)$/m', $lang->translateString('D3_ASSERTIONS_NOPORT') )
->that( 'cloud1-vm279.de-nserver.de:5', $lang->translateString('SHOP_MAIN_SMTPSERVER') )
->notBlank( $lang->translateString('D3_ASSERTIONS_NOTSET') )
->regex( '/.*:(587|2525)$/m', $lang->translateString('D3_ASSERTIONS_NOPORT') )
->that( $shop->getFieldData( 'oxsmtpuser' ), $lang->translateString('SHOP_MAIN_SMTPUSER') )
->notBlank( $lang->translateString('D3_ASSERTIONS_NOTSET') )
->that( $shop->getFieldData( 'oxsmtppwd' ), $lang->translateString('SHOP_MAIN_SMTPPASSWORD') )
@ -72,83 +67,19 @@ class MailConfigCheck extends AdminDetailsController
public function checkConfiguration()
{
dumpvar('');
dumpvar('');
dumpvar('');
dumpVar('');
$this->useSmtp();
$this->checkSpf();
$this->getCurrentMailer();
}
protected function useSmtp()
protected function getCurrentMailer()
{
try {
echo "<h3>Mailer Check</h3>";
$mail = oxNew( Email::class );
$mail->setRecipient( 'test@example.com' );
$mail->setRecipient( $this->testMailAddress );
$mail->setBody('.');
$mail->send();
dumpvar( 'Shop sendet ueber '.ucfirst($mail->getMailer()) );
} catch (\Exception $e) {
dumpvar($e->getMessage());
$this->addTplParam('mailer', $mail->getMailer());
} catch ( Exception $e) {
Registry::getUtilsView()->addErrorToDisplay($e);
}
}
protected function checkSpf()
{
echo "<h3>SPF Check</h3>";
$mailDomains = $this->getMailDomains();
array_walk(
$mailDomains,
function($domain) {
dumpvar('<b>'.$domain.'</b>');
$this->checkSpfByDomain($domain);
}
);
}
protected function checkSpfByDomain($domain)
{
$checker = new SPFCheck(new DNSRecordGetter());
$query = new Query('', $domain);
$result = $checker->getResult($query);
if (in_array(
$result->getResult(),
[Result::FAIL, Result::NEUTRAL, Result::PASS, Result::SOFTFAIL]
)) {
dumpvar('SPF is set');
} elseif (Result::NONE) {
dumpvar('SPF missing');
} else {
dumpvar('error determine SPF record');
}
if ($record = $result->getRecord()) {
dumpvar($record->getRawRecord());
}
}
protected function getMailDomains()
{
return
array_filter(
array_unique(
array_map(
function($mailAddress) {
$mailAddress = trim($mailAddress);
return strstr($mailAddress, '@') ?
array_pop(explode('@', $mailAddress)) :
'';
},
[
Registry::getConfig()->getActiveShop()->getFieldData('oxinfoemail'),
Registry::getConfig()->getActiveShop()->getFieldData('oxorderemail'),
Registry::getConfig()->getActiveShop()->getFieldData('oxowneremail'),
]
)
)
);
}
}

View File

@ -0,0 +1,117 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\MailConfigChecker\Application\Controller\Admin;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\MailConfigChecker\Application\Model\SpfResult;
use Mika56\SPFCheck\DNS\DNSRecordGetter;
use Mika56\SPFCheck\Model\Query;
use Mika56\SPFCheck\Model\Result;
use Mika56\SPFCheck\SPFCheck;
use Net_SMTP;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Registry;
use PEAR;
use PEAR_Error;
class SpfChecker extends AdminDetailsController
{
protected $_sThisTemplate = 'spfCheck.tpl';
public function render()
{
$this->checkSpf();
return parent::render();
}
protected function checkSpf()
{
$result = [];
$mailDomains = $this->getMailDomains();
array_walk(
$mailDomains,
function($domain) use (&$result) {
$this->checkSpfByDomain($domain, $result);
}
);
$this->addTplParam('result', $result);
}
protected function getMailDomains()
{
return
array_filter(
array_unique(
array_map(
function($mailAddress) {
$mailAddress = trim($mailAddress);
return strstr($mailAddress, '@') ?
array_pop(explode('@', $mailAddress)) :
'';
},
[
Registry::getConfig()->getActiveShop()->getFieldData('oxinfoemail'),
Registry::getConfig()->getActiveShop()->getFieldData('oxorderemail'),
Registry::getConfig()->getActiveShop()->getFieldData('oxowneremail'),
]
)
)
);
}
protected function checkSpfByDomain($domain, &$summarize)
{
$checker = new SPFCheck(new DNSRecordGetter());
$query = new Query('', $domain);
$result = $checker->getResult($query);
switch ($result->getResult()) {
case Result::FAIL:
case Result::NEUTRAL:
case Result::PASS:
case Result::SOFTFAIL:
$status = SpfResult::SET;
break;
case Result::NONE:
$status = SpfResult::MISSING;
break;
default:
$status = SpfResult::ERROR;
}
$rawRecord = ($record = $result->getRecord()) ?
$record->getRawRecord() :
null;
$summarize[$domain] = oxNew(SpfResult::class, $status, $rawRecord);
}
public function getSpfStatusColor(SpfResult $result)
{
switch ($result->getStatus()) {
case SpfResult::SET:
return 'success';
case SpfResult::ERROR:
return 'warning';
default:
return 'danger';
}
}
}