|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?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]);
- }
- }
|