195 Zeilen
6.5 KiB
PHP
195 Zeilen
6.5 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
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace D3\SmtpChecker\Application\Controller\Admin;
|
|
|
|
use Assert\Assert;
|
|
use Assert\InvalidArgumentException;
|
|
use Net_SMTP;
|
|
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
|
|
use OxidEsales\Eshop\Application\Model\Shop;
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
use PEAR;
|
|
use PEAR_Error;
|
|
|
|
class SmtpChecker extends AdminDetailsController
|
|
{
|
|
protected bool $debug = true;
|
|
|
|
protected string $host;
|
|
protected string $user;
|
|
protected string $from;
|
|
protected string $to;
|
|
|
|
protected Net_SMTP $smtp;
|
|
protected string $action;
|
|
protected array $log = [];
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
/** @var Shop $activeShop */
|
|
$activeShop = Registry::getConfig()->getActiveShop();
|
|
|
|
list('host' => $shopHost, 'port' => $shopPort) = parse_url($activeShop->getFieldData('oxsmtp'));
|
|
|
|
$this->host = Registry::getRequest()->getRequestEscapedParameter('smtpHost') ?: $shopHost;
|
|
$this->addTplParam('smtpHost', Registry::getRequest()->getRequestEscapedParameter('smtpHost'));
|
|
$this->port = Registry::getRequest()->getRequestEscapedParameter('smtpHost') ? (Registry::getRequest()->getRequestEscapedParameter('smtpPort') ?: null) : $shopPort;
|
|
$this->addTplParam('smtpPort', Registry::getRequest()->getRequestEscapedParameter('smtpPort'));
|
|
$this->user = Registry::getRequest()->getRequestEscapedParameter('smtpUser') ?: $activeShop->getFieldData('oxsmtpuser');
|
|
$this->addTplParam('smtpUser', Registry::getRequest()->getRequestEscapedParameter('smtpUser'));
|
|
$this->pwd = Registry::getRequest()->getRequestEscapedParameter('smtpPwd') ?: $activeShop->getFieldData('oxsmtppwd');
|
|
$this->addTplParam('smtpPwd', Registry::getRequest()->getRequestEscapedParameter('smtpPwd'));
|
|
$this->from = Registry::getRequest()->getRequestEscapedParameter('from') ?: Registry::getRequest()->getRequestEscapedParameter('fromCust') ?: '';
|
|
$this->addTplParam('from', Registry::getRequest()->getRequestEscapedParameter('from'));
|
|
$this->addTplParam('fromCust', Registry::getRequest()->getRequestEscapedParameter('fromCust'));
|
|
$this->to = Registry::getRequest()->getRequestEscapedParameter('to') ?: '';
|
|
|
|
$this->addTplParam('smtpLog', $this->log);
|
|
}
|
|
|
|
public function getTemplateName()
|
|
{
|
|
return 'smtpCheck.tpl';
|
|
}
|
|
|
|
public function getMailAddressList()
|
|
{
|
|
$shop = Registry::getConfig()->getActiveShop();
|
|
|
|
return array_filter(
|
|
array_unique(
|
|
[
|
|
$shop->getFieldData('oxinfoemail'),
|
|
$shop->getFieldData('oxorderemail'),
|
|
$shop->getFieldData('oxowneremail'),
|
|
]
|
|
)
|
|
);
|
|
}
|
|
|
|
public function sendMail()
|
|
{
|
|
try {
|
|
$this->checkDataAreSet();
|
|
$this->hostIsAvailable();
|
|
$this->connect();
|
|
$this->auth();
|
|
$this->setFrom();
|
|
$this->setRecipient();
|
|
$this->sendContent();
|
|
} catch (InvalidArgumentException $e) {
|
|
Registry::getUtilsView()->addErrorToDisplay($e);
|
|
} finally {
|
|
$this->disconnect();
|
|
}
|
|
|
|
$this->addTplParam('smtpLog', $this->log);
|
|
}
|
|
|
|
protected function checkDataAreSet()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidArgumentException
|
|
* @return void
|
|
*/
|
|
protected function hostIsAvailable()
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
Assert::that(( $this->smtp = new Net_SMTP( $this->host, $this->port ) ))
|
|
->isInstanceOf(Net_SMTP::class, 'Unable to instantiate SMTP object');
|
|
$this->smtp->setDebug($this->debug, [$this, 'dumpDebug']);
|
|
}
|
|
|
|
protected function connect()
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
Assert::that($this->smtp->connect())
|
|
->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'can not connect');
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidArgumentException
|
|
* @return void
|
|
*/
|
|
protected function auth()
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
Assert::that($this->smtp->auth($this->user, $this->pwd))
|
|
->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'auth failed');
|
|
}
|
|
|
|
protected function setFrom()
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
Assert::that($this->smtp->mailFrom($this->from))
|
|
->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to set sender "'.$this->from.'"');
|
|
}
|
|
|
|
protected function setRecipient()
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
Assert::that(( $this->smtp->rcptTo( $this->to)))
|
|
->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to set recipient "'.$this->to.'"');
|
|
}
|
|
|
|
protected function sendContent()
|
|
{
|
|
if (!Registry::getRequest()->getRequestEscapedParameter('sendMail')) {
|
|
return;
|
|
}
|
|
|
|
$this->action = __FUNCTION__;
|
|
$subj = "Subject: Test Message\n";
|
|
$body = "Body Line 1\nBody Line 2";
|
|
Assert::that($this->smtp->data( $subj . "\r\n" . $body))
|
|
->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to send content');
|
|
}
|
|
|
|
protected function disconnect()
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
Assert::that($this->smtp->disconnect())
|
|
->notIsInstanceOf(PEAR_Error::class, [$this, 'formatMessage'], 'unable to disconnect');
|
|
}
|
|
|
|
public function dumpDebug($smtp, $message)
|
|
{
|
|
unset($smtp);
|
|
|
|
if (!isset($this->log[$this->action])) {
|
|
$this->log[$this->action] = [];
|
|
}
|
|
$this->log[$this->action][] = trim(htmlentities($message));
|
|
}
|
|
|
|
public function formatMessage(array $arguments)
|
|
{
|
|
/** @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;
|
|
}
|
|
} |