MagePal\GmailSmtpApp\Controller\Adminhtml\Test\Index::execute PHP Method

execute() public method

Index action
public execute ( ) : Magento\Backend\Model\View\Result\Page
return Magento\Backend\Model\View\Result\Page
    public function execute()
    {
        $request = $this->getRequest();
        $store_id = $request->getParam('store', null);
        $name = 'MagePal Gmail Smtp App Test';
        $username = $request->getPost('username');
        $password = $request->getPost('password');
        //if default view
        //see https://github.com/magento/magento2/issues/3019
        if (!$request->getParam('store', false)) {
            if (empty($username) || empty($password)) {
                $this->getResponse()->setBody(__('Please enter a valid username/password'));
                return;
            }
        }
        //if password mask (6 stars)
        $password = $password == '******' ? $this->_dataHelper->getConfigPassword($store_id) : $password;
        $to = $request->getPost('email') ? $request->getPost('email') : $username;
        //SMTP server configuration
        $smtpHost = $request->getPost('smtphost');
        $smtpConf = array('auth' => strtolower($request->getPost('auth')), 'ssl' => $request->getPost('ssl'), 'username' => $username, 'password' => $password, 'port' => $request->getPost('smtpport'));
        $transport = new \Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);
        $from = trim($request->getPost('from_email'));
        $from = \Zend_Validate::is($from, 'EmailAddress') ? $from : $username;
        //Create email
        $mail = new \Zend_Mail();
        $mail->setFrom($from, $name);
        $mail->addTo($to, $to);
        $mail->setSubject('Hello from MagePal');
        $mail->setBodyText('Thank you for choosing MagePal extension.');
        $result = __('Sent... Please check your email') . ' ' . $to;
        try {
            //only way to prevent zend from giving a error
            if (!$mail->send($transport) instanceof \Zend_Mail) {
            }
        } catch (\Exception $e) {
            $result = __($e->getMessage());
        }
        $this->getResponse()->setBody($this->makeClickableLinks($result));
    }