示例#1
0
 /**
  * @return ViewModel
  */
 public function mailAction()
 {
     /** @var \Install\Service\Install $installService */
     $installService = $this->getServiceLocator()->get('Install\\Service\\Install');
     $sessionProgress = new Container('progress_tracker');
     $sessionProgress->offsetSet('current_step', 'mail');
     $previousStep = $installService->checkPreviousStep();
     if (null !== $previousStep) {
         return $this->redirect()->toRoute('install/default', ['controller' => 'index', 'action' => $previousStep]);
     }
     $sessionProgress->offsetSet('mail', Install::TODO);
     $sessionForms = new Container('forms');
     $this->setProgress();
     if ($this->getRequest()->isPost()) {
         $mailForm = new MailConfig();
         $mailForm->setInputFilter(new MailConfigInputFilter());
         $mailForm->setData($this->getRequest()->getPost());
         if ($mailForm->isValid()) {
             try {
                 $sessionForms->offsetSet('mailForm', $mailForm->getData());
                 $installService->createMailConfig($mailForm);
                 $sessionProgress->offsetSet('mail', Install::DONE);
                 return $this->redirect()->toRoute('install/default', ['controller' => 'index', 'action' => 'modules']);
             } catch (\Exception $ex) {
                 $mailForm->get('host')->setMessages([$ex->getMessage()]);
             }
         }
     } else {
         $mailForm = new MailConfig();
         if (null !== $sessionForms->offsetGet('mailForm')) {
             $mailForm->setData($sessionForms->offsetGet('mailForm'));
         }
     }
     return new ViewModel(['mailForm' => $mailForm]);
 }
示例#2
0
文件: Install.php 项目: zfury/cmf
 /**
  * @param MailConfig $mailForm
  */
 public function createMailConfig(MailConfig $mailForm)
 {
     copy('config/autoload/mail.local.php.dist', 'config/autoload/mail.local.php');
     $from = ['*****@*****.**'];
     $host = '';
     $port = '';
     $headers = '';
     for ($i = 0; $i < count($mailForm->getData()); $i++) {
         $paramName = array_keys($mailForm->getData())[$i];
         $paramValue = array_values($mailForm->getData())[$i];
         switch ($paramName) {
             case 'from':
                 $emailsArray = [];
                 for ($j = 0; $j < count($paramValue); $j++) {
                     $value = array_values($paramValue[$j]);
                     $currentEmail = array_shift($value);
                     if (!$currentEmail) {
                         break;
                     }
                     if ('emails' == $paramName) {
                         $paramName = strtoupper($paramName);
                     }
                     array_push($emailsArray, "'{$currentEmail}'");
                 }
                 $from = implode(',', $emailsArray);
                 break;
             case 'header':
                 for ($j = 0; $j < count($paramValue); $j++) {
                     $headerName = strtoupper($paramValue[$j]['header-name']);
                     $headerValue = $paramValue[$j]['header-value'];
                     if (!$headerName && !$headerValue) {
                         break;
                     }
                     $newRow = "'{$headerName}'=>'{$headerValue}',";
                     $headers .= $newRow;
                 }
                 break;
             case 'host':
                 $host = $paramValue;
                 break;
             case 'port':
                 $port = $paramValue;
                 break;
             default:
                 break;
         }
     }
     $content = "<?php\n        return array(\n                'mail' => array(\n                    'transport' => array(\n                        'options' => array(\n                            'host'              => '{$host}',\n                            'port'              => '{$port}',\n                        ),\n                    ),\n                    'message' => array(\n                        'headers' => array(\n                            {$headers}\n                        ),\n                        'from' => [{$from}]\n                    )\n                ),\n                // set true if module mail exists\n                'mailOptions' => array(\n                    'useModuleMail' => false\n                ),\n        );";
     $config = fopen("config/autoload/mail.local.php", "w");
     fwrite($config, $content);
     fclose($config);
 }