first commit
This commit is contained in:
72
bundles/KupShop/KupShopBundle/Mailer/TransportFactory.php
Normal file
72
bundles/KupShop/KupShopBundle/Mailer/TransportFactory.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KupShop\KupShopBundle\Mailer;
|
||||
|
||||
use KupShop\KupShopBundle\Config;
|
||||
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
|
||||
use Symfony\Component\Mailer\Transport\Dsn;
|
||||
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
|
||||
use Symfony\Component\Mailer\Transport\TransportInterface;
|
||||
|
||||
class TransportFactory extends AbstractTransportFactory
|
||||
{
|
||||
/** @var EsmtpTransportFactory */
|
||||
private $transportFactory;
|
||||
|
||||
public function setTransportFactory(EsmtpTransportFactory $transportFactory): void
|
||||
{
|
||||
$this->transportFactory = $transportFactory;
|
||||
}
|
||||
|
||||
public function create(Dsn $dsn): TransportInterface
|
||||
{
|
||||
if ($customDsn = $this->getCustomDsn($dsn->getOption('index'))) {
|
||||
$dsn = $customDsn;
|
||||
}
|
||||
|
||||
return $this->transportFactory->create($dsn);
|
||||
}
|
||||
|
||||
public function getCustomDsn($index): ?Dsn
|
||||
{
|
||||
$dbcfg = \Settings::getDefault();
|
||||
|
||||
// change smtp by settings
|
||||
if (!empty($dbcfg->smtp)) {
|
||||
$scheme = 'smtp';
|
||||
$smtp = $dbcfg->smtp;
|
||||
if (!empty($smtp[$index])) {
|
||||
$smtp = $smtp[$index];
|
||||
if ($smtp['enabled'] == 'Y' && !empty($smtp['host']) && !empty($smtp['port'])) {
|
||||
$encryption = $smtp['encryption'] ?? null;
|
||||
// enables TLS - routines:ssl3_get_record:wrong version number - bez toho to funguje taky, tak snad to je ok
|
||||
// if ($encryption) {
|
||||
// $scheme = 'smtps';
|
||||
// }
|
||||
$user = $smtp['user'] ?? null;
|
||||
|
||||
$users = explode(',', $smtp['user']);
|
||||
// randomly select user if there is more than one
|
||||
if (count($users) > 1) {
|
||||
$user = trim($users[array_rand($users)]);
|
||||
}
|
||||
|
||||
return new Dsn($scheme, $smtp['host'], $user, $smtp['password'] ?? null, (int) $smtp['port'], [
|
||||
'verify_peer' => 0,
|
||||
'local_domain' => rtrim(Config::get()['Addr']['print'], '/'),
|
||||
'timeout' => 10,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getSupportedSchemes(): array
|
||||
{
|
||||
return ['wpjshop'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user