MailConfigChecker/Application/Controller/Admin/MailTester.php
2024-06-04 14:37:41 +02:00

96 lines
3.4 KiB
PHP

<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
namespace D3\MailConfigChecker\Application\Controller\Admin;
use Assert\Assert;
use D3\MailConfigChecker\Application\Model\Constants;
use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException;
use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Email;
use OxidEsales\Eshop\Core\Registry;
class MailTester extends AdminDetailsController
{
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailTester';
public function sendMail(): void
{
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(): void
{
$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(): array
{
/** @var Shop $shop */
$shop = Registry::getConfig()->getActiveShop();
return array_filter(
array_unique(
[
$shop->getFieldData('oxinfoemail'),
$shop->getFieldData('oxorderemail'),
$shop->getFieldData('oxowneremail'),
]
)
);
}
}