Example #1
0
 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'));
 }
Example #2
0
 /**
  * Creates data provider instance with search query applied
  * @param $params
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     /** @var ActiveQuery $query */
     $query = EmailRecord::find();
     if (!isset($params['sort'])) {
         $query->orderBy(['created_at' => SORT_DESC]);
     }
     $session = Yii::$app->session;
     if (!$session['language_id']) {
         $session['language_id'] = LanguageRecord::getMainLanguageId();
     }
     $query->andWhere(['language_id' => $session['language_id']]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['active' => $this->active]);
     $query->andFilterWhere(['like', 'email', $this->email]);
     return $dataProvider;
 }
 public function actionSend($id)
 {
     /** @var Newsletter $model */
     $model = Newsletter::findOne($id);
     if ($model) {
         $this->layout = '@backend/views/newsletter/layouts/' . $model->layout->filename;
         $viewMail = true;
         $htmlBody = $this->render('view', compact('model', 'viewMail'));
         $textBody = Yii::t('back', 'Dear user') . ",\n\n";
         $textBody .= Yii::t('back', 'we would send to you a pretty version of our newsletter. But your email client doesn\'t support it.') . "\n";
         $textBody .= Yii::t('back', 'Luckily you can see our newsletter at this link') . ": " . Url::to(['view', 'id' => $id], true) . "\n\n";
         $textBody .= Yii::t('back', 'Yours sincerely') . " " . Yii::$app->params['sendingEmailTitle'];
         $sendingEmail = Yii::$app->params['sendingEmail'];
         $targetEmailsChunks = array_chunk(EmailRecord::getActiveEmails(), Yii::$app->params['maxEmailsCount']);
         foreach ($targetEmailsChunks as $targetEmailsChunk) {
             Yii::$app->mailer->compose()->setTo(Yii::$app->params['sendingEmail'])->setBcc($targetEmailsChunk)->setFrom([$sendingEmail => Yii::$app->params['sendingEmailTitle']])->setSubject(Yii::$app->params['sendingEmailTitle'] . ' - ' . Yii::t('back', 'newsletter'))->setTextBody($textBody)->setHtmlBody($htmlBody)->send();
         }
         $model->content_date = new Expression('DATE(NOW())');
         $model->content_time = new Expression('TIME(NOW())');
         $model->save(false);
         $session = Yii::$app->session;
         $session->setFlash('info', Yii::t('back', 'Newsletter successfuly sent!'));
         return $this->redirect(['index']);
     } else {
         throw new NotFoundHttpException(Yii::t('back', 'The requested page does not exist.'));
     }
 }
Example #4
0
 /**
  * Deletes Email
  * @throws \Exception
  */
 public function deleteEmail()
 {
     /** @var $email EmailRecord */
     if ($email = EmailRecord::findOne($this->item_id)) {
         $email->delete();
     }
 }