@@ -29,6 +29,31 @@ final class SmtpTransport implements TransportInterface
29
29
*/
30
30
private $ logger ;
31
31
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
+
32
57
/**
33
58
* Smtp constructor.
34
59
* @param string $host
@@ -51,23 +76,38 @@ public function __construct($host, $port, $user, $password, $email, $encryption
51
76
if (in_array ($ encryption , array (self ::ENCRYPTION_TLS , self ::ENCRYPTION_SSL ))) {
52
77
$ host = "$ encryption:// $ host " ;
53
78
}
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 ;
61
84
}
62
85
}
63
86
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
+
64
101
/**
65
102
* @param Message $message
66
103
* @return bool
67
104
* @throws RecipientsListEmptyException
68
105
*/
69
106
public function send (Message $ message )
70
107
{
108
+ if (!$ this ->socket ) {
109
+ $ this ->connect ();
110
+ }
71
111
if (!$ message ->getRecipients ()) {
72
112
throw new RecipientsListEmptyException ();
73
113
}
0 commit comments