public function run() { $model = new NewsletterForm(); if ($model->load(\Yii::$app->request->post()) && $model->validate()) { $subscribe = \Yii::$app->request->post('subscribe-button'); if (isset($subscribe)) { if ($model->agree == 0) { $model->addError('agree', \Yii::t('front', 'You must agree with newsletter subscription.')); } else { /** @var EmailRecord $email */ $email = EmailRecord::findOne(['email' => $model->email]); if ($email && $email->active == 1) { $model->addError('email', \Yii::t('front', 'This email is already in use.')); } else { if (!$email) { $email = new EmailRecord(); } $hashedEmail = $this->hashEmail($model->email); $email->email = $model->email; $email->hash = $hashedEmail; $email->active = 0; $email->created_at = new Expression('NOW()'); $email->language_id = FrontEndHelper::getLanguageIdFromAcronym(); $email->save(); $model->send(true, $hashedEmail); \Yii::$app->session->setFlash('info', \Yii::t('front', 'Thank you for subscription request of our newsletter.')); } } } else { /** @var EmailRecord $email */ $email = EmailRecord::findOne(['email' => $model->email]); if (!$email || $email && $email->active == 0) { $model->addError('email', \Yii::t('front', 'Email not found.')); } else { $hashedEmail = $this->hashEmail($model->email); $email->hash = $hashedEmail; $email->save(); $model->send(false, $hashedEmail); \Yii::$app->session->setFlash('newsletter-info', \Yii::t('front', 'Your unsubscription request was sent.')); } } } return $this->render('newsletter', compact('model')); }
public function actionNewsletter() { $model = new NewsletterForm(); Yii::$app->response->format = 'json'; if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->sendEmail(Yii::$app->params['contactEmail'])) { return ['message' => Yii::t('app', 'Thank you. We will respond to you as soon as possible.'), 'status' => 'success']; } else { return ['message' => Yii::t('app', 'There was an error during request.'), 'status' => 'error']; } } else { return ['message' => 'There was an error during processing your inputs.', 'status' => 'error']; } }