public function run(User $userToSendTo, $messageLogger)
 {
     $emailMessage = EmailMessageHelper::sendTestEmailFromUser(Yii::app()->emailHelper, Yii::app()->user->userModel, Yii::app()->user->userModel->primaryEmail->emailAddress);
     $messageLogger->addInfoMessage('Sending test SMTP setup message');
 }
 /**
  * Assumes before calling this, the outbound settings have been validated in the form.
  * Todo: When new user interface is complete, this will be re-worked to be on page instead of modal.
  */
 public function actionSendTestMessage()
 {
     $configurationForm = EmailSmtpConfigurationFormAdapter::makeFormFromGlobalConfiguration();
     $postVariableName = get_class($configurationForm);
     if (isset($_POST[$postVariableName]) || isset($_POST['UserEmailConfigurationForm'])) {
         if (isset($_POST[$postVariableName])) {
             $configurationForm->setAttributes($_POST[$postVariableName]);
         } else {
             $configurationForm->host = $_POST['UserEmailConfigurationForm']['outboundHost'];
             $configurationForm->port = $_POST['UserEmailConfigurationForm']['outboundPort'];
             $configurationForm->username = $_POST['UserEmailConfigurationForm']['outboundUsername'];
             $configurationForm->password = $_POST['UserEmailConfigurationForm']['outboundPassword'];
             $configurationForm->security = $_POST['UserEmailConfigurationForm']['outboundSecurity'];
             $configurationForm->aTestToAddress = $_POST['UserEmailConfigurationForm']['aTestToAddress'];
             $fromNameToSendMessagesFrom = $_POST['UserEmailConfigurationForm']['fromName'];
             $fromAddressToSendMessagesFrom = $_POST['UserEmailConfigurationForm']['fromAddress'];
         }
         if ($configurationForm->aTestToAddress != null) {
             $emailHelper = new EmailHelper();
             $emailHelper->loadDefaultFromAndToAddresses();
             $emailHelper->outboundHost = $configurationForm->host;
             $emailHelper->outboundPort = $configurationForm->port;
             $emailHelper->outboundUsername = $configurationForm->username;
             $emailHelper->outboundPassword = $configurationForm->password;
             $emailHelper->outboundSecurity = $configurationForm->security;
             if (isset($fromNameToSendMessagesFrom) && isset($fromAddressToSendMessagesFrom)) {
                 $from = array('name' => $fromNameToSendMessagesFrom, 'address' => $fromAddressToSendMessagesFrom);
                 $emailMessage = EmailMessageHelper::sendTestEmail($emailHelper, $from, $configurationForm->aTestToAddress);
             } else {
                 $user = BaseControlUserConfigUtil::getUserToRunAs();
                 $userToSendMessagesFrom = User::getById((int) $user->id);
                 $emailMessage = EmailMessageHelper::sendTestEmailFromUser($emailHelper, $userToSendMessagesFrom, $configurationForm->aTestToAddress);
             }
             $messageContent = null;
             if (!($emailMessage->hasErrors() || $emailMessage->hasSendError())) {
                 $messageContent .= Zurmo::t('EmailMessagesModule', 'Message successfully sent') . "\n";
             } else {
                 $messageContent .= Zurmo::t('EmailMessagesModule', 'Message failed to send') . "\n";
                 if ($emailMessage->hasSendError()) {
                     $messageContent .= $emailMessage->error . "\n";
                 } else {
                     //todo: refactor to use ZurmoHtml::errorSummary after this supports that method
                     //todo: supports nested messages better.
                     $errors = $emailMessage->getErrors();
                     foreach ($errors as $attributeNameWithErrors) {
                         foreach ($attributeNameWithErrors as $attributeError) {
                             if (is_array($attributeError)) {
                                 foreach ($attributeError as $nestedAttributeError) {
                                     $messageContent .= reset($nestedAttributeError) . "\n";
                                 }
                             } else {
                                 $messageContent .= reset($attributeError) . "\n";
                             }
                         }
                     }
                 }
             }
         } else {
             $messageContent = Zurmo::t('EmailMessagesModule', 'A test email address must be entered before you can send a test email.') . "\n";
         }
         Yii::app()->getClientScript()->setToAjaxMode();
         $messageView = new TestConnectionView($messageContent);
         $view = new ModalView($this, $messageView);
         echo $view->render();
     } else {
         throw new NotSupportedException();
     }
 }