18
18
use Magento \Store \Model \ScopeInterface ;
19
19
use Laminas \Mail \Message ;
20
20
use Laminas \Mail \Transport \Sendmail ;
21
+ use Laminas \Mail \Transport \TransportInterface as LaminasTransportInterface ;
21
22
use Psr \Log \LoggerInterface ;
22
23
23
24
/**
@@ -90,10 +91,20 @@ class Transport implements TransportInterface
90
91
private $ returnPathValue ;
91
92
92
93
/**
93
- * @var Sendmail
94
+ * @var ScopeConfigInterface
95
+ */
96
+ private $ scopeConfig ;
97
+
98
+ /**
99
+ * @var LaminasTransportInterface|null
94
100
*/
95
101
private $ laminasTransport ;
96
102
103
+ /**
104
+ * @var null|string|array|\Traversable
105
+ */
106
+ private $ parameters ;
107
+
97
108
/**
98
109
* @var MessageInterface
99
110
*/
@@ -124,20 +135,28 @@ public function __construct(
124
135
self ::XML_PATH_SENDING_RETURN_PATH_EMAIL ,
125
136
ScopeInterface::SCOPE_STORE
126
137
);
138
+ $ this ->message = $ message ;
139
+ $ this ->scopeConfig = $ scopeConfig ;
140
+ $ this ->parameters = $ parameters ;
141
+ $ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
142
+ }
127
143
128
- $ transport = $ scopeConfig ->getValue (
129
- self ::XML_PATH_TRANSPORT ,
130
- ScopeInterface::SCOPE_STORE
131
- );
132
-
133
- if ($ transport === 'smtp ' ) {
134
- $ this ->laminasTransport = $ this ->createSmtpTransport ($ scopeConfig );
135
- } else {
136
- $ this ->laminasTransport = $ this ->createSendmailTransport ($ parameters );
144
+ public function getTransport (): LaminasTransportInterface
145
+ {
146
+ if ($ this ->laminasTransport === null ) {
147
+ $ transport = $ this ->scopeConfig ->getValue (
148
+ self ::XML_PATH_TRANSPORT ,
149
+ ScopeInterface::SCOPE_STORE
150
+ );
151
+
152
+ if ($ transport === 'smtp ' ) {
153
+ $ this ->laminasTransport = $ this ->createSmtpTransport ();
154
+ } else {
155
+ $ this ->laminasTransport = $ this ->createSendmailTransport ();
156
+ }
137
157
}
138
158
139
- $ this ->message = $ message ;
140
- $ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
159
+ return $ this ->laminasTransport ;
141
160
}
142
161
143
162
/**
@@ -155,7 +174,7 @@ public function sendMessage()
155
174
$ laminasMessage ->setSender ($ fromAddressList ->current ()->getEmail ());
156
175
}
157
176
158
- $ this ->laminasTransport ->send ($ laminasMessage );
177
+ $ this ->getTransport () ->send ($ laminasMessage );
159
178
} catch (\Exception $ e ) {
160
179
$ this ->logger ->error ($ e );
161
180
throw new MailException (new Phrase ('Unable to send mail. Please try again later. ' ));
@@ -170,38 +189,34 @@ public function getMessage()
170
189
return $ this ->message ;
171
190
}
172
191
173
- /**
174
- * @param $parameters
175
- * @return Smtp
176
- */
177
- private function createSmtpTransport ($ scopeConfig )
192
+ private function createSmtpTransport (): Smtp
178
193
{
179
- $ host = $ scopeConfig ->getValue (
194
+ $ host = $ this -> scopeConfig ->getValue (
180
195
self ::XML_PATH_HOST ,
181
196
ScopeInterface::SCOPE_STORE
182
197
);
183
198
184
- $ port = $ scopeConfig ->getValue (
199
+ $ port = $ this -> scopeConfig ->getValue (
185
200
self ::XML_PATH_PORT ,
186
201
ScopeInterface::SCOPE_STORE
187
202
);
188
203
189
- $ username = $ scopeConfig ->getValue (
204
+ $ username = $ this -> scopeConfig ->getValue (
190
205
self ::XML_PATH_USERNAME ,
191
206
ScopeInterface::SCOPE_STORE
192
207
);
193
208
194
- $ password = $ scopeConfig ->getValue (
209
+ $ password = $ this -> scopeConfig ->getValue (
195
210
self ::XML_PATH_PASSWORD ,
196
211
ScopeInterface::SCOPE_STORE
197
212
);
198
213
199
- $ auth = $ scopeConfig ->getValue (
214
+ $ auth = $ this -> scopeConfig ->getValue (
200
215
self ::XML_PATH_AUTH ,
201
216
ScopeInterface::SCOPE_STORE
202
217
);
203
218
204
- $ ssl = $ scopeConfig ->getValue (
219
+ $ ssl = $ this -> scopeConfig ->getValue (
205
220
self ::XML_PATH_SSL ,
206
221
ScopeInterface::SCOPE_STORE
207
222
);
@@ -218,7 +233,7 @@ private function createSmtpTransport($scopeConfig)
218
233
];
219
234
220
235
if ($ ssl && $ ssl !== 'none ' ) {
221
- $ connectionConfig ['ssl ' ] = $ ssl ;
236
+ $ options [ ' connection_config ' ] ['ssl ' ] = $ ssl ;
222
237
}
223
238
224
239
return new Smtp (new SmtpOptions ($ options ));
@@ -228,8 +243,8 @@ private function createSmtpTransport($scopeConfig)
228
243
* @param $parameters
229
244
* @return Sendmail
230
245
*/
231
- private function createSendmailTransport ($ parameters )
246
+ private function createSendmailTransport (): Sendmail
232
247
{
233
- return new Sendmail ($ parameters );
248
+ return new Sendmail ($ this -> parameters );
234
249
}
235
250
}
0 commit comments