8
0
MailConfigChecker/Application/Controller/Admin/MailTester.php

96 Zeilen
3.4 KiB
PHP

2023-12-09 13:32:01 +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-09 13:32:01 +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-09 13:32:01 +01:00
*/
2023-12-11 10:38:59 +01:00
namespace D3\MailConfigChecker\Application\Controller\Admin;
2023-12-12 11:28:18 +01:00
use Assert\Assert;
2024-05-31 16:31:25 +02:00
use D3\MailConfigChecker\Application\Model\Constants;
2023-12-12 11:28:18 +01:00
use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException;
2024-06-04 14:37:41 +02:00
use Exception;
2023-12-11 10:38:59 +01:00
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
2023-12-12 11:43:46 +01:00
use OxidEsales\Eshop\Application\Model\Shop;
2023-12-12 11:28:18 +01:00
use OxidEsales\Eshop\Core\Email;
use OxidEsales\Eshop\Core\Registry;
2023-12-11 10:38:59 +01:00
class MailTester extends AdminDetailsController
{
2024-05-31 16:31:25 +02:00
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailTester';
2023-12-12 11:28:18 +01:00
2024-06-04 14:37:41 +02:00
public function sendMail(): void
2023-12-12 11:28:18 +01:00
{
try {
$request = Registry::getRequest();
2023-12-12 11:50:41 +01:00
$from = trim($request->getRequestEscapedParameter('from'));
2023-12-12 11:28:18 +01:00
$this->addTplParam('sender', $from);
2023-12-12 11:50:41 +01:00
$to = trim($request->getRequestEscapedParameter('to'));
2023-12-12 11:28:18 +01:00
$this->addTplParam('recipient', $to);
2023-12-12 11:50:41 +01:00
$subject = trim($request->getRequestEscapedParameter('subject'));
2023-12-12 11:28:18 +01:00
$this->addTplParam('subject', $subject);
2023-12-12 11:50:41 +01:00
$body = trim($request->getRequestEscapedParameter('body'));
2023-12-12 11:28:18 +01:00
$this->addTplParam('body', $body);
$this->assertContent();
2023-12-12 11:50:41 +01:00
$mail = oxNew(Email::class);
$mail->setFrom($from);
2023-12-12 11:28:18 +01:00
$mail->sendEmail($to, $subject, $body);
$this->addTplParam('success', true);
2024-08-22 10:11:08 +02:00
} catch (Exception $e) {
2023-12-12 11:28:18 +01:00
Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage()));
}
}
2024-06-04 14:37:41 +02:00
protected function assertContent(): void
2023-12-12 11:28:18 +01:00
{
$request = Registry::getRequest();
$lang = Registry::getLang();
Assert::lazy()
->setExceptionClass(d3TranslatableLazyAssertionException::class)
->that(
2023-12-12 11:50:41 +01:00
$request->getRequestEscapedParameter('from'),
2023-12-12 11:28:18 +01:00
$lang->translateString('D3_MAILCHECKER_SMTPCHECK_SENDER')
)->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
->that(
2023-12-12 11:50:41 +01:00
$request->getRequestEscapedParameter('to'),
2023-12-12 11:28:18 +01:00
$lang->translateString('D3_MAILCHECKER_SMTPCHECK_RECIPIENT')
)->email($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
->that(
2023-12-12 11:50:41 +01:00
$request->getRequestEscapedParameter('subject'),
2023-12-12 11:28:18 +01:00
$lang->translateString('D3_MAILCHECKER_TESTMAIL_SUBJECT')
)->notBlank($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
->that(
2023-12-12 11:50:41 +01:00
$request->getRequestEscapedParameter('body'),
2023-12-12 11:28:18 +01:00
$lang->translateString('D3_MAILCHECKER_TESTMAIL_BODY')
)->notBlank($lang->translateString('D3_MAILCHECKER_ASSERTIONS_NOTSET'))
->verifyNow();
}
2024-06-04 14:37:41 +02:00
public function getMailAddressList(): array
2023-12-12 11:28:18 +01:00
{
2023-12-12 11:43:46 +01:00
/** @var Shop $shop */
2023-12-12 11:28:18 +01:00
$shop = Registry::getConfig()->getActiveShop();
return array_filter(
array_unique(
[
$shop->getFieldData('oxinfoemail'),
$shop->getFieldData('oxorderemail'),
$shop->getFieldData('oxowneremail'),
]
)
);
}
2023-12-12 11:50:41 +01:00
}