Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

BulkMailerBrevo.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. }