88namespace MagePal \CustomSmtp \Block \Adminhtml ;
99
1010use Exception ;
11+ use Laminas \Mime \Message as MineMessage ;
12+ use Laminas \Mime \Part as MinePart ;
1113use Magento \Backend \Block \Template ;
1214use Magento \Backend \Block \Template \Context ;
1315use Magento \Framework \Validator \EmailAddress ;
1416use MagePal \CustomSmtp \Helper \Data ;
1517use MagePal \CustomSmtp \Model \Email ;
16- use Zend_Mail ;
17- use Zend_Mail_Exception ;
18- use Zend_Mail_Transport_Smtp ;
19- use Zend_Validate_Exception ;
18+ use Laminas \Mail \Message ;
19+ use Laminas \Mail \Transport \Smtp ;
20+ use Laminas \Mail \Transport \SmtpOptions ;
2021
2122class ValidateConfig extends Template
2223{
@@ -195,7 +196,7 @@ protected function init()
195196
196197 $ this ->toAddress = $ this ->getConfig ('email ' ) ? $ this ->getConfig ('email ' ) : $ this ->getConfig ('username ' );
197198
198- $ this ->fromAddress = trim ($ this ->getConfig ('from_email ' ));
199+ $ this ->fromAddress = trim (( string ) $ this ->getConfig ('from_email ' ));
199200
200201 if (!$ this ->emailAddressValidator ->isValid ($ this ->fromAddress )) {
201202 $ this ->fromAddress = $ this ->toAddress ;
@@ -245,7 +246,7 @@ public function verify()
245246 /**
246247 * Todo: update to new Zend Framework SMTP
247248 * @return array
248- * @throws Zend_Mail_Exception
249+ * @throws \Exception
249250 * @throws \Magento\Framework\Exception\NoSuchEntityException
250251 */
251252 protected function validateServerEmailSetting ()
@@ -254,7 +255,6 @@ protected function validateServerEmailSetting()
254255
255256 $ username = $ this ->getConfig ('username ' );
256257 $ password = $ this ->getConfig ('password ' );
257-
258258 $ auth = strtolower ($ this ->getConfig ('auth ' ));
259259
260260 //if default view
@@ -268,30 +268,31 @@ protected function validateServerEmailSetting()
268268 }
269269 }
270270
271- $ transport = $ this ->getMailTransportSmtp ();
272-
273- $ from = trim ($ this ->getConfig ('from_email ' ));
271+ $ name = 'Test from MagePal SMTP ' ;
272+ $ from = trim ((string ) $ this ->getConfig ('from_email ' ));
274273 $ from = filter_var ($ from , FILTER_VALIDATE_EMAIL ) ? $ from : $ username ;
275274 $ this ->fromAddress = filter_var ($ username , FILTER_VALIDATE_EMAIL ) ? $ username : $ from ;
275+ $ htmlBody = $ this ->_email ->setTemplateVars (['hash ' => $ this ->hash ])->getEmailBody ();
276276
277- //Create email
278- $ name = 'Test from MagePal SMTP ' ;
279- $ mail = new Zend_Mail ();
280- $ mail ->setFrom ($ this ->fromAddress , $ name );
281- $ mail ->addTo ($ this ->toAddress , 'MagePal SMTP ' );
282- $ mail ->setSubject ('Hello from MagePal SMTP (1 of 2) ' );
277+ $ transport = $ this ->getMailTransportSmtp ();
283278
284- $ htmlBody = $ this ->_email ->setTemplateVars (['hash ' => $ this ->hash ])->getEmailBody ();
279+ $ bodyMessage = new MinePart ($ htmlBody );
280+ $ bodyMessage ->type = 'text/html ' ;
281+
282+ $ body = new MineMessage ();
283+ $ body ->addPart ($ bodyMessage );
285284
286- $ mail ->setBodyHtml ($ htmlBody );
285+ $ message = new Message ();
286+ $ message ->addTo ($ this ->toAddress , 'MagePal SMTP ' )
287+ ->addFrom ($ this ->fromAddress , $ name )
288+ ->setSubject ('Hello from MagePal SMTP (1 of 2) ' )
289+ ->setBody ($ body )
290+ ->setEncoding ('UTF-8 ' );
287291
288292 $ result = $ this ->error ();
289293
290294 try {
291- //only way to prevent zend from giving an error
292- if (!$ mail ->send ($ transport ) instanceof Zend_Mail) {
293- $ result = $ this ->error (true , __ ('Invalid class, not instance of Zend Mail ' ));
294- }
295+ $ transport ->send ($ message );
295296 } catch (Exception $ e ) {
296297 $ result = $ this ->error (true , __ ($ e ->getMessage ()));
297298 }
@@ -303,29 +304,35 @@ public function getMailTransportSmtp()
303304 {
304305 $ username = $ this ->getConfig ('username ' );
305306 $ password = $ this ->getConfig ('password ' );
306-
307307 $ auth = strtolower ($ this ->getConfig ('auth ' ));
308308
309- //SMTP server configuration
310- $ smtpHost = $ this ->getConfig ('smtphost ' );
311-
312- $ smtpConf = [
309+ $ optionsArray = [
313310 'name ' => $ this ->getConfig ('name ' ),
311+ 'host ' => $ this ->getConfig ('smtphost ' ),
314312 'port ' => $ this ->getConfig ('smtpport ' )
315313 ];
316314
317315 if ($ auth != 'none ' ) {
318- $ smtpConf ['auth ' ] = $ auth ;
319- $ smtpConf ['username ' ] = $ username ;
320- $ smtpConf ['password ' ] = $ password ;
316+ $ optionsArray ['connection_class ' ] = $ auth ;
317+ $ optionsArray ['connection_config ' ] = [
318+ 'username ' => $ username ,
319+ 'password ' => $ password ,
320+ ];
321321 }
322322
323323 $ ssl = $ this ->getConfig ('ssl ' );
324324 if ($ ssl != 'none ' ) {
325- $ smtpConf ['ssl ' ] = $ ssl ;
325+ $ optionsArray = array_merge_recursive (
326+ ['connection_config ' => ['ssl ' => $ ssl ]],
327+ $ optionsArray
328+ );
326329 }
327330
328- return new Zend_Mail_Transport_Smtp ($ smtpHost , $ smtpConf );
331+ $ options = new SmtpOptions ($ optionsArray );
332+ $ transport = new Smtp ();
333+ $ transport ->setOptions ($ options );
334+
335+ return $ transport ;
329336 }
330337
331338 /**
0 commit comments