@@ -29,6 +29,31 @@ final class SmtpTransport implements TransportInterface
2929 */
3030 private $ logger ;
3131
32+ /**
33+ * @var string
34+ */
35+ private $ connectHost ;
36+
37+ /**
38+ * @var int
39+ */
40+ private $ connectPort ;
41+
42+ /**
43+ * @var string
44+ */
45+ private $ connectUser ;
46+
47+ /**
48+ * @var string
49+ */
50+ private $ connectPassword ;
51+
52+ /**
53+ * @var string
54+ */
55+ private $ connectDomain ;
56+
3257 /**
3358 * Smtp constructor.
3459 * @param string $host
@@ -51,23 +76,38 @@ public function __construct($host, $port, $user, $password, $email, $encryption
5176 if (in_array ($ encryption , array (self ::ENCRYPTION_TLS , self ::ENCRYPTION_SSL ))) {
5277 $ host = "$ encryption:// $ host " ;
5378 }
54- $ this ->socket = fsockopen ((string )$ host , (int )$ port , $ errCode , $ errMessage , 30 );
55- $ test = fgets ($ this ->socket , 512 );
56- unset($ test );
57- $ this ->smtpCommand ("EHLO $ domain " );
58- $ this ->smtpCommand ("AUTH LOGIN " );
59- $ this ->smtpCommand (base64_encode ($ user ));
60- $ this ->smtpCommand (base64_encode ($ password ));
79+ $ this ->connectHost = $ host ;
80+ $ this ->connectPort = $ port ;
81+ $ this ->connectUser = $ user ;
82+ $ this ->connectPassword = $ password ;
83+ $ this ->connectDomain = $ domain ;
6184 }
6285 }
6386
87+ private function connect ()
88+ {
89+ if ($ this ->socket ) {
90+ return ;
91+ }
92+ $ this ->socket = fsockopen ($ this ->connectHost , $ this ->connectPort , $ errCode , $ errMessage , 30 );
93+ $ test = fgets ($ this ->socket , 512 );
94+ unset($ test );
95+ $ this ->smtpCommand ("EHLO {$ this ->connectDomain }" );
96+ $ this ->smtpCommand ("AUTH LOGIN " );
97+ $ this ->smtpCommand (base64_encode ($ this ->connectUser ));
98+ $ this ->smtpCommand (base64_encode ($ this ->connectPassword ));
99+ }
100+
64101 /**
65102 * @param Message $message
66103 * @return bool
67104 * @throws RecipientsListEmptyException
68105 */
69106 public function send (Message $ message )
70107 {
108+ if (!$ this ->socket ) {
109+ $ this ->connect ();
110+ }
71111 if (!$ message ->getRecipients ()) {
72112 throw new RecipientsListEmptyException ();
73113 }
0 commit comments