Example #1
0
 /**
  * Send a mail using this transport
  *
  * @return void
  * @throws \Magento\Framework\Exception\MailException
  */
 public function sendMessage()
 {
     try {
         $service = $this->serviceFactory->create();
         $id = (int) $this->scopeConfig->getValue(self::SERVICE_CONFIG, ScopeInterface::SCOPE_STORE);
         if ($id) {
             $service->load($id);
         }
         $args = ['message' => $this->message, 'config' => $service->getData(), 'parameters' => $this->parameters];
         $type = $service->getTransportNameByType();
         $this->transportFactory->create($type, $args)->sendMessage();
     } catch (\Exception $e) {
         $phrase = new \Magento\Framework\Phrase($e->getMessage());
         throw new \Magento\Framework\Exception\MailException($phrase, $e);
     }
 }
Example #2
0
 /**
  * Check Email transport service
  *
  * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $request = $this->getRequest();
     $uenc = $request->getParam('uenc');
     $uenc = base64_decode($uenc, true);
     $data = [];
     parse_str($uenc, $data);
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($data) {
         $id = $data['id'];
         $email = $data['email'];
         if (empty($email)) {
             $email = $data['user'];
         }
         $mailMessage = new \Magento\Framework\Mail\Message();
         $mailMessage->setBodyText('This is test transport mail.');
         $mailMessage->setFrom($email, 'test');
         $mailMessage->addTo($email, 'test');
         $mailMessage->setSubject('Test Email Transport ()');
         try {
             $args = ['message' => $mailMessage, 'config' => $data];
             switch ($data['type']) {
                 case ServiceInterface::TYPE_GMAIL:
                     $type = 'Gmail';
                     break;
                 case ServiceInterface::TYPE_SMTP:
                     $type = 'Smtp';
                     break;
                 case ServiceInterface::TYPE_SES:
                     $type = 'Ses';
                     break;
                 case ServiceInterface::TYPE_MANDRILL:
                     $type = 'Mandrill';
                     break;
                 case ServiceInterface::TYPE_SENDMAIL:
                 default:
                     $type = 'Sendmail';
                     break;
             }
             $transport = $this->transportFactory->create($type, $args);
             $transport->sendMessage();
             $successMessage = __('Connection with mail server was succesfully established.' . ' Please check your inbox to verify this final.');
             $this->messageManager->addSuccess($successMessage);
             $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setFormData(false);
             if ($this->getRequest()->getParam('back')) {
                 return $resultRedirect->setPath('*/*/edit', ['id' => $id, '_current' => true]);
             }
             return $resultRedirect->setPath('*/*/');
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\RuntimeException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addException($e, __('Something went wrong while checking the service.') . $e->getMessage());
         }
         $this->_getSession()->setFormData($data);
         return $resultRedirect->setPath('*/*/edit', ['id' => $id]);
     }
     return $resultRedirect->setPath('*/*/');
 }