public function send($display_log = false) { $mailer = Yii::$app->mailer; $newsletter = $this->owner; $log = new NewsletterLog(); $log->link('newsletter', $newsletter); $appendToLog = function ($string, $skip_eol = false) use($log, $display_log) { $content = $string . ($skip_eol ? '' : PHP_EOL); if ($display_log) { echo nl2br($content); } $log->content .= $content; $log->save(); }; $errors = []; foreach ($newsletter->mailingLists as $list) { if ($list->from_email) { $from_email = $list->from_email; $from_name = $list->from_name ?: null; } else { $from_email = Yii::$app->params['supportEmail']; $from_name = $list->from_name ?: Yii::$app->params['companyName']; } foreach ($list->partners as $partner) { if (!$partner->email) { $appendToLog(sprintf("Partner #%s didn't have email. Skipping.", $partner->id)); continue; } $appendToLog($partner->email . ': ', true); $error = ''; $content = $newsletter->processContent($newsletter->body, $partner); try { $mail = $mailer->compose()->setHtmlBody($content)->setFrom($from_email, $from_name)->setTo($partner->email)->setSubject($newsletter->subject); if ($list->reply_to) { $mail->setReplyTo($list->reply_to); } foreach ($newsletter->getAttachments() as $attachment) { $mail->attach($attachment->getPath()); } $result = $mail->send(); $appendToLog($result ? __('Success') : __('Failed')); } catch (\Swift_SwiftException $e) { $appendToLog(__('Failed') . ': ' . $e->getMessage()); $errors[] = $e->getMessage(); } } } return $errors; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = NewsletterLog::find()->orderBy('timestamp'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSizeLimit' => [10, 100]], 'sort' => ['defaultOrder' => ['timestamp' => SORT_DESC]]]); $params = $this->processParams($params); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'newsletter_id' => $this->newsletter_id, 'user_id' => $this->user_id]); return $dataProvider; }
public function getNewsletterLogs() { return $this->hasMany(NewsletterLog::className(), ['user_id' => 'id']); }
public function getLogsCount() { return NewsletterLog::find()->where(['newsletter_id' => $this->id])->count(); }
public function actionLogDelete($newsletter_id, array $ids = null) { if ($newsletter_id && $ids && NewsletterLog::deleteAll(['newsletter_id' => $newsletter_id, 'id' => $ids])) { Yii::$app->session->setFlash('success', __('Items have been deleted successfully.')); } return $this->redirect(['update', 'id' => $newsletter_id]); }