/**
  * Display the error log.
  */
 public function dispAdvanced_MailerAdminErrors()
 {
     $obj = new stdClass();
     $obj->status = 'error';
     $obj->page = $page = Context::get('page') ?: 1;
     $maillog = executeQuery('advanced_mailer.getLogByType', $obj);
     $maillog = $maillog->toBool() ? $this->procMailLog($maillog->data) : array();
     Context::set('advanced_mailer_log', $maillog);
     Context::set('advanced_mailer_status', 'error');
     $paging = $this->procPaging('error', $page);
     Context::set('total_count', $paging->total_count);
     Context::set('total_page', $paging->total_page);
     Context::set('page', $paging->page);
     Context::set('page_navigation', $paging->page_navigation);
     $sending_methods = Rhymix\Framework\Mail::getSupportedDrivers();
     Context::set('sending_methods', $sending_methods);
     $this->setTemplatePath($this->module_path . 'tpl');
     $this->setTemplateFile('view_log');
 }
 /**
  * Migrate from previous configuration format.
  */
 public function migrateConfig($config)
 {
     $systemconfig = array();
     if (isset($config->sending_method)) {
         $systemconfig['mail.type'] = $config->sending_method;
     } elseif (isset($config->send_type)) {
         $systemconfig['mail.type'] = $config->send_type;
     }
     if ($systemconfig['mail.type'] === 'mail') {
         $systemconfig['mail.type'] = 'mailfunction';
     }
     if (isset($config->username)) {
         if (in_array('username', $this->sending_methods[$config->sending_method]['conf'])) {
             $config->{$config->sending_method . '_username'} = $config->username;
         }
         unset($config->username);
     }
     if (isset($config->password)) {
         if (in_array('password', $this->sending_methods[$config->sending_method]['conf'])) {
             $config->{$config->sending_method . '_password'} = $config->password;
         }
         unset($config->password);
     }
     if (isset($config->domain)) {
         if (in_array('domain', $this->sending_methods[$config->sending_method]['conf'])) {
             $config->{$config->sending_method . '_domain'} = $config->domain;
         }
         unset($config->domain);
     }
     if (isset($config->api_key)) {
         if (in_array('api_key', $this->sending_methods[$config->sending_method]['conf'])) {
             $config->{$config->sending_method . '_api_key'} = $config->api_key;
         }
         unset($config->api_key);
     }
     if (isset($config->account_type)) {
         if (in_array('account_type', $this->sending_methods[$config->sending_method]['conf'])) {
             $config->{$config->sending_method . '_account_type'} = $config->account_type;
         }
         unset($config->account_type);
     }
     if (isset($config->aws_region)) {
         $config->ses_region = $config->aws_region;
         unset($config->aws_region);
     }
     if (isset($config->aws_access_key)) {
         $config->ses_access_key = $config->aws_access_key;
         unset($config->aws_access_key);
     }
     if (isset($config->aws_secret_key)) {
         $config->ses_secret_key = $config->aws_secret_key;
         unset($config->aws_secret_key);
     }
     $mail_drivers = Rhymix\Framework\Mail::getSupportedDrivers();
     foreach ($mail_drivers as $driver_name => $driver_definition) {
         foreach ($config as $key => $value) {
             if (strncmp($key, $driver_name . '_', strlen($driver_name) + 1) === 0) {
                 $subkey = substr($key, strlen($driver_name) + 1);
                 switch ($subkey) {
                     case 'host':
                     case 'port':
                     case 'security':
                         $systemconfig["mail.{$driver_name}.smtp_" . $subkey] = $value;
                         break;
                     case 'username':
                     case 'password':
                         $systemconfig["mail.{$driver_name}." . ($driver_name === 'smtp' ? 'smtp_' : 'api_') . substr($subkey, 0, 4)] = $value;
                         break;
                     case 'account_type':
                     case 'region':
                         $systemconfig["mail.{$driver_name}.api_type"] = $value;
                         break;
                     case 'access_key':
                         $systemconfig["mail.{$driver_name}.api_user"] = $value;
                         break;
                     case 'secret_key':
                         $systemconfig["mail.{$driver_name}.api_pass"] = $value;
                         break;
                     case 'domain':
                         $systemconfig["mail.{$driver_name}.api_domain"] = $value;
                         break;
                     case 'api_key':
                         $systemconfig["mail.{$driver_name}.api_token"] = $value;
                         break;
                     default:
                         break;
                 }
                 unset($config->{$key});
             }
         }
     }
     if (count($systemconfig)) {
         foreach ($systemconfig as $key => $value) {
             Rhymix\Framework\Config::set($key, $value);
         }
         Rhymix\Framework\Config::save();
     }
     unset($config->is_enabled);
     unset($config->sending_method);
     unset($config->send_type);
     $config->log_sent_mail = toBool($config->log_sent_mail);
     $config->log_errors = toBool($config->log_errors);
     $config->force_sender = toBool($config->force_sender);
     if (!isset($config->exceptions)) {
         $config->exceptions = array();
     }
     return $config;
 }
 /**
  * Send a test email using a temporary configuration.
  */
 public function procAdvanced_MailerAdminTestSend()
 {
     $advanced_mailer_config = $this->getConfig();
     $recipient_config = Context::gets('recipient_name', 'recipient_email');
     $recipient_name = $recipient_config->recipient_name;
     $recipient_email = $recipient_config->recipient_email;
     if (!$recipient_name) {
         $this->add('test_result', 'Error: ' . Context::getLang('msg_advanced_mailer_recipient_name_is_empty'));
         return;
     }
     if (!$recipient_email) {
         $this->add('test_result', 'Error: ' . Context::getLang('msg_advanced_mailer_recipient_email_is_empty'));
         return;
     }
     if (!Mail::isVaildMailAddress($recipient_email)) {
         $this->add('test_result', 'Error: ' . Context::getLang('msg_advanced_mailer_recipient_email_is_invalid'));
         return;
     }
     $oAdvancedMailerController = getController('advanced_mailer');
     $sending_method = $oAdvancedMailerController->getSendingMethodForEmailAddress($recipient_email) ?: config('mail.type');
     try {
         $oMail = new Rhymix\Framework\Mail();
         $oMail->setTitle('Advanced Mailer Test : ' . strtoupper($sending_method));
         $oMail->setContent('<p>This is a <b>test email</b> from Advanced Mailer.</p><p>Thank you for trying Advanced Mailer.</p>' . '<p>고급 메일 발송 모듈 <b>테스트</b> 메일입니다.</p><p>메일이 정상적으로 발송되고 있습니다.</p>');
         $oMail->addTo($recipient_email, $recipient_name);
         $result = $oMail->send();
         if (!$result) {
             if (count($oMail->errors)) {
                 if (config('mail.type') === 'smtp') {
                     if (strpos(config('mail.smtp.smtp_host'), 'gmail.com') !== false && strpos(implode("\n", $oMail->errors), 'code "535"') !== false) {
                         $this->add('test_result', Context::getLang('msg_advanced_mailer_google_account_security'));
                         return;
                     }
                     if (strpos(config('mail.smtp.smtp_host'), 'naver.com') !== false && strpos(implode("\n", $oMail->errors), 'Failed to authenticate') !== false) {
                         $this->add('test_result', Context::getLang('msg_advanced_mailer_naver_smtp_disabled'));
                         return;
                     }
                 }
                 $this->add('test_result', nl2br(htmlspecialchars(implode("\n", $oMail->errors))));
                 return;
             } else {
                 $this->add('test_result', Context::getLang('msg_advanced_mailer_unknown_error'));
                 return;
             }
         }
     } catch (Exception $e) {
         $this->add('test_result', nl2br(htmlspecialchars($e->getMessage())));
         return;
     }
     $this->add('test_result', Context::getLang('msg_advanced_mailer_test_success'));
     return;
 }