You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6KB

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