Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

54 lines
1.6KB

  1. <?php
  2. namespace common\components\BulkMailer;
  3. use domain\Communication\Email\Email;
  4. use domain\User\User\UserSolver;
  5. use GuzzleHttp\Client;
  6. use Psr\Http\Message\ResponseInterface;
  7. class BulkMailerBrevo implements BulkMailerInterface
  8. {
  9. public function sendEmail(Email $email, array $usersArray)
  10. {
  11. $userSolver = UserSolver::getInstance();
  12. $client = new Client();
  13. $data = [
  14. 'sender' => [
  15. 'name' => $email->getFromName(),
  16. 'email' => $email->getFromEmail()
  17. ],
  18. 'bcc' => [],
  19. 'subject' => $email->getSubject(),
  20. 'htmlContent' => $email->getHtmlContent()
  21. ] ;
  22. foreach($usersArray as $user) {
  23. $data['bcc'][] = [
  24. 'name' => $userSolver->getUsernameFromArray($user),
  25. 'email' => $user['email']
  26. ];
  27. if(count($data['bcc']) == 50) {
  28. $this->requestPostEmailViaApi($client, $data);
  29. $data['bcc'] = [] ;
  30. }
  31. }
  32. if(count($data['bcc']) > 0) {
  33. $this->requestPostEmailViaApi($client, $data);
  34. }
  35. }
  36. public function requestPostEmailViaApi(Client $client, array $data): ResponseInterface
  37. {
  38. return $client->request('POST', 'https://api.brevo.com/v3/smtp/email', [
  39. 'headers' => [
  40. 'accept' => 'application/json',
  41. 'content-type' => 'application/json',
  42. 'api-key' => \Yii::$app->parameterBag->get('brevoApiKey'),
  43. ],
  44. 'body' => json_encode($data)
  45. ]);
  46. }
  47. }