Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

53 lines
1.5KB

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