瀏覽代碼

[Backend] Newsletter : correctif envoi massif

feature/export_comptable
Guillaume 4 年之前
父節點
當前提交
814a017b67
共有 1 個文件被更改,包括 45 次插入17 次删除
  1. +45
    -17
      ShopBundle/Controller/Backend/NewsController.php

+ 45
- 17
ShopBundle/Controller/Backend/NewsController.php 查看文件

@@ -15,6 +15,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use \Mailjet\Resources ;

class NewsController extends AdminController
{
@@ -60,34 +61,61 @@ class NewsController extends AdminController

public function sendAction()
{
$currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
$newsletter = $this->getNewsletter() ;

$news = $this->getNews() ;
$users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
$countUsers = count($users) ;
$messages = [];
$toArray = [];

foreach ($users as $user) {
$message = new \Swift_Message('[Place du Local] '.$news->getTitle());
$message->addTo($user->getEmail())
->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
->setBody($this->renderView('mail/news-html.html.twig', [
'news' => $news,
'newsletter' => $newsletter,
'user' => $user
]), 'text/html');
array_push($messages, $message);
$toArray[] = [
'Email' => $user->getEmail(),
'Name' => $user->getFirstname().' '.$user->getLastname(),
] ;
}

if($countUsers > 0) {
$result = $this->mailjetTransport->bulkSend($messages);

$this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');

$news->setIsSent(true) ;
$this->em->persist($news);
$this->em->flush() ;
$mj = new \Mailjet\Client($this->mailjetTransport->getApiKey(), $this->mailjetTransport->getApiSecret(),true,['version' => 'v3.1']);

$paramsTemplate = [
'news' => $news,
'newsletter' => $newsletter,
'user' => $this->security->getUser()
] ;

$body = [
'Messages' => [
[
'From' => [
'Email' => $this->getParameter('app.noreply_email'),
'Name' => $this->getParameter('app.site_name')
],
'To' => $toArray,
'Subject' => $currentMerchant->getMerchantConfig('email-subject-prefix').' '.$news->getTitle(),
'TextPart' => $this->renderView('mail/news-text.html.twig', $paramsTemplate),
'HTMLPart' => $this->renderView('mail/news-html.html.twig', $paramsTemplate),
'CustomCampaign' => $news->getTitle(),
'DeduplicateCampaign' => true
]
]
];


$response = $mj->post(Resources::$Email, ['body' => $body]);

if($response->success()) {
$this->addFlash('success', 'Actualité envoyée à '.$countUsers.' utilisateurs.');

$news->setIsSent(true) ;
$this->em->persist($news);
$this->em->flush() ;
}
else {
$this->addFlash('error', "Une erreur est survenue lors de l'envoi de l'actualité.");
}
}
else {
$this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');

Loading…
取消
儲存