|
- <?php
-
- namespace common\components\BulkMailer;
-
- use common\helpers\Mailjet;
- use domain\Communication\Email\Email;
- use Mailjet\Client;
-
- class BulkMailerMailjet implements BulkMailerInterface
- {
- public function sendEmail(Email $email, array $usersArray)
- {
- $client = new Client(
- Mailjet::getApiKey('public'),
- Mailjet::getApiKey('private'),
- true,
- ['version' => 'v3.1']
- );
-
- $data = ['Messages' => []] ;
-
- foreach($usersArray as $user) {
- $data['Messages'][] = [
- 'From' => [
- 'Email' => $email->getFromEmail(),
- 'Name' => $email->getFromName()
- ],
- 'To' => [
- [
- 'Email' => $user['email'],
- 'Name' => $user['name'].' '.$user['lastname']
- ]
- ],
- 'Subject' => $email->getSubject(),
- 'HTMLPart' => $email->getHtmlContent(),
- 'TextPart' => $email->getTextContent()
- ] ;
-
- if(count($data['Messages']) == 50) {
- $this->requestPostEmailViaApi($client, $data);
- $data['Messages'] = [] ;
- }
- }
-
- if(count($data['Messages']) > 0) {
- $this->requestPostEmailViaApi($client, $data);
- }
- }
-
- public function requestPostEmailViaApi(Client $client, array $data)
- {
- $client->post(\Mailjet\Resources::$Email, ['body' => $data]);
- }
- }
|