/** * Creates a new ContactMsg model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new ContactMsg(); $model->loadDefaultValues(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->getSession()->setFlash('success', Adm::t('', 'Data successfully inserted!')); return Adm::redirect(['update', 'id' => $model->id]); } return $this->render('create', ['model' => $model]); }
/** * 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 email was sent */ public function sendEmail() { $model = new ContactMsg(); $subject = Yii::t("app/contact", "Message from {site}", ['dot' => false, 'name' => $this->name, 'email' => $this->email, 'body' => $this->body, 'phone' => $this->phone, 'site' => Yii::$app->name]); $body = Yii::t("app/contact", "Contact message<br/>Name: {name}<br/>Email: {email}<br/>Phone: {phone}<br/>Message: {body}", ['dot' => false, 'name' => $this->name, 'email' => $this->email, 'body' => $this->body, 'phone' => $this->phone]); if ($model) { $model->from_email = $this->email; $model->subject = $subject; $model->text = $body; $model->save(false); } $valid = EmailConfig::eachEmail(function ($email) use($subject, $body) { return Yii::$app->mailer->compose()->setTo($email)->setFrom(Yii::$app->params['adminEmailName'])->setReplyTo($this->email)->setSubject($subject)->setHtmlBody($body)->send(); }); if ($valid === false) { return false; } return true; }