mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-06 13:10:37 -04:00
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Co-authored-by: Aditya Tripathi <aditya@climactic.co>
80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Rules\SafeSmtpHost;
|
|
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
|
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
|
|
|
|
class SmtpTransportFactory
|
|
{
|
|
/**
|
|
* @param string|null $connectAddress Connect to this checked address instead of resolving the host
|
|
* again. TLS still verifies the certificate for the host name.
|
|
*/
|
|
public static function fromSettings(object $settings, ?string $localDomain = null, ?string $connectAddress = null): EsmtpTransport
|
|
{
|
|
$mode = self::encryptionMode($settings);
|
|
$host = SafeSmtpHost::normalizeHost((string) $settings->smtp_host);
|
|
|
|
$transport = new EsmtpTransport(
|
|
formatHostForUrl($host),
|
|
(int) $settings->smtp_port,
|
|
match ($mode) {
|
|
'none' => false,
|
|
'tls' => true,
|
|
default => null,
|
|
}
|
|
);
|
|
|
|
if ($mode === 'none') {
|
|
$transport->setAutoTls(false);
|
|
}
|
|
|
|
$transport->setUsername($settings->smtp_username ?? '');
|
|
$transport->setPassword($settings->smtp_password ?? '');
|
|
|
|
$localDomain = $settings->smtp_ehlo_domain ?? $localDomain;
|
|
if ($localDomain !== null && $localDomain !== '') {
|
|
$transport->setLocalDomain($localDomain);
|
|
}
|
|
|
|
$stream = $transport->getStream();
|
|
if (isset($settings->smtp_timeout) && $stream instanceof SocketStream) {
|
|
$stream->setTimeout((float) $settings->smtp_timeout);
|
|
}
|
|
|
|
if ($connectAddress !== null && $connectAddress !== $host && $stream instanceof SocketStream) {
|
|
$stream->setHost(formatHostForUrl($connectAddress));
|
|
$stream->setStreamOptions(['ssl' => ['peer_name' => $host]]);
|
|
}
|
|
|
|
return $transport;
|
|
}
|
|
|
|
/**
|
|
* @return array{scheme: ?string, encryption: ?string, auto_tls: string}
|
|
*/
|
|
public static function mailerOptions(object $settings): array
|
|
{
|
|
$mode = self::encryptionMode($settings);
|
|
|
|
return [
|
|
'scheme' => match ($mode) {
|
|
'none', 'starttls' => 'smtp',
|
|
'tls' => 'smtps',
|
|
default => null,
|
|
},
|
|
'encryption' => $mode === 'tls' ? 'tls' : null,
|
|
'auto_tls' => $mode === 'none' ? '0' : '',
|
|
];
|
|
}
|
|
|
|
private static function encryptionMode(object $settings): ?string
|
|
{
|
|
return $settings->smtp_encryption === null
|
|
? null
|
|
: strtolower((string) $settings->smtp_encryption);
|
|
}
|
|
}
|