コード例 #1
0
 /**
  * Create template on mailtank
  * @param string $templateName name of template
  * @param string $basePath path to root directory of mailtank templates
  * @param string $prefix Unique for project prefix for creating template name
  */
 private function createTemplate($templateName, $basePath, $prefix)
 {
     $html = self::renderTemplate($templateName . '.html', $basePath);
     $textPlain = self::renderTemplate($templateName . '.txt', $basePath);
     // Check for template errors
     $html = self::checkTemplate($templateName . '.html', $html);
     if ($html === false) {
         return;
     }
     $textPlain = self::checkTemplate($templateName . '.txt', $textPlain);
     if ($textPlain === false) {
         return;
     }
     // Create unique mailtank ID from domain and template name
     $id = static::createLayoutId($templateName, $prefix);
     try {
         $layout = new MailtankLayout();
         $layout->setAttributes(['id' => $id]);
         $layout->delete();
     } catch (\Exception $e) {
         // Do nothing
     }
     $layout = new MailtankLayout();
     $attr = ['id' => $id, 'name' => $id, 'subject_markup' => '{{subject}}', 'markup' => $html, 'plaintext_markup' => $textPlain];
     $layout->setAttributes($attr);
     if ($layout->save()) {
         if (self::$useConsoleOut) {
             Console::outputColored("Template <%g{$templateName}%n> was create, id=" . $layout->id);
         }
     } else {
         $err = [];
         foreach ($layout->getErrors() as $k => $v) {
             $err[] = $k . ' : ' . $v;
         }
         if (!self::$useConsoleOut) {
             throw new MailtankException("Template <%m{$templateName}%n> wasn't create" . PHP_EOL . implode(PHP_EOL, $err));
         }
         Console::outputColored("Template <%m{$templateName}%n> wasn't create");
         Console::addIndent();
         foreach ($err as $v) {
             Console::error($v);
         }
         Console::removeIndent();
     }
 }
コード例 #2
0
 /**
  * Синхронизация подписчиков с мейлтанком
  * @param $isSync boolean false - только сравнить, true - синхронизировать
  */
 public static function syncFromMailtank($isSync = false)
 {
     $subscriberCount = 0;
     try {
         $page = 1;
         $res = self::getPage($page);
         $totalPages = $res['pages_total'];
         while (true) {
             // Обрабатываем подписчиков
             foreach ($res['objects'] as $subscriber) {
                 $subscriberCount++;
                 $mailtankId = $subscriber['id'];
                 $email = $subscriber['email'];
                 $mailtank2email = Mailtank2Email::find()->where(['email' => $email])->asArray()->one();
                 if (!$mailtank2email) {
                     // У нас нет такого email
                     $obj = Mailtank2Email::find()->where(['mailtankId' => $mailtankId])->asArray()->one();
                     if (!$obj) {
                         // Если у нас нет такого mailtankId, то это новый подписчик
                         Console::outputColored("- Subscriber <%y" . $email . "%n> exists only in the mailtank", false);
                         if ($isSync) {
                             if (self::createInnerSubscriber($email, $mailtankId)) {
                                 Console::outputColored("...%g created%n", false);
                             } else {
                                 Console::outputColored("...%r not created%n", false);
                             }
                         }
                         Console::output();
                     } else {
                         // Если у нас есть такой mailtankId, то прописываем ему email с мейлтанка
                         Console::outputColored("- Found inner subscriber mailtankId=<%y" . $mailtankId . "%n> but other email=<%y" . $obj['email'] . "%n>", false);
                         if ($isSync) {
                             $obj['email'] = $email;
                             if ($obj->save()) {
                                 Console::outputColored("...%g changed to <" . $email . ">%n", false);
                             } else {
                                 Console::outputColored("...%r not changed%n", false);
                             }
                         }
                         Console::output();
                     }
                 } else {
                     // У нас есть такой email
                     // Если совпадает mailtankId, то корректный пользователь и ничего делать не надо
                     if ($mailtank2email['mailtankId'] == $mailtankId) {
                         continue;
                     }
                     Console::outputColored("- Same emails <%y" . $email . "%n> belong to different subscribers in the mailtank. Inner id=<%y" . $mailtank2email['mailtankId'] . "%n>, mailtank id=<%y" . $mailtankId . "%n>");
                     if ($isSync) {
                         // Если не совпадает mailtankId, ищем подписчика по mailtankId с мейлтанка
                         $obj = Mailtank2Email::find()->where(['mailtankId' => $mailtankId])->asArray()->one();
                         if ($obj) {
                             // Если нашли, то удаляем его, а предыдущему меняем mailtankId
                             if ($obj->delete()) {
                                 Console::outputColored("  %gInner subscriber mailtankId <" . $mailtankId . "> succesfully delete%n");
                             } else {
                                 Console::outputColored("  %rWrong delete mailtank2email with mailtankId=<" . $mailtankId . ">%n");
                                 die;
                             }
                         }
                         $mailtank2email['mailtankId'] = $mailtankId;
                         if ($mailtank2email->save()) {
                             Console::outputColored("  %gFor email <" . $email . "> changed MailtankId to <" . $mailtankId . ">%n");
                         } else {
                             Console::outputColored("  %rFor email <" . $email . "> didn't change MailtankId to <" . $mailtankId . ">%n");
                         }
                     }
                 }
             }
             if ($page >= $totalPages) {
                 break;
             }
             $page++;
             $res = self::getPage($page);
         }
     } catch (MailtankException $e) {
         if ($e->getCode() == 404) {
             return true;
         }
         throw $e;
     }
 }