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.

54 lines
1.5KB

  1. <?php
  2. namespace common\components\BulkMailer;
  3. use common\helpers\Mailjet;
  4. use domain\Communication\Email\Email;
  5. use Mailjet\Client;
  6. class BulkMailerMailjet implements BulkMailerInterface
  7. {
  8. public function sendEmail(Email $email, array $usersArray)
  9. {
  10. $client = new Client(
  11. Mailjet::getApiKey('public'),
  12. Mailjet::getApiKey('private'),
  13. true,
  14. ['version' => 'v3.1']
  15. );
  16. $data = ['Messages' => []] ;
  17. foreach($usersArray as $user) {
  18. $data['Messages'][] = [
  19. 'From' => [
  20. 'Email' => $email->getFromEmail(),
  21. 'Name' => $email->getFromName()
  22. ],
  23. 'To' => [
  24. [
  25. 'Email' => $user['email'],
  26. 'Name' => $user['name'].' '.$user['lastname']
  27. ]
  28. ],
  29. 'Subject' => $email->getSubject(),
  30. 'HTMLPart' => $email->getHtmlContent(),
  31. 'TextPart' => $email->getTextContent()
  32. ] ;
  33. if(count($data['Messages']) == 50) {
  34. $this->requestPostEmailViaApi($client, $data);
  35. $data['Messages'] = [] ;
  36. }
  37. }
  38. if(count($data['Messages']) > 0) {
  39. $this->requestPostEmailViaApi($client, $data);
  40. }
  41. }
  42. public function requestPostEmailViaApi(Client $client, array $data)
  43. {
  44. $client->post(\Mailjet\Resources::$Email, ['body' => $data]);
  45. }
  46. }