選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BulkMailerMailjet.php 1.5KB

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