MailConfigChecker/Application/Controller/Admin/MailTester.php

96 regels
3.5 KiB
PHP

2023-12-09 13:32:01 +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
*/
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;
use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException;
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
{
protected $_sThisTemplate = 'mailTester.tpl';
2023-12-12 11:28:18 +01:00
public function sendMail()
{
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);
} catch (\Exception $e) {
Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage()));
}
}
protected function assertContent()
{
$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();
}
public function getMailAddressList()
{
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
}