2023-12-11 10:38:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2024-06-03 07:57:58 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* https://www.d3data.de
|
2023-12-11 10:38:59 +01:00
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
2024-06-03 07:57:58 +02:00
|
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
2023-12-11 10:38:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\MailConfigChecker\Application\Controller\Admin;
|
|
|
|
|
|
|
|
use Assert\Assert;
|
|
|
|
use Assert\InvalidArgumentException;
|
2024-05-31 16:31:25 +02:00
|
|
|
use D3\MailConfigChecker\Application\Model\Constants;
|
2023-12-11 10:38:59 +01:00
|
|
|
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
|
|
|
|
{
|
2024-05-31 16:31:25 +02:00
|
|
|
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailConfigCheck';
|
2024-08-22 09:56:01 +02:00
|
|
|
protected string $testMailAddress = 'test@example.tld';
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->setSmtpCredentials();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setSmtpCredentials(): void
|
|
|
|
{
|
|
|
|
$shop = Registry::getConfig()->getActiveShop();
|
|
|
|
|
|
|
|
$this->addTplParam('smtpHost', $shop->getFieldData('oxsmtp'));
|
|
|
|
$this->addTplParam('smtpUser', $shop->getFieldData('oxsmtpuser'));
|
|
|
|
}
|
2023-12-11 10:38:59 +01:00
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function render(): string
|
2023-12-11 10:38:59 +01:00
|
|
|
{
|
|
|
|
$this->checkDataAreSet();
|
|
|
|
$this->addTplParam('shop', Registry::getConfig()->getActiveShop());
|
2024-07-09 23:33:17 +02:00
|
|
|
$this->addTplParam('recipient', $this->testMailAddress);
|
2023-12-11 10:38:59 +01:00
|
|
|
|
|
|
|
return parent::render();
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function checkDataAreSet(): void
|
2023-12-11 10:38:59 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
/** @var Shop $shop */
|
|
|
|
$shop = Registry::getConfig()->getActiveShop();
|
|
|
|
$lang = Registry::getLang();
|
|
|
|
|
|
|
|
Assert::lazy()
|
|
|
|
->setExceptionClass(d3TranslatableLazyAssertionException::class)
|
2023-12-12 11:50:41 +01:00
|
|
|
->that($shop->getFieldData('oxsmtp'), $lang->translateString('SHOP_MAIN_SMTPSERVER'))
|
|
|
|
->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_MAILCHECKER_ASSERTIONS_NOTSET'))
|
|
|
|
->that($shop->getFieldData('oxsmtppwd'), $lang->translateString('SHOP_MAIN_SMTPPASSWORD'))
|
|
|
|
->notBlank($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
|
|
|
|
->that($shop->getFieldData('oxinfoemail'), $lang->translateString('SHOP_MAIN_INFOEMAIL'))
|
|
|
|
->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
|
|
|
|
->that($shop->getFieldData('oxorderemail'), $lang->translateString('SHOP_MAIN_ORDEREMAIL'))
|
|
|
|
->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
|
|
|
|
->that($shop->getFieldData('oxowneremail'), $lang->translateString('SHOP_MAIN_OWNEREMAIL'))
|
|
|
|
->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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function checkConfiguration(): void
|
2023-12-11 10:38:59 +01:00
|
|
|
{
|
2023-12-11 15:48:31 +01:00
|
|
|
$this->getCurrentMailer();
|
2023-12-11 10:38:59 +01:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function getCurrentMailer(): void
|
2023-12-11 10:38:59 +01:00
|
|
|
{
|
2024-08-22 11:06:35 +02:00
|
|
|
set_error_handler(
|
|
|
|
function ($errno, $errstr, $errfile, $errline) {
|
|
|
|
throw oxNew(Exception::class, $errstr.' in '.$errfile.'('.$errline.')', $errno);
|
|
|
|
},
|
|
|
|
E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
|
|
|
|
);
|
|
|
|
|
2023-12-11 10:38:59 +01:00
|
|
|
try {
|
2024-08-22 10:11:08 +02:00
|
|
|
$shop = Registry::getConfig()->getActiveShop();
|
2024-08-01 10:42:41 +02:00
|
|
|
|
2023-12-12 11:50:41 +01:00
|
|
|
$mail = oxNew(Email::class);
|
2024-07-09 23:33:17 +02:00
|
|
|
$mail->setRecipient(
|
|
|
|
trim(Registry::getRequest()->getRequestEscapedParameter('recipient')) ?: $this->testMailAddress
|
|
|
|
);
|
2023-12-11 10:38:59 +01:00
|
|
|
$mail->setBody('.');
|
2024-08-22 10:11:08 +02:00
|
|
|
$mail->setFrom($shop->getFieldData('oxowneremail'));
|
2024-07-09 23:33:17 +02:00
|
|
|
|
2024-08-01 10:42:41 +02:00
|
|
|
$mail->set("SMTPDebug", true); // don't set via iDebug = 6 because different handling
|
2024-07-09 23:33:17 +02:00
|
|
|
$mail->setSmtp();
|
|
|
|
|
|
|
|
ob_start();
|
2023-12-11 10:38:59 +01:00
|
|
|
$mail->send();
|
2024-07-09 23:33:17 +02:00
|
|
|
$communication = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
$re = '/(^|\<br\>)(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s)/m';
|
|
|
|
$subst = "$1";
|
|
|
|
$communication = preg_replace($re, $subst, $communication);
|
2023-12-11 15:48:31 +01:00
|
|
|
$this->addTplParam('mailer', $mail->getMailer());
|
2024-07-09 23:33:17 +02:00
|
|
|
$this->addTplParam('communication', $communication);
|
2023-12-12 11:50:41 +01:00
|
|
|
} catch (Exception $e) {
|
2024-07-10 08:50:18 +02:00
|
|
|
ob_end_clean();
|
2023-12-11 15:48:31 +01:00
|
|
|
Registry::getUtilsView()->addErrorToDisplay($e);
|
2023-12-11 10:38:59 +01:00
|
|
|
}
|
2024-08-22 11:06:35 +02:00
|
|
|
|
|
|
|
restore_error_handler();
|
2023-12-11 10:38:59 +01:00
|
|
|
}
|
2023-12-12 11:50:41 +01:00
|
|
|
}
|