Skip to content

Commit

Permalink
SmtpMailer::__construct() $options changed to parameters (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 26, 2023
1 parent b316586 commit 1943767
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
20 changes: 15 additions & 5 deletions src/Bridges/MailDI/MailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function getConfigSchema(): Nette\Schema\Schema
'smtp' => Expect::bool(false),
'host' => Expect::string()->dynamic(),
'port' => Expect::int()->dynamic(),
'username' => Expect::string()->dynamic(),
'password' => Expect::string()->dynamic(),
'username' => Expect::string('')->dynamic(),
'password' => Expect::string('')->dynamic(),
'secure' => Expect::anyOf(null, 'ssl', 'tls')->dynamic(), // deprecated
'encryption' => Expect::anyOf(null, 'ssl', 'tls')->dynamic(),
'timeout' => Expect::int()->dynamic(),
'timeout' => Expect::int(20)->dynamic(),
'context' => Expect::arrayOf('array')->dynamic(),
'clientHost' => Expect::string()->dynamic(),
'persistent' => Expect::bool(false)->dynamic(),
Expand Down Expand Up @@ -65,8 +65,18 @@ public function loadConfiguration()
}

if ($this->config['smtp']) {
$this->config['secure'] = $this->config['encryption'] ?? $this->config['secure'];
$mailer->setFactory(Nette\Mail\SmtpMailer::class, [$this->config]);
$mailer->setFactory(Nette\Mail\SmtpMailer::class, [
'host' => $this->config['host'] ?? ini_get('SMTP'),
'port' => isset($this->config['host']) ? $this->config['port'] : (int) ini_get('smtp_port'),
'username' => $this->config['username'],
'password' => $this->config['password'],
'encryption' => $this->config['encryption'] ?? $this->config['secure'],
'persistent' => $this->config['persistent'],
'timeout' => $this->config['timeout'],
'clientHost' => $this->config['clientHost'],
'streamOptions' => $this->config['context'] ?: null,
]);

} else {
$mailer->setFactory(Nette\Mail\SendmailMailer::class);
}
Expand Down
64 changes: 25 additions & 39 deletions src/Mail/SmtpMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,40 @@ class SmtpMailer implements Mailer
{
use Nette\SmartObject;

private ?Signer $signer = null;
public const
EncryptionSSL = 'ssl',
EncryptionTLS = 'tls';

/** @var ?resource */
private $connection;
private string $host;
private ?int $port;
private string $username;
private string $password;
private string $secure;
private int $timeout;

/** @var resource */
private $context;
private bool $persistent;
private string $clientHost;
private ?Signer $signer = null;


public function __construct(array $options = [])
{
if (isset($options['host'])) {
$this->host = $options['host'];
$this->port = isset($options['port'])
? (int) $options['port']
: null;
} else {
$this->host = ini_get('SMTP');
$this->port = (int) ini_get('smtp_port');
}

$this->username = $options['username'] ?? '';
$this->password = $options['password'] ?? '';
$this->secure = $options['secure'] ?? '';
$this->timeout = isset($options['timeout'])
? (int) $options['timeout']
: 20;
$this->context = isset($options['context'])
? stream_context_create($options['context'])
: stream_context_get_default();
if (!$this->port) {
$this->port = $this->secure === 'ssl' ? 465 : 25;
}

$this->persistent = !empty($options['persistent']);
if (isset($options['clientHost'])) {
$this->clientHost = $options['clientHost'];
} else {
public function __construct(
private string $host,
private string $username,
private string $password,
private ?int $port = null,
private ?string $encryption = null,
private bool $persistent = false,
private int $timeout = 20,
?string $clientHost = null,
?array $streamOptions = null,
) {
$this->context = $streamOptions === null
? stream_context_get_default()
: stream_context_create($streamOptions);

if ($clientHost === null) {
$this->clientHost = isset($_SERVER['HTTP_HOST']) && preg_match('#^[\w.-]+$#D', $_SERVER['HTTP_HOST'])
? $_SERVER['HTTP_HOST']
: 'localhost';
} else {
$this->clientHost = $clientHost;
}
}

Expand Down Expand Up @@ -136,8 +121,9 @@ public function send(Message $mail): void
*/
protected function connect(): void
{
$port = $this->port ?? ($this->encryption === self::EncryptionSSL ? 465 : 25);
$this->connection = @stream_socket_client(// @ is escalated to exception
($this->secure === 'ssl' ? 'ssl://' : '') . $this->host . ':' . $this->port,
($this->encryption === self::EncryptionSSL ? 'ssl://' : '') . $this->host . ':' . $port,
$errno,
$error,
$this->timeout,
Expand All @@ -151,7 +137,7 @@ protected function connect(): void
stream_set_timeout($this->connection, $this->timeout, 0);
$this->read(); // greeting

if ($this->secure === 'tls') {
if ($this->encryption === self::EncryptionTLS) {
$this->write("EHLO $this->clientHost", 250);
$this->write('STARTTLS', 220);
if (!stream_socket_enable_crypto(
Expand Down

0 comments on commit 1943767

Please sign in to comment.