88namespace MagePal \CustomSmtp \Block \Adminhtml ;
99
1010use Exception ;
11- use Laminas \Mime \Message as MineMessage ;
12- use Laminas \Mime \Part as MinePart ;
1311use Magento \Backend \Block \Template ;
1412use Magento \Backend \Block \Template \Context ;
1513use Magento \Framework \Validator \EmailAddress ;
1614use Magento \Store \Model \ScopeInterface ;
1715use MagePal \CustomSmtp \Helper \Data ;
1816use MagePal \CustomSmtp \Model \Email ;
19- use Laminas \Mail \Message ;
20- use Laminas \Mail \Transport \Smtp ;
21- use Laminas \Mail \Transport \SmtpOptions ;
17+ use Symfony \Component \Mailer \Transport \Smtp \Auth \LoginAuthenticator ;
18+ use Symfony \Component \Mailer \Transport \Smtp \Auth \PlainAuthenticator ;
19+ use Symfony \Component \Mailer \Transport \Smtp \EsmtpTransport ;
20+ use Symfony \Component \Mime \Email as SymfonyEmail ;
21+ use Symfony \Component \Mime \Address ;
2222
2323class ValidateConfig extends Template
2424{
@@ -123,7 +123,7 @@ public function setStoreId($id)
123123 }
124124
125125 /**
126- * @return int \ null
126+ * @return string| null
127127 */
128128 public function getStoreId ()
129129 {
@@ -192,13 +192,20 @@ public function loadDefaultConfig()
192192 return $ this ;
193193 }
194194
195+ /**
196+ * @param $scopeType
197+ * @param $scopeCode
198+ * @return $this
199+ */
195200 public function loadObscuredData ($ scopeType , $ scopeCode )
196201 {
197202 //if password mask (6 stars)
198203 if ($ this ->getConfig ('password ' ) === '****** ' ) {
199204 $ password = $ this ->_dataHelper ->getConfigPassword ($ scopeType , $ scopeCode );
200205 $ this ->setConfig ('password ' , $ password );
201206 }
207+
208+ return $ this ;
202209 }
203210
204211 /**
@@ -260,9 +267,7 @@ public function verify()
260267 }
261268
262269 /**
263- * Todo: update to new Zend Framework SMTP
264270 * @return array
265- * @throws \Exception
266271 * @throws \Magento\Framework\Exception\NoSuchEntityException
267272 */
268273 protected function validateServerEmailSetting ()
@@ -290,24 +295,16 @@ protected function validateServerEmailSetting()
290295 $ this ->fromAddress = filter_var ($ username , FILTER_VALIDATE_EMAIL ) ? $ username : $ from ;
291296 $ htmlBody = $ this ->_email ->setTemplateVars (['hash ' => $ this ->hash ])->getEmailBody ();
292297
293- $ transport = $ this ->getMailTransportSmtp ();
294-
295- $ bodyMessage = new MinePart ($ htmlBody );
296- $ bodyMessage ->type = 'text/html ' ;
297-
298- $ body = new MineMessage ();
299- $ body ->addPart ($ bodyMessage );
300-
301- $ message = new Message ();
302- $ message ->addTo ($ this ->toAddress , 'MagePal SMTP ' )
303- ->addFrom ($ this ->fromAddress , $ name )
304- ->setSubject ('Hello from MagePal SMTP (1 of 2) ' )
305- ->setBody ($ body )
306- ->setEncoding ('UTF-8 ' );
298+ $ message = new SymfonyEmail ();
299+ $ message ->to (new Address ($ this ->toAddress , 'MagePal SMTP ' ))
300+ ->from (new Address ($ this ->fromAddress , $ name ))
301+ ->subject ('Hello from MagePal SMTP (1 of 2) ' )
302+ ->html ($ htmlBody );
307303
308304 $ result = $ this ->error ();
309305
310306 try {
307+ $ transport = $ this ->getMailTransportSmtp ();
311308 $ transport ->send ($ message );
312309 } catch (Exception $ e ) {
313310 $ result = $ this ->error (true , __ ($ e ->getMessage ()));
@@ -317,39 +314,46 @@ protected function validateServerEmailSetting()
317314 }
318315
319316 /**
320- * @return Smtp
317+ * @return EsmtpTransport
321318 */
322319 public function getMailTransportSmtp ()
323320 {
324321 $ username = $ this ->getConfig ('username ' );
325322 $ password = $ this ->getConfig ('password ' );
326323 $ auth = strtolower ($ this ->getConfig ('auth ' ));
324+ $ name = $ this ->getConfig ('name ' );
325+ $ host = $ this ->getConfig ('smtphost ' );
326+ $ port = $ this ->getConfig ('smtpport ' );
327327
328- $ optionsArray = [
329- 'name ' => $ this ->getConfig ('name ' ),
330- 'host ' => $ this ->getConfig ('smtphost ' ),
331- 'port ' => $ this ->getConfig ('smtpport ' )
332- ];
328+ $ tls = false ;
329+ if ($ auth !== 'none ' ) {
330+ $ tls = true ;
331+ }
332+
333+ $ transport = new EsmtpTransport ($ host , $ port , $ tls );
333334
334335 if ($ auth != 'none ' ) {
335- $ optionsArray ['connection_class ' ] = $ auth ;
336- $ optionsArray ['connection_config ' ] = [
337- 'username ' => $ username ,
338- 'password ' => $ password ,
339- ];
340- }
336+ if ($ username ) {
337+ $ transport ->setUsername ($ username );
338+ }
341339
342- $ ssl = $ this ->getConfig ('ssl ' );
343- if ($ ssl != 'none ' ) {
344- $ optionsArray = array_merge_recursive (
345- ['connection_config ' => ['ssl ' => $ ssl ]],
346- $ optionsArray
347- );
340+ if ($ password ) {
341+ $ transport ->setPassword ($ password );
342+ }
348343 }
349344
350- $ options = new SmtpOptions ($ optionsArray );
351- $ transport = new Smtp ();
352- $ transport ->setOptions ($ options );
345+ switch ($ auth ) {
346+ case 'plain ' :
347+ $ transport ->setAuthenticators ([new PlainAuthenticator ()]);
348+ break ;
349+ case 'login ' :
350+ $ transport ->setAuthenticators ([new LoginAuthenticator ()]);
351+ break ;
352+ case 'none ' :
353+ break ;
354+ default :
355+ throw new \InvalidArgumentException ('Invalid authentication type: ' . $ auth );
356+ }
353357
354358 return $ transport ;
355359 }
0 commit comments