MailConfigChecker/Application/Controller/Admin/MailConfigCheck.php

85 lines
3.4 KiB
PHP
Raw Normal View History

2023-12-11 10:38:59 +01:00
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
*/
namespace D3\MailConfigChecker\Application\Controller\Admin;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException;
2023-12-11 15:48:31 +01:00
use Exception;
2023-12-11 10:38:59 +01:00
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Email;
use OxidEsales\Eshop\Core\Registry;
class MailConfigCheck extends AdminDetailsController
{
protected $_sThisTemplate = 'mailConfigCheck.tpl';
2023-12-11 15:48:31 +01:00
protected $testMailAddress = 'test@example.com';
2023-12-11 10:38:59 +01:00
public function render()
{
$this->checkDataAreSet();
$this->addTplParam('shop', Registry::getConfig()->getActiveShop());
return parent::render();
}
protected function checkDataAreSet()
{
try {
/** @var Shop $shop */
$shop = Registry::getConfig()->getActiveShop();
$lang = Registry::getLang();
Assert::lazy()
->setExceptionClass(d3TranslatableLazyAssertionException::class)
->that( $shop->getFieldData( 'oxsmtp' ), $lang->translateString('SHOP_MAIN_SMTPSERVER') )
2023-12-12 11:28:18 +01:00
->notBlank( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') )
->regex( '/.*:(587|2525)$/m', $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOPORT') )
2023-12-11 10:38:59 +01:00
->that( $shop->getFieldData( 'oxsmtpuser' ), $lang->translateString('SHOP_MAIN_SMTPUSER') )
2023-12-12 11:28:18 +01:00
->notBlank( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') )
2023-12-11 10:38:59 +01:00
->that( $shop->getFieldData( 'oxsmtppwd' ), $lang->translateString('SHOP_MAIN_SMTPPASSWORD') )
2023-12-12 11:28:18 +01:00
->notBlank( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') )
2023-12-11 10:38:59 +01:00
->that( $shop->getFieldData( 'oxinfoemail' ), $lang->translateString('SHOP_MAIN_INFOEMAIL') )
2023-12-12 11:28:18 +01:00
->email( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') )
2023-12-11 10:38:59 +01:00
->that( $shop->getFieldData( 'oxorderemail' ), $lang->translateString('SHOP_MAIN_ORDEREMAIL') )
2023-12-12 11:28:18 +01:00
->email( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') )
2023-12-11 10:38:59 +01:00
->that( $shop->getFieldData( 'oxowneremail' ), $lang->translateString('SHOP_MAIN_OWNEREMAIL') )
2023-12-12 11:28:18 +01:00
->email( $lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET') )
2023-12-11 10:38:59 +01:00
->verifyNow();
} catch (InvalidArgumentException $e) {
Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage()));
}
}
public function checkConfiguration()
{
2023-12-11 15:48:31 +01:00
$this->getCurrentMailer();
2023-12-11 10:38:59 +01:00
}
2023-12-11 15:48:31 +01:00
protected function getCurrentMailer()
2023-12-11 10:38:59 +01:00
{
try {
$mail = oxNew( Email::class );
2023-12-11 15:48:31 +01:00
$mail->setRecipient( $this->testMailAddress );
2023-12-11 10:38:59 +01:00
$mail->setBody('.');
$mail->send();
2023-12-11 15:48:31 +01:00
$this->addTplParam('mailer', $mail->getMailer());
} catch ( Exception $e) {
Registry::getUtilsView()->addErrorToDisplay($e);
2023-12-11 10:38:59 +01:00
}
}
}