Browse Source

[Backend] Correctif newsletter

master
Guillaume 4 years ago
parent
commit
449db37906
1 changed files with 20 additions and 9 deletions
  1. +20
    -9
      ShopBundle/Controller/Backend/NewsController.php

+ 20
- 9
ShopBundle/Controller/Backend/NewsController.php View File

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

$packageMessagesArray = [];
$packageMessageLimit = 50 ;


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


$i = 0 ;
foreach ($users as $user) { foreach ($users as $user) {
if(!isset($packageMessagesArray[$i])) {
$packageMessagesArray[$i] = [] ;
}

if($user->getEmail() && strlen($user->getEmail())) { if($user->getEmail() && strlen($user->getEmail())) {
$messagesArray[] = [
$packageMessagesArray[$i][] = [
'To' => [ 'To' => [
[ [
'Email' => $user->getEmail(), 'Email' => $user->getEmail(),
'CustomCampaign' => $news->getTitle(), 'CustomCampaign' => $news->getTitle(),
'DeduplicateCampaign' => true 'DeduplicateCampaign' => true
] ; ] ;

if(count($packageMessagesArray[$i]) >= $packageMessageLimit) {
$i ++ ;
}
} }
} }


if($countUsers > 0) { if($countUsers > 0) {
$mj = new \Mailjet\Client($this->mailjetTransport->getApiKey(), $this->mailjetTransport->getApiSecret(),true,['version' => 'v3.1']); $mj = new \Mailjet\Client($this->mailjetTransport->getApiKey(), $this->mailjetTransport->getApiSecret(),true,['version' => 'v3.1']);


$body = [
'Messages' => $messagesArray
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
foreach($packageMessagesArray as $messagesArray) {
$body = [
'Messages' => $messagesArray
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
}


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


$news->setIsSent(true) ; $news->setIsSent(true) ;
$this->em->flush() ; $this->em->flush() ;
} }
else { else {
print_r($response->getBody()) ;
die() ;
$this->addFlash('error', "Une erreur est survenue lors de l'envoi de l'actualité."); $this->addFlash('error', "Une erreur est survenue lors de l'envoi de l'actualité.");
} }
} }

Loading…
Cancel
Save