@@ -19,55 +19,40 @@ class SmtpMailer implements Mailer
19
19
{
20
20
use Nette \SmartObject;
21
21
22
- private ?Signer $ signer = null ;
22
+ public const
23
+ EncryptionSSL = 'ssl ' ,
24
+ EncryptionTLS = 'tls ' ;
23
25
24
26
/** @var ?resource */
25
27
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 ;
32
28
33
29
/** @var resource */
34
30
private $ context ;
35
- private bool $ persistent ;
36
31
private string $ clientHost ;
32
+ private ?Signer $ signer = null ;
37
33
38
34
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 ) {
68
51
$ this ->clientHost = isset ($ _SERVER ['HTTP_HOST ' ]) && preg_match ('#^[\w.-]+$#D ' , $ _SERVER ['HTTP_HOST ' ])
69
52
? $ _SERVER ['HTTP_HOST ' ]
70
53
: 'localhost ' ;
54
+ } else {
55
+ $ this ->clientHost = $ clientHost ;
71
56
}
72
57
}
73
58
@@ -136,8 +121,9 @@ public function send(Message $mail): void
136
121
*/
137
122
protected function connect (): void
138
123
{
124
+ $ port = $ this ->port ?? ($ this ->encryption === self ::EncryptionSSL ? 465 : 25 );
139
125
$ 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 ,
141
127
$ errno ,
142
128
$ error ,
143
129
$ this ->timeout ,
@@ -151,7 +137,7 @@ protected function connect(): void
151
137
stream_set_timeout ($ this ->connection , $ this ->timeout , 0 );
152
138
$ this ->read (); // greeting
153
139
154
- if ($ this ->secure === ' tls ' ) {
140
+ if ($ this ->encryption === self ::EncryptionTLS ) {
155
141
$ this ->write ("EHLO $ this ->clientHost " , 250 );
156
142
$ this ->write ('STARTTLS ' , 220 );
157
143
if (!stream_socket_enable_crypto (
0 commit comments