/** * 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; }
/** * Finds the ContactMsg model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ContactMsg the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ContactMsg::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }