@@ -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