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.

BulkMailerMailjet.php 1.5KB

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