diff --git a/Application/Controller/Admin/MailCheckBase.php b/Application/Controller/Admin/MailCheckBase.php index daff894..d9da9b2 100644 --- a/Application/Controller/Admin/MailCheckBase.php +++ b/Application/Controller/Admin/MailCheckBase.php @@ -31,9 +31,7 @@ class MailCheckBase extends AdminController public function render(): string { $this->addTplParam('sListClass', MailCheckMenu::class); - $this->addTplParam('sMainClass', setMainController::class); - - $this->_hasListItems = false; + $this->addTplParam('sMainClass', MailConfigCheck::class); return parent::render(); } diff --git a/Application/Controller/Admin/MailConfigCheck.php b/Application/Controller/Admin/MailConfigCheck.php index f18817b..4d9e3bf 100644 --- a/Application/Controller/Admin/MailConfigCheck.php +++ b/Application/Controller/Admin/MailConfigCheck.php @@ -47,18 +47,18 @@ class MailConfigCheck extends AdminDetailsController Assert::lazy() ->setExceptionClass(d3TranslatableLazyAssertionException::class) ->that( $shop->getFieldData( 'oxsmtp' ), $lang->translateString('SHOP_MAIN_SMTPSERVER') ) - ->notBlank( $lang->translateString('D3_ASSERTIONS_NOTSET') ) - ->regex( '/.*:(587|2525)$/m', $lang->translateString('D3_ASSERTIONS_NOPORT') ) + ->notBlank( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') ) + ->regex( '/.*:(587|2525)$/m', $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOPORT') ) ->that( $shop->getFieldData( 'oxsmtpuser' ), $lang->translateString('SHOP_MAIN_SMTPUSER') ) - ->notBlank( $lang->translateString('D3_ASSERTIONS_NOTSET') ) + ->notBlank( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') ) ->that( $shop->getFieldData( 'oxsmtppwd' ), $lang->translateString('SHOP_MAIN_SMTPPASSWORD') ) - ->notBlank( $lang->translateString('D3_ASSERTIONS_NOTSET') ) + ->notBlank( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') ) ->that( $shop->getFieldData( 'oxinfoemail' ), $lang->translateString('SHOP_MAIN_INFOEMAIL') ) - ->email( $lang->translateString('D3_ASSERTIONS_NOTSET') ) + ->email( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') ) ->that( $shop->getFieldData( 'oxorderemail' ), $lang->translateString('SHOP_MAIN_ORDEREMAIL') ) - ->email( $lang->translateString('D3_ASSERTIONS_NOTSET') ) + ->email( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') ) ->that( $shop->getFieldData( 'oxowneremail' ), $lang->translateString('SHOP_MAIN_OWNEREMAIL') ) - ->email( $lang->translateString('D3_ASSERTIONS_NOTSET') ) + ->email( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') ) ->verifyNow(); } catch (InvalidArgumentException $e) { Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage())); diff --git a/Application/Controller/Admin/MailTester.php b/Application/Controller/Admin/MailTester.php index c3f1879..9c1eb5f 100644 --- a/Application/Controller/Admin/MailTester.php +++ b/Application/Controller/Admin/MailTester.php @@ -15,9 +15,79 @@ namespace D3\MailConfigChecker\Application\Controller\Admin; +use Assert\Assert; +use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException; use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; +use OxidEsales\Eshop\Core\Email; +use OxidEsales\Eshop\Core\Registry; class MailTester extends AdminDetailsController { protected $_sThisTemplate = 'mailTester.tpl'; + + public function sendMail() + { + try { + $request = Registry::getRequest(); + + $from = trim($request->getRequestEscapedParameter( 'from' )); + $this->addTplParam('sender', $from); + $to = trim($request->getRequestEscapedParameter( 'to' )); + $this->addTplParam('recipient', $to); + $subject = trim($request->getRequestEscapedParameter( 'subject' )); + $this->addTplParam('subject', $subject); + $body = trim($request->getRequestEscapedParameter( 'body' )); + $this->addTplParam('body', $body); + + $this->assertContent(); + + $mail = oxNew( Email::class ); + $mail->setFrom( $from ); + $mail->sendEmail($to, $subject, $body); + $this->addTplParam('success', true); + } catch (\Exception $e) { + Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage())); + } + } + + protected function assertContent() + { + $request = Registry::getRequest(); + $lang = Registry::getLang(); + + Assert::lazy() + ->setExceptionClass(d3TranslatableLazyAssertionException::class) + ->that( + $request->getRequestEscapedParameter( 'from' ), + $lang->translateString('D3_MAILCHECKER_SMTPCHECK_SENDER') + )->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET')) + ->that( + $request->getRequestEscapedParameter( 'to' ), + $lang->translateString('D3_MAILCHECKER_SMTPCHECK_RECIPIENT') + )->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET')) + ->that( + $request->getRequestEscapedParameter( 'subject' ), + $lang->translateString('D3_MAILCHECKER_TESTMAIL_SUBJECT') + )->notBlank($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET')) + ->that( + $request->getRequestEscapedParameter( 'body' ), + $lang->translateString('D3_MAILCHECKER_TESTMAIL_BODY') + )->notBlank($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET')) + ->verifyNow(); + } + + public function getMailAddressList() + { + $shop = Registry::getConfig()->getActiveShop(); + + return array_filter( + array_unique( + [ + $shop->getFieldData('oxinfoemail'), + $shop->getFieldData('oxorderemail'), + $shop->getFieldData('oxowneremail'), + ] + ) + ); + } } \ No newline at end of file diff --git a/Application/Controller/Admin/SmtpChecker.php b/Application/Controller/Admin/SmtpChecker.php index b0d5233..5de4814 100644 --- a/Application/Controller/Admin/SmtpChecker.php +++ b/Application/Controller/Admin/SmtpChecker.php @@ -26,16 +26,18 @@ use PEAR_Error; class SmtpChecker extends AdminDetailsController { - protected bool $debug = true; + protected $debug = true; - protected string $host; - protected string $user; - protected string $from; - protected string $to; + protected $host; + protected $port; + protected $user; + protected $pwd; + protected $from; + protected $to; - protected Net_SMTP $smtp; - protected string $action; - protected array $log = []; + protected $smtp; + protected $action; + protected $log = []; public function __construct() { @@ -44,20 +46,24 @@ class SmtpChecker extends AdminDetailsController /** @var Shop $activeShop */ $activeShop = Registry::getConfig()->getActiveShop(); - list('host' => $shopHost, 'port' => $shopPort) = parse_url($activeShop->getFieldData('oxsmtp')); + if ($localHost = Registry::getRequest()->getRequestEscapedParameter('smtpHost')) { + list( 'host' => $shopHost, 'port' => $shopPort ) = parse_url( trim($localHost) ); + } else { + list( 'host' => $shopHost, 'port' => $shopPort ) = parse_url( trim($activeShop->getFieldData( 'oxsmtp' )) ); + } - $this->host = Registry::getRequest()->getRequestEscapedParameter('smtpHost') ?: $shopHost; + $this->host = $shopHost ?? null; $this->addTplParam('smtpHost', Registry::getRequest()->getRequestEscapedParameter('smtpHost')); - $this->port = Registry::getRequest()->getRequestEscapedParameter('smtpHost') ? (Registry::getRequest()->getRequestEscapedParameter('smtpPort') ?: null) : $shopPort; - $this->addTplParam('smtpPort', Registry::getRequest()->getRequestEscapedParameter('smtpPort')); + $this->port = $shopPort ?? null; $this->user = Registry::getRequest()->getRequestEscapedParameter('smtpUser') ?: $activeShop->getFieldData('oxsmtpuser'); - $this->addTplParam('smtpUser', Registry::getRequest()->getRequestEscapedParameter('smtpUser')); + $this->addTplParam('smtpUser', $this->user); $this->pwd = Registry::getRequest()->getRequestEscapedParameter('smtpPwd') ?: $activeShop->getFieldData('oxsmtppwd'); - $this->addTplParam('smtpPwd', Registry::getRequest()->getRequestEscapedParameter('smtpPwd')); - $this->from = Registry::getRequest()->getRequestEscapedParameter('from') ?: Registry::getRequest()->getRequestEscapedParameter('fromCust') ?: ''; - $this->addTplParam('from', Registry::getRequest()->getRequestEscapedParameter('from')); - $this->addTplParam('fromCust', Registry::getRequest()->getRequestEscapedParameter('fromCust')); + $this->addTplParam('smtpPwd', $this->pwd); + $this->from = Registry::getRequest()->getRequestEscapedParameter('from') ?: ''; + $this->addTplParam('from', $this->from); $this->to = Registry::getRequest()->getRequestEscapedParameter('to') ?: ''; + $this->addTplParam('recipient', $this->to); + $this->addTplParam('sendMail', Registry::getRequest()->getRequestEscapedParameter('sendmail')); $this->addTplParam('smtpLog', $this->log); } @@ -67,6 +73,13 @@ class SmtpChecker extends AdminDetailsController return 'smtpCheck.tpl'; } + public function render() + { + $this->addTplParam('shop', Registry::getConfig()->getActiveShop()); + + return parent::render(); + } + public function getMailAddressList() { $shop = Registry::getConfig()->getActiveShop(); @@ -91,6 +104,7 @@ class SmtpChecker extends AdminDetailsController $this->setFrom(); $this->setRecipient(); $this->sendContent(); + $this->addTplParam('success', true); } catch (InvalidArgumentException $e) { Registry::getUtilsView()->addErrorToDisplay($e); } finally { @@ -100,10 +114,6 @@ class SmtpChecker extends AdminDetailsController $this->addTplParam('smtpLog', $this->log); } - protected function checkDataAreSet() - { - } - /** * @throws InvalidArgumentException * @return void @@ -111,16 +121,27 @@ class SmtpChecker extends AdminDetailsController protected function hostIsAvailable() { $this->action = __FUNCTION__; - Assert::that(( $this->smtp = new Net_SMTP( $this->host, $this->port ) )) - ->isInstanceOf(Net_SMTP::class, 'Unable to instantiate SMTP object'); + Assert::that( + ( $this->smtp = new Net_SMTP( $this->host, $this->port ) ), + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_INSTANCE') + )->isInstanceOf( + Net_SMTP::class, + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOCONNECTION') + ); $this->smtp->setDebug($this->debug, [$this, 'dumpDebug']); } protected function connect() { $this->action = __FUNCTION__; - Assert::that($this->smtp->connect()) - ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'can not connect'); + Assert::that( + $this->smtp->connect(), + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_CONNECTION') + )->notIsInstanceOf( + PEAR_Error::class, + [$this, 'formatMessage'], + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOCONNECTION') + ); } /** @@ -130,42 +151,65 @@ class SmtpChecker extends AdminDetailsController protected function auth() { $this->action = __FUNCTION__; - Assert::that($this->smtp->auth($this->user, $this->pwd)) - ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'auth failed'); + Assert::that( + $this->smtp->auth($this->user, $this->pwd), + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_AUTHENTICATION') + )->notIsInstanceOf( + PEAR_Error::class, + [$this, 'formatMessage'], + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOAUTHENTICATION') + ); } protected function setFrom() { $this->action = __FUNCTION__; - Assert::that($this->smtp->mailFrom($this->from)) - ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to set sender "'.$this->from.'"'); + Assert::that( + $this->smtp->mailFrom($this->from), + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_SENDER') + )->notIsInstanceOf( + PEAR_Error::class, + [$this, 'formatMessage'], + sprintf(Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOSENDER'), $this->from) + ); } protected function setRecipient() { $this->action = __FUNCTION__; - Assert::that(( $this->smtp->rcptTo( $this->to))) - ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to set recipient "'.$this->to.'"'); + Assert::that( + $this->smtp->rcptTo( $this->to), + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_RECIPIENT') + )->notIsInstanceOf( + PEAR_Error::class, + [$this, 'formatMessage'], + sprintf(Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NORECIPIENT'), $this->to) + ); } protected function sendContent() { - if (!Registry::getRequest()->getRequestEscapedParameter('sendMail')) { + if (!Registry::getRequest()->getRequestEscapedParameter('sendmail')) { return; } $this->action = __FUNCTION__; $subj = "Subject: Test Message\n"; $body = "Body Line 1\nBody Line 2"; - Assert::that($this->smtp->data( $subj . "\r\n" . $body)) - ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to send content'); + Assert::that( + $this->smtp->data( $subj . "\r\n" . $body), + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_TRANSMIT') + )->notIsInstanceOf( + PEAR_Error::class, + [$this, 'formatMessage'], + Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOTRANSMIT') + ); } protected function disconnect() { $this->action = __FUNCTION__; - Assert::that($this->smtp->disconnect()) - ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to disconnect'); + $this->smtp->disconnect(); } public function dumpDebug($smtp, $message) diff --git a/Application/Controller/Admin/SpfChecker.php b/Application/Controller/Admin/SpfChecker.php index 6fab6f2..162ba80 100644 --- a/Application/Controller/Admin/SpfChecker.php +++ b/Application/Controller/Admin/SpfChecker.php @@ -15,19 +15,14 @@ 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 { @@ -61,9 +56,16 @@ class SpfChecker extends AdminDetailsController array_map( function($mailAddress) { $mailAddress = trim($mailAddress); - return strstr($mailAddress, '@') ? - array_pop(explode('@', $mailAddress)) : - ''; + try { + if ( ! strstr( $mailAddress, '@' ) ) { + throw oxNew( InvalidArgumentException::class ); + } + $addressChunks = explode( '@', $mailAddress ); + + return array_pop( $addressChunks ); + } catch (InvalidArgumentException $e) { + return ''; + } }, [ Registry::getConfig()->getActiveShop()->getFieldData('oxinfoemail'), diff --git a/Application/Model/Exception/d3TranslatableLazyAssertionException.php b/Application/Model/Exception/d3TranslatableLazyAssertionException.php index b102e98..7295443 100644 --- a/Application/Model/Exception/d3TranslatableLazyAssertionException.php +++ b/Application/Model/Exception/d3TranslatableLazyAssertionException.php @@ -20,14 +20,14 @@ use OxidEsales\Eshop\Core\Registry; class d3TranslatableLazyAssertionException extends LazyAssertionException { - public static function fromErrors(array $errors): self + public static function fromErrors(array $errors): LazyAssertionException { - $text = Registry::getLang()->translateString('D3_ASSERTIONS_FAILED'); - $message = \sprintf($text, \count($errors))."\n"; + $text = Registry::getLang()->translateString('D3_MAILCHECKER_ASSERTIONS_FAILED'); + $message = sprintf( $text, count( $errors ) ) . "\n"; $i = 1; foreach ($errors as $error) { - $message .= \sprintf("%d) %s: %s\n", $i++, $error->getPropertyPath(), $error->getMessage()); + $message .= sprintf( "%d) %s: %s\n", $i ++, $error->getPropertyPath(), $error->getMessage() ); } return new static($message, $errors); diff --git a/Application/Model/SpfResult.php b/Application/Model/SpfResult.php index a6176d7..0ec38d6 100644 --- a/Application/Model/SpfResult.php +++ b/Application/Model/SpfResult.php @@ -20,8 +20,8 @@ class SpfResult const MISSING = 'missing'; const ERROR = 'error'; - protected string $status; - protected ?string $record; + protected $status; + protected $record; public function __construct(string $status, ?string $record = null) { diff --git a/Application/views/admin/tpl/inc/bootstrap.tpl b/Application/views/admin/tpl/inc/bootstrap.tpl index 07a1a31..785f600 100644 --- a/Application/views/admin/tpl/inc/bootstrap.tpl +++ b/Application/views/admin/tpl/inc/bootstrap.tpl @@ -2,9 +2,13 @@ \ No newline at end of file diff --git a/Application/views/admin/tpl/mailTester.tpl b/Application/views/admin/tpl/mailTester.tpl index d4b2f07..02c9942 100644 --- a/Application/views/admin/tpl/mailTester.tpl +++ b/Application/views/admin/tpl/mailTester.tpl @@ -1,4 +1,5 @@ [{include file="headitem.tpl" title="d3mxd3cleartmp"|oxmultilangassign}] +[{include file="inc_bootstrap.tpl"}] [{if $readonly}] [{assign var="readonly" value="readonly disabled"}] @@ -12,7 +13,49 @@ -... +[{if $success}] +
+ [{oxmultilang ident="D3_MAILCHECKER_SMTPCHECK_DESC1"}] +
++ [{oxmultilang ident="D3_MAILCHECKER_SMTPCHECK_DESC2"}] +
++ [{oxmultilang ident="D3_MAILCHECKER_SMTPCHECK_DESC3"}] +
+ + +[{oxmultilang ident="D3_MAILCHECKER_SPFRESULT_"|cat:$spf->getStatus()|upper}]
+ [{if $spf->getRecord()}] ++ + +
+ [{/if}] + +