2023-12-08 15:16:48 +01:00
|
|
|
<?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
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-12-11 10:38:59 +01:00
|
|
|
namespace D3\MailConfigChecker\Application\Controller\Admin;
|
2023-12-08 15:16:48 +01:00
|
|
|
|
|
|
|
use Assert\Assert;
|
|
|
|
use Assert\InvalidArgumentException;
|
2024-05-31 16:31:25 +02:00
|
|
|
use D3\MailConfigChecker\Application\Model\Constants;
|
2023-12-08 15:16:48 +01:00
|
|
|
use Net_SMTP;
|
|
|
|
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
|
|
|
|
use OxidEsales\Eshop\Application\Model\Shop;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
|
|
use PEAR;
|
2023-12-09 13:32:01 +01:00
|
|
|
use PEAR_Error;
|
2023-12-08 15:16:48 +01:00
|
|
|
|
|
|
|
class SmtpChecker extends AdminDetailsController
|
|
|
|
{
|
2024-06-04 14:37:41 +02:00
|
|
|
protected bool $debug = true;
|
2023-12-08 15:16:48 +01:00
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected string $host;
|
|
|
|
protected int $port;
|
|
|
|
protected string $user;
|
|
|
|
protected string $pwd;
|
|
|
|
protected string $from;
|
|
|
|
protected ?string $to = null;
|
2023-12-08 15:16:48 +01:00
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected Net_SMTP $smtp;
|
|
|
|
protected string $action = '';
|
|
|
|
protected array $log = [];
|
2023-12-08 15:16:48 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
/** @var Shop $activeShop */
|
|
|
|
$activeShop = Registry::getConfig()->getActiveShop();
|
|
|
|
|
2023-12-12 11:28:18 +01:00
|
|
|
if ($localHost = Registry::getRequest()->getRequestEscapedParameter('smtpHost')) {
|
2023-12-12 11:50:41 +01:00
|
|
|
['host' => $shopHost, 'port' => $shopPort] = parse_url(trim($localHost));
|
2023-12-12 11:28:18 +01:00
|
|
|
} else {
|
2023-12-12 11:50:41 +01:00
|
|
|
['host' => $shopHost, 'port' => $shopPort] = parse_url(trim($activeShop->getFieldData('oxsmtp')));
|
2023-12-12 11:28:18 +01:00
|
|
|
}
|
2023-12-09 13:32:01 +01:00
|
|
|
|
2023-12-12 11:43:46 +01:00
|
|
|
$this->host = $shopHost;
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->addTplParam('smtpHost', Registry::getRequest()->getRequestEscapedParameter('smtpHost'));
|
2023-12-12 11:43:46 +01:00
|
|
|
$this->port = $shopPort;
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->user = Registry::getRequest()->getRequestEscapedParameter('smtpUser') ?: $activeShop->getFieldData('oxsmtpuser');
|
2023-12-12 11:28:18 +01:00
|
|
|
$this->addTplParam('smtpUser', $this->user);
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->pwd = Registry::getRequest()->getRequestEscapedParameter('smtpPwd') ?: $activeShop->getFieldData('oxsmtppwd');
|
2023-12-12 11:28:18 +01:00
|
|
|
$this->addTplParam('smtpPwd', $this->pwd);
|
|
|
|
$this->from = Registry::getRequest()->getRequestEscapedParameter('from') ?: '';
|
|
|
|
$this->addTplParam('from', $this->from);
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->to = Registry::getRequest()->getRequestEscapedParameter('to') ?: '';
|
2023-12-12 11:28:18 +01:00
|
|
|
$this->addTplParam('recipient', $this->to);
|
|
|
|
$this->addTplParam('sendMail', Registry::getRequest()->getRequestEscapedParameter('sendmail'));
|
2023-12-09 13:32:01 +01:00
|
|
|
|
|
|
|
$this->addTplParam('smtpLog', $this->log);
|
|
|
|
}
|
2023-12-08 15:16:48 +01:00
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function getTemplateName(): string
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
2024-05-31 16:31:25 +02:00
|
|
|
return '@'.Constants::OXID_MODULE_ID.'/admin/smtpCheck';
|
2023-12-09 13:32:01 +01:00
|
|
|
}
|
2023-12-08 15:16:48 +01:00
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function render(): ?string
|
2023-12-12 11:28:18 +01:00
|
|
|
{
|
|
|
|
$this->addTplParam('shop', Registry::getConfig()->getActiveShop());
|
|
|
|
|
|
|
|
return parent::render();
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function getMailAddressList(): array
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
2023-12-12 11:43:46 +01:00
|
|
|
/** @var Shop $shop */
|
2023-12-09 13:32:01 +01:00
|
|
|
$shop = Registry::getConfig()->getActiveShop();
|
|
|
|
|
|
|
|
return array_filter(
|
|
|
|
array_unique(
|
|
|
|
[
|
|
|
|
$shop->getFieldData('oxinfoemail'),
|
|
|
|
$shop->getFieldData('oxorderemail'),
|
|
|
|
$shop->getFieldData('oxowneremail'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-12-08 15:16:48 +01:00
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function sendMail(): void
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->hostIsAvailable();
|
|
|
|
$this->connect();
|
|
|
|
$this->auth();
|
|
|
|
$this->setFrom();
|
|
|
|
$this->setRecipient();
|
|
|
|
$this->sendContent();
|
2023-12-12 11:28:18 +01:00
|
|
|
$this->addTplParam('success', true);
|
2023-12-09 13:32:01 +01:00
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
Registry::getUtilsView()->addErrorToDisplay($e);
|
|
|
|
} finally {
|
|
|
|
$this->disconnect();
|
2023-12-08 15:16:48 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->addTplParam('smtpLog', $this->log);
|
2023-12-08 15:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function hostIsAvailable(): void
|
2023-12-08 15:16:48 +01:00
|
|
|
{
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->action = __FUNCTION__;
|
2023-12-12 11:28:18 +01:00
|
|
|
Assert::that(
|
2023-12-12 11:50:41 +01:00
|
|
|
($this->smtp = new Net_SMTP($this->host, $this->port)),
|
2023-12-12 11:28:18 +01:00
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_INSTANCE')
|
|
|
|
)->isInstanceOf(
|
|
|
|
Net_SMTP::class,
|
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOCONNECTION')
|
|
|
|
);
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->smtp->setDebug($this->debug, [$this, 'dumpDebug']);
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function connect(): void
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
|
|
|
$this->action = __FUNCTION__;
|
2023-12-12 11:28:18 +01:00
|
|
|
Assert::that(
|
|
|
|
$this->smtp->connect(),
|
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_CONNECTION')
|
|
|
|
)->notIsInstanceOf(
|
|
|
|
PEAR_Error::class,
|
|
|
|
[$this, 'formatMessage'],
|
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOCONNECTION')
|
|
|
|
);
|
2023-12-09 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function auth(): void
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
|
|
|
$this->action = __FUNCTION__;
|
2023-12-12 11:28:18 +01:00
|
|
|
Assert::that(
|
|
|
|
$this->smtp->auth($this->user, $this->pwd),
|
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_AUTHENTICATION')
|
|
|
|
)->notIsInstanceOf(
|
|
|
|
PEAR_Error::class,
|
|
|
|
[$this, 'formatMessage'],
|
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOAUTHENTICATION')
|
|
|
|
);
|
2023-12-09 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function setFrom(): void
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
|
|
|
$this->action = __FUNCTION__;
|
2023-12-12 11:28:18 +01:00
|
|
|
Assert::that(
|
|
|
|
$this->smtp->mailFrom($this->from),
|
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_SENDER')
|
|
|
|
)->notIsInstanceOf(
|
|
|
|
PEAR_Error::class,
|
|
|
|
[$this, 'formatMessage'],
|
|
|
|
sprintf(Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOSENDER'), $this->from)
|
|
|
|
);
|
2023-12-09 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function setRecipient(): void
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
2024-06-04 14:37:41 +02:00
|
|
|
if (!$this->to) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-09 13:32:01 +01:00
|
|
|
$this->action = __FUNCTION__;
|
2023-12-12 11:28:18 +01:00
|
|
|
Assert::that(
|
2023-12-12 11:50:41 +01:00
|
|
|
$this->smtp->rcptTo($this->to),
|
2023-12-12 11:28:18 +01:00
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_RECIPIENT')
|
|
|
|
)->notIsInstanceOf(
|
|
|
|
PEAR_Error::class,
|
|
|
|
[$this, 'formatMessage'],
|
|
|
|
sprintf(Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NORECIPIENT'), $this->to)
|
|
|
|
);
|
2023-12-08 15:16:48 +01:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function sendContent(): void
|
2023-12-08 15:16:48 +01:00
|
|
|
{
|
2023-12-12 11:28:18 +01:00
|
|
|
if (!Registry::getRequest()->getRequestEscapedParameter('sendmail')) {
|
2023-12-09 13:32:01 +01:00
|
|
|
return;
|
2023-12-08 15:16:48 +01:00
|
|
|
}
|
2023-12-09 13:32:01 +01:00
|
|
|
|
|
|
|
$this->action = __FUNCTION__;
|
|
|
|
$subj = "Subject: Test Message\n";
|
|
|
|
$body = "Body Line 1\nBody Line 2";
|
2023-12-12 11:28:18 +01:00
|
|
|
Assert::that(
|
2023-12-12 11:50:41 +01:00
|
|
|
$this->smtp->data($subj . "\r\n" . $body),
|
2023-12-12 11:28:18 +01:00
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_TRANSMIT')
|
|
|
|
)->notIsInstanceOf(
|
|
|
|
PEAR_Error::class,
|
|
|
|
[$this, 'formatMessage'],
|
|
|
|
Registry::getLang()->translateString('D3_MAILCHECKER_SMTPCHECK_NOTRANSMIT')
|
|
|
|
);
|
2023-12-09 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
protected function disconnect(): void
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
|
|
|
$this->action = __FUNCTION__;
|
2023-12-12 11:28:18 +01:00
|
|
|
$this->smtp->disconnect();
|
2023-12-09 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function dumpDebug($smtp, $message): void
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
|
|
|
unset($smtp);
|
|
|
|
|
|
|
|
if (!isset($this->log[$this->action])) {
|
|
|
|
$this->log[$this->action] = [];
|
|
|
|
}
|
|
|
|
$this->log[$this->action][] = trim(htmlentities($message));
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:37:41 +02:00
|
|
|
public function formatMessage(array $arguments): ?string
|
2023-12-09 13:32:01 +01:00
|
|
|
{
|
|
|
|
/** @var PEAR_Error|true $response */
|
|
|
|
$response = $arguments['value'];
|
|
|
|
$propertyPath = $arguments['propertyPath'];
|
|
|
|
if (PEAR::isError($response)) {
|
|
|
|
return ($propertyPath ? $propertyPath .' - ' : '').
|
|
|
|
$response->getMessage() .
|
|
|
|
($response->getCode() ? ': '.$response->getCode() : '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2023-12-08 15:16:48 +01:00
|
|
|
}
|
2023-12-12 11:50:41 +01:00
|
|
|
}
|