- <?php
-
- namespace common\components\BulkMailer;
-
- use domain\User\User\UserSolver;
- use GuzzleHttp\Client;
- use Psr\Http\Message\ResponseInterface;
-
- class BulkMailerBrevo implements BulkMailerInterface
- {
- public function sendEmails(array $contactsArray, string $fromName, string $fromEmail, string $subject, string $htmlContent, string $textContent = null)
- {
- $userSolver = UserSolver::getInstance();
- $client = new Client();
- $data = [
- 'sender' => [
- 'name' => $fromName,
- 'email' => $fromEmail
- ],
- 'bcc' => [],
- 'subject' => $subject,
- 'htmlContent' => $htmlContent
- ] ;
-
- foreach($contactsArray as $user) {
- $data['bcc'][] = [
- 'name' => $userSolver->getUsernameFromArray($user),
- 'email' => $user['email']
- ];
-
- if(count($data['bcc']) == 50) {
- $this->requestPostEmailViaApi($client, $data);
- $data['bcc'] = [] ;
- }
- }
-
- if(count($data['bcc']) > 0) {
- $this->requestPostEmailViaApi($client, $data);
- }
- }
-
- public function requestPostEmailViaApi(Client $client, array $data): ResponseInterface
- {
- return $client->request('POST', 'https://api.brevo.com/v3/smtp/email', [
- 'headers' => [
- 'accept' => 'application/json',
- 'content-type' => 'application/json',
- 'api-key' => \Yii::$app->parameterBag->get('brevoApiKey'),
- ],
- 'body' => json_encode($data)
- ]);
- }
- }
|