bundle/w3des/NewsletterBundle/src/Util.php line 82

Open in your IDE?
  1. <?php
  2. namespace w3des\NewsletterBundle;
  3. use Doctrine\ORM\EntityManager;
  4. use Ramsey\Uuid\Uuid;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Component\Validator\Validation;
  7. use w3des\NewsletterBundle\Entity\NewsletterEmail;
  8. use Symfony\Component\Validator\Constraints\NotBlank;
  9. use Symfony\Component\Validator\Constraints\Email;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  12. use w3des\AdminBundle\Service\Settings;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use w3des\NewsletterBundle\Entity\NewsletterContent;
  15. use Egulias\EmailValidator\EmailValidator;
  16. use Egulias\EmailValidator\Validation\RFCValidation;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. class Util
  19. {
  20. protected $em;
  21. protected $stack;
  22. protected $router;
  23. protected $settings;
  24. protected $mailer;
  25. protected $defaultScheme;
  26. protected $defaultHost;
  27. public function __construct(EntityManager $em, RequestStack $stack, Router $router, Settings $settings, MailerInterface $mailer, $defaultScheme, $defaultHost)
  28. {
  29. $this->settings = $settings;
  30. $this->em = $em;
  31. $this->stack = $stack;
  32. $this->router = $router;
  33. $this->mailer = $mailer;
  34. $this->defaultHost = $defaultHost;
  35. $this->defaultScheme = $defaultScheme;
  36. }
  37. public function import($_service, $pageLocale, $fileName)
  38. {
  39. $repo = $this->em->getRepository(NewsletterEmail::class);
  40. $fp = fopen($fileName, 'r');
  41. $count = 0;
  42. $lines = 0;
  43. $v = Validation::createValidator();
  44. $rules = [
  45. new NotBlank(),
  46. new Email()
  47. ];
  48. while ($line = \fgetcsv($fp, null, ';')) {
  49. $lines ++;
  50. $email = trim($line[0]);
  51. if (count($v->validate($email, $rules)) == 0 && ! $repo->findOneBy([
  52. 'locale' => $pageLocale,
  53. 'email' => $email
  54. ])) {
  55. $mail = new NewsletterEmail();
  56. $mail->setEmail($email);
  57. $mail->setService($_service);
  58. $mail->setCreatedAt(new \DateTime());
  59. $mail->setLocale($pageLocale);
  60. $mail->setId(Uuid::uuid4()->toString());
  61. $this->em->persist($mail);
  62. $this->em->flush();
  63. $count ++;
  64. }
  65. }
  66. \fclose($fp);
  67. return ' Zaimportowano ' . $count . '/ ' . $lines . ' nowych e-maili';
  68. }
  69. public function send(NewsletterContent $model, $testMail = null, OutputInterface $output = null)
  70. {
  71. $content = $this->fixContent($model->getContent());
  72. if ($testMail) {
  73. $mails = [
  74. $testMail
  75. ];
  76. } else {
  77. $mails = [];
  78. foreach ($this->em->getRepository(NewsletterEmail::class)->findBy([
  79. 'locale' => $model->getLocale()
  80. ], [
  81. 'createdAt' => 'desc'
  82. ]) as $tmp) {
  83. $mails[] = $tmp->getEmail();
  84. }
  85. }
  86. $i = 0;
  87. $mailFrom = $this->settings->get('mail_from', null, $model->getLocale());
  88. $mailFromName = $this->settings->get('mail_from_name', null, $model->getLocale()) ?: $mailFrom;
  89. foreach ($mails as $mail) {
  90. try {
  91. $validator = new EmailValidator();
  92. if (! $validator->isValid($mail, new RFCValidation())) {
  93. continue;
  94. }
  95. $link = $this->router->generate('newsletter.subscribe', [
  96. 'email' => \base64_encode($mail),
  97. 'remove' => 1,
  98. 'direct' => 1
  99. ], UrlGeneratorInterface::ABSOLUTE_URL);
  100. $cnt = $content . '<br /><br />--<br /><p style="font-size: 11px;">Aby wypisać się z naszej listy dystrybucyjnej <a href="' . $link . '" target="_blank"><span style="color:blue">kliknij tutaj</span></a></p>';
  101. $msg = \Swift_Message::newInstance($model->getTitle(), $cnt, 'text/html', 'utf-8');
  102. $msg->setFrom($mailFrom, $mailFromName);
  103. $msg->setReplyTo($mailFrom);
  104. $msg->setReturnPath($mailFrom);
  105. // $this->getHeaders()->addMailboxHeader('Rcp-To', [$this->get('settings')->get('mail_from') => $this->get('settings')->get('mail_from')]);
  106. $msg->setTo(trim($mail));
  107. if ($output) {
  108. $output->writeln('<comment>' . $mail . ';</comment>');
  109. }
  110. $this->mailer->send($msg);
  111. $i ++;
  112. if ($i % 100 == 0) {
  113. if ($output) {
  114. $output->writeln('<info>Sleep</info>');
  115. }
  116. // sleep(60);
  117. }
  118. } catch (\Exception $e1) {} catch (\Throwable $e2) {}
  119. }
  120. }
  121. protected function fixContent($cnt)
  122. {
  123. $request = $this->stack->getCurrentRequest();
  124. $prefix = '';
  125. if ($request) {
  126. $prefix = $request->getScheme() . '://' . $request->getHost() . '/';
  127. } else {
  128. $prefix = $this->defaultScheme . '://' . $this->defaultHost . '/';
  129. }
  130. $cnt = preg_replace('#src="/#i', 'src="' . $prefix, $cnt);
  131. $cnt = preg_replace('#href="/#i', 'href="' . $prefix, $cnt);
  132. return $cnt;
  133. }
  134. }