diff --git a/Application/Controller/Admin/SmtpChecker.php b/Application/Controller/Admin/SmtpChecker.php index 387a8ca..92e79b6 100644 --- a/Application/Controller/Admin/SmtpChecker.php +++ b/Application/Controller/Admin/SmtpChecker.php @@ -22,14 +22,20 @@ use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; use OxidEsales\Eshop\Application\Model\Shop; use OxidEsales\Eshop\Core\Registry; use PEAR; +use PEAR_Error; class SmtpChecker extends AdminDetailsController { protected bool $debug = true; protected string $host; + protected string $user; + protected string $from; + protected string $to; protected Net_SMTP $smtp; + protected string $action; + protected array $log = []; public function __construct() { @@ -38,47 +44,61 @@ class SmtpChecker extends AdminDetailsController /** @var Shop $activeShop */ $activeShop = Registry::getConfig()->getActiveShop(); - $this->host = Registry::getRequest()->getRequestEscapedParameter('') ?? $activeShop->getFieldData('oxsmtp'); + list('host' => $shopHost, 'port' => $shopPort) = parse_url($activeShop->getFieldData('oxsmtp')); - $this->checkDataAreSet(); - $this->hostIsAvailable(); + $this->host = Registry::getRequest()->getRequestEscapedParameter('smtpHost') ?: $shopHost; + $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->user = Registry::getRequest()->getRequestEscapedParameter('smtpUser') ?: $activeShop->getFieldData('oxsmtpuser'); + $this->addTplParam('smtpUser', Registry::getRequest()->getRequestEscapedParameter('smtpUser')); + $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->to = Registry::getRequest()->getRequestEscapedParameter('to') ?: ''; -/* - //$host = 'cloud1-vm279.de-nserver.de:587'; - $from = 'mail@ds.data-develop.de'; - $rcpt = array('test@ds.data-develop.de', 'test@daniel-seifert.com', 'recipient2@example.com'); - //$rcpt = array('test@ds.data-develop.de'); - $rcpt = array('test@daniel-seifert.com'); - $subj = "Subject: Test Message\n"; - $body = "Body Line 1\nBody Line 2"; + $this->addTplParam('smtpLog', $this->log); + } - if ( PEAR::isError( $e = $smtp->connect())) { - dumpvar(__METHOD__.__LINE__.PHP_EOL); - die($e->getMessage() . "\n"); + public function getTemplateName() + { + return 'smtpCheck.tpl'; + } + + public function getMailAddressList() + { + $shop = Registry::getConfig()->getActiveShop(); + + return array_filter( + array_unique( + [ + $shop->getFieldData('oxinfoemail'), + $shop->getFieldData('oxorderemail'), + $shop->getFieldData('oxowneremail'), + ] + ) + ); + } + + public function sendMail() + { + try { + $this->checkDataAreSet(); + $this->hostIsAvailable(); + $this->connect(); + $this->auth(); + $this->setFrom(); + $this->setRecipient(); + $this->sendContent(); + } catch (InvalidArgumentException $e) { + Registry::getUtilsView()->addErrorToDisplay($e); + } finally { + $this->disconnect(); } - if ( PEAR::isError( $smtp->mailFrom( $from))) { - die("Unable to set sender to $from\n"); - } - - foreach ($rcpt as $to) { - dumpvar($to.PHP_EOL); - if ( PEAR::isError( $res = $smtp->rcptTo( $to))) { - die("Unable to add recipient $to: " . $res->getMessage() . "\n"); - } - } - - /* Set the body of the message. */ -/* - if ( PEAR::isError( $res = $smtp->data( $subj . "\r\n" . $body))) { - dumpvar($res->getMessage()); - die("Unable to send data\n"); - } - - /* Disconnect from the SMTP server. */ -/* - $smtp->disconnect(); - */ + $this->addTplParam('smtpLog', $this->log); } protected function checkDataAreSet() @@ -91,20 +111,85 @@ class SmtpChecker extends AdminDetailsController */ protected function hostIsAvailable() { - $this->host = 'test.example.dev'; - Assert::that(( $this->smtp = new Net_SMTP( $this->host ) ))->true('Unable to instantiate SMTP object'); - $this->smtp->setDebug($this->debug); + $this->action = __FUNCTION__; + Assert::that(( $this->smtp = new Net_SMTP( $this->host, $this->port ) )) + ->isInstanceOf(Net_SMTP::class, 'Unable to instantiate SMTP object'); + $this->smtp->setDebug($this->debug, [$this, 'dumpDebug']); } - protected function foo() + protected function connect() { - PEAR::isError( $e = $this->smtp->connect()); - dumpvar(get_class($e)); - die(); - Assert::that(PEAR::isError( $e = $this->smtp->connect()))->false($e->getMessage()); - { - dumpvar(__METHOD__.__LINE__.PHP_EOL); - die($e->getMessage() . "\n"); + $this->action = __FUNCTION__; + Assert::that($this->smtp->connect()) + ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'can not connect'); + } + + /** + * @throws InvalidArgumentException + * @return void + */ + protected function auth() + { + $this->action = __FUNCTION__; + Assert::that($this->smtp->auth($this->user, $this->pwd)) + ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'auth failed'); + } + + 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.'"'); + } + + 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.'"'); + } + + protected function sendContent() + { + 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'); + } + + protected function disconnect() + { + $this->action = __FUNCTION__; + Assert::that($this->smtp->disconnect()) + ->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to disconnect'); + } + + public function dumpDebug($smtp, $message) + { + unset($smtp); + + if (!isset($this->log[$this->action])) { + $this->log[$this->action] = []; + } + $this->log[$this->action][] = trim(htmlentities($message)); + } + + public function formatMessage(array $arguments) + { + /** @var PEAR_Error|true $response */ + $response = $arguments['value']; + $propertyPath = $arguments['propertyPath']; + if (PEAR::isError($response)) { + return ($propertyPath ? $propertyPath .' - ' : ''). + $response->getMessage() . + ($response->getCode() ? ': '.$response->getCode() : ''); + } + + return null; } } \ No newline at end of file diff --git a/Application/views/admin/tpl/smtpCheck.tpl b/Application/views/admin/tpl/smtpCheck.tpl new file mode 100644 index 0000000..b28034b --- /dev/null +++ b/Application/views/admin/tpl/smtpCheck.tpl @@ -0,0 +1,228 @@ +[{include file="headitem.tpl" title="GENERAL_ADMIN_TITLE"|oxmultilangassign}] + + + +
+ [{$oViewConf->getHiddenSid()}] + + + + +
+ +

SMTP Test

+ +
+
+ [{$oViewConf->getHiddenSid()}] + + + + + + +
+ + + + +
+ + + + +
+ + +
+ + +
+ +
+
+
+ [{foreach from=$smtpLog key="action" item="logItems" name="actionList"}] +
+
[{$smarty.foreach.actionList.iteration}] - [{$action}]
+
+
    + [{foreach from=$logItems item="logItem"}] +
  • + [{$logItem}] +
  • + [{/foreach}] +
+
+
+ [{/foreach}] +
+ + + +[{*
*}] +[{* [{$oViewConf->getHiddenSid()}]*}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] + +[{* *}] +[{* [{if $edit->getId() && !$edit->getLicenseActive()}]*}] +[{* *}] +[{* *}] +[{* *}] +[{* [{/if}]*}] + +[{* *}] +[{* *}] +[{* *}] + +[{* [{if $edit->getBasicRestrictionSettings()}]*}] +[{* *}] +[{* *}] +[{* *}] +[{* [{/if}]*}] + +[{* *}] +[{* *}] + +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{*
*}] +[{*
[{oxmultilang ident="D3_ORDERMANAGER_ERROR_IEXECJOBSLIMIT"}]
*}] +[{*
*}] +[{*
*}] +[{* [{oxmultilang ident="D3_GENERAL_ORDERMANAGER_DESCRIPTION"}]*}] +[{* [{oxmultilang ident="d3tbclordermanager_items_main_desc"}]*}] +[{*
*}] +[{*
*}] +[{* [{oxmultilang ident="D3_GENERAL_ORDERMANAGER_BASICRESTRICTIONS"}]*}] +[{*
*}] + +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] + +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* [{if $blShowLangSwitch}]*}] +[{* *}] +[{* *}] +[{* *}] +[{* [{/if}]*}] +[{*
*}] +[{*  *}] +[{* *}] +[{* *}] +[{*
*}] +[{*  *}] +[{* *}] +[{* *}] +[{*
*}] +[{*  *}] +[{* *}] +[{* *}] +[{* [{oxinputhelp ident="D3_ORDERMANAGER_MAIN_SORT_DESC"}]*}] +[{*
*}] +[{*  *}] +[{* *}] +[{* *}] +[{* [{oxinputhelp ident="D3_ORDERMANAGER_MAIN_FOLDER_DESC"}]*}] +[{*
*}] +[{* [{oxmultilang ident="D3_ORDERMANAGER_MAIN_LASTEXEC"}] *}] +[{* *}] +[{* [{$edit->getValue('iLastExecDate')|date_format:"%Y-%m-%d %H:%M:%S"}]*}] +[{*

*}] +[{* [{include file="language_edit.tpl"}]
*}] +[{*
*}] + +[{* *}] +[{* [{include file="d3_modprofile_actionbuttons.tpl"}]*}] +[{*
*}] +[{*
*}] +[{*
*}] +[{* [{oxmultilang ident="D3_ORDERMANAGER_MAIN_FIELDSET_EXECUTION"}]*}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{* *}] +[{*
*}] +[{* *}] +[{* *}] +[{* *}] +[{* hasDebugMode()}]disabled[{/if}] class="edittext ext_edittext" name="value[blItemExecute]" value="0" [{if false == $edit->getValue('blItemExecute')}]checked[{/if}] [{$readonly}]>*}] +[{* [{oxinputhelp ident="D3_ORDERMANAGER_MAIN_EXECUTE_DESC"}]*}] +[{*
*}] +[{*  *}] +[{* *}] +[{* *}] +[{* getValue('blItemMailSend')}]checked[{/if}] [{$readonly}]>*}] +[{* [{oxinputhelp ident="D3_ORDERMANAGER_MAIN_MAILSEND_DESC"}]*}] +[{*
*}] +[{* *}] +[{* *}] +[{* *}] +[{* getValue('blAddHistory')}]checked[{/if}] [{$readonly}]>*}] +[{* [{oxinputhelp ident="D3_ORDERMANAGER_MAIN_ADDHISTORYITEM_DESC"}]*}] +[{*
*}] +[{*
*}] +[{*
*}] +[{*
*}] + +[{include file="bottomnaviitem.tpl"}] + +[{include file="bottomitem.tpl"}] diff --git a/composer.json b/composer.json index c345178..860bea8 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "d3/smtpchecker", - "description": "SMTP checker", + "name": "d3/mailconfigchecker", + "description": "Mail configuration check tool", "type": "oxideshop-module", "keywords": [ "oxid", @@ -46,12 +46,12 @@ "*.xml", "*.neon" ], - "target-directory": "d3/smtpchecker" + "target-directory": "d3/mailconfigchecker" } }, "autoload": { "psr-4": { - "D3\\SmtpChecker\\": "../../../source/modules/d3/smtpchecker" + "D3\\MailConfigChecker\\": "../../../source/modules/d3/mailconfigchecker" } }, "scripts": { diff --git a/de/translations.php b/de/translations.php new file mode 100644 index 0000000..998aa50 --- /dev/null +++ b/de/translations.php @@ -0,0 +1,18 @@ + + * @link http://www.oxidmodule.com + */ + +return [ + 'MXMAILCHECK' => "Mail Check" +]; \ No newline at end of file diff --git a/menu.xml b/menu.xml index dc14f59..80dcb36 100644 --- a/menu.xml +++ b/menu.xml @@ -2,7 +2,11 @@ - + + + + + \ No newline at end of file diff --git a/metadata.php b/metadata.php index c1cbf39..2f8f924 100644 --- a/metadata.php +++ b/metadata.php @@ -17,7 +17,7 @@ use D3\SmtpChecker\Application\Controller\Admin\SmtpChecker; $sMetadataVersion = '2.1'; -$sModuleId = 'd3smtpchecker'; +$sModuleId = 'd3mailconfigchecker'; $logo = '(D3)'; /** @@ -25,7 +25,7 @@ $logo = '