Skip to content

Commit 1943767

Browse files
committed
SmtpMailer::__construct() $options changed to parameters (BC break)
1 parent b316586 commit 1943767

File tree

2 files changed

+40
-44
lines changed

2 files changed

+40
-44
lines changed

src/Bridges/MailDI/MailExtension.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public function getConfigSchema(): Nette\Schema\Schema
2424
'smtp' => Expect::bool(false),
2525
'host' => Expect::string()->dynamic(),
2626
'port' => Expect::int()->dynamic(),
27-
'username' => Expect::string()->dynamic(),
28-
'password' => Expect::string()->dynamic(),
27+
'username' => Expect::string('')->dynamic(),
28+
'password' => Expect::string('')->dynamic(),
2929
'secure' => Expect::anyOf(null, 'ssl', 'tls')->dynamic(), // deprecated
3030
'encryption' => Expect::anyOf(null, 'ssl', 'tls')->dynamic(),
31-
'timeout' => Expect::int()->dynamic(),
31+
'timeout' => Expect::int(20)->dynamic(),
3232
'context' => Expect::arrayOf('array')->dynamic(),
3333
'clientHost' => Expect::string()->dynamic(),
3434
'persistent' => Expect::bool(false)->dynamic(),
@@ -65,8 +65,18 @@ public function loadConfiguration()
6565
}
6666

6767
if ($this->config['smtp']) {
68-
$this->config['secure'] = $this->config['encryption'] ?? $this->config['secure'];
69-
$mailer->setFactory(Nette\Mail\SmtpMailer::class, [$this->config]);
68+
$mailer->setFactory(Nette\Mail\SmtpMailer::class, [
69+
'host' => $this->config['host'] ?? ini_get('SMTP'),
70+
'port' => isset($this->config['host']) ? $this->config['port'] : (int) ini_get('smtp_port'),
71+
'username' => $this->config['username'],
72+
'password' => $this->config['password'],
73+
'encryption' => $this->config['encryption'] ?? $this->config['secure'],
74+
'persistent' => $this->config['persistent'],
75+
'timeout' => $this->config['timeout'],
76+
'clientHost' => $this->config['clientHost'],
77+
'streamOptions' => $this->config['context'] ?: null,
78+
]);
79+
7080
} else {
7181
$mailer->setFactory(Nette\Mail\SendmailMailer::class);
7282
}

src/Mail/SmtpMailer.php

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,40 @@ class SmtpMailer implements Mailer
1919
{
2020
use Nette\SmartObject;
2121

22-
private ?Signer $signer = null;
22+
public const
23+
EncryptionSSL = 'ssl',
24+
EncryptionTLS = 'tls';
2325

2426
/** @var ?resource */
2527
private $connection;
26-
private string $host;
27-
private ?int $port;
28-
private string $username;
29-
private string $password;
30-
private string $secure;
31-
private int $timeout;
3228

3329
/** @var resource */
3430
private $context;
35-
private bool $persistent;
3631
private string $clientHost;
32+
private ?Signer $signer = null;
3733

3834

39-
public function __construct(array $options = [])
40-
{
41-
if (isset($options['host'])) {
42-
$this->host = $options['host'];
43-
$this->port = isset($options['port'])
44-
? (int) $options['port']
45-
: null;
46-
} else {
47-
$this->host = ini_get('SMTP');
48-
$this->port = (int) ini_get('smtp_port');
49-
}
50-
51-
$this->username = $options['username'] ?? '';
52-
$this->password = $options['password'] ?? '';
53-
$this->secure = $options['secure'] ?? '';
54-
$this->timeout = isset($options['timeout'])
55-
? (int) $options['timeout']
56-
: 20;
57-
$this->context = isset($options['context'])
58-
? stream_context_create($options['context'])
59-
: stream_context_get_default();
60-
if (!$this->port) {
61-
$this->port = $this->secure === 'ssl' ? 465 : 25;
62-
}
63-
64-
$this->persistent = !empty($options['persistent']);
65-
if (isset($options['clientHost'])) {
66-
$this->clientHost = $options['clientHost'];
67-
} else {
35+
public function __construct(
36+
private string $host,
37+
private string $username,
38+
private string $password,
39+
private ?int $port = null,
40+
private ?string $encryption = null,
41+
private bool $persistent = false,
42+
private int $timeout = 20,
43+
?string $clientHost = null,
44+
?array $streamOptions = null,
45+
) {
46+
$this->context = $streamOptions === null
47+
? stream_context_get_default()
48+
: stream_context_create($streamOptions);
49+
50+
if ($clientHost === null) {
6851
$this->clientHost = isset($_SERVER['HTTP_HOST']) && preg_match('#^[\w.-]+$#D', $_SERVER['HTTP_HOST'])
6952
? $_SERVER['HTTP_HOST']
7053
: 'localhost';
54+
} else {
55+
$this->clientHost = $clientHost;
7156
}
7257
}
7358

@@ -136,8 +121,9 @@ public function send(Message $mail): void
136121
*/
137122
protected function connect(): void
138123
{
124+
$port = $this->port ?? ($this->encryption === self::EncryptionSSL ? 465 : 25);
139125
$this->connection = @stream_socket_client(// @ is escalated to exception
140-
($this->secure === 'ssl' ? 'ssl://' : '') . $this->host . ':' . $this->port,
126+
($this->encryption === self::EncryptionSSL ? 'ssl://' : '') . $this->host . ':' . $port,
141127
$errno,
142128
$error,
143129
$this->timeout,
@@ -151,7 +137,7 @@ protected function connect(): void
151137
stream_set_timeout($this->connection, $this->timeout, 0);
152138
$this->read(); // greeting
153139

154-
if ($this->secure === 'tls') {
140+
if ($this->encryption === self::EncryptionTLS) {
155141
$this->write("EHLO $this->clientHost", 250);
156142
$this->write('STARTTLS', 220);
157143
if (!stream_socket_enable_crypto(

0 commit comments

Comments
 (0)