public function actionDefault() { if (!empty($this->app->request->post->message)) { try { $question = new Contact(); $question->fill($this->app->request->post->message); $question->save(); $this->redirect('/contact/sent'); } catch (MultiException $e) { $this->data->errors = $e; } } $this->data->merge($this->app->request->post->message); if (empty($this->data->email) && !empty($this->app->user)) { $this->data->email = $this->app->user->email; } }
/** * Sends an email to the specified email address using the information collected by this model. * @param string $email the target email address * @return boolean whether the model passes validation */ public function contact($email) { if (Yii::$app->request->isAjax && $this->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; echo json_encode(ActiveForm::validate($this)); Yii::$app->end(); } if ($this->validate()) { $user_info = Yii::$app->get('user', false); $user_id = $user_info && !$user_info->isGuest ? $user_info->id : null; $contact = new Contact(); $contact->user_id = $user_id; $contact->user_email = is_null($user_id) ? $this->user_email : $user_info->identity->email; $contact->user_name = is_null($user_id) ? $this->user_name : $user_info->identity->username; $contact->user_message = $this->user_message; $contact->status = self::STATUS_NEW; $contact->verifyCode = $this->verifyCode; if ($contact->save()) { Yii::$app->mailer->compose()->setTo($email)->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])->setTextBody($this->user_message)->send(); } else { /* var_dump($contact->getErrors()); die();*/ return false; } return true; } else { return false; } }