- <?php
-
- namespace common\components\BulkMailer;
-
- use domain\Communication\Email\Email;
- use domain\User\User\UserSolver;
- use GuzzleHttp\Client;
- use Psr\Http\Message\ResponseInterface;
-
- class BulkMailerBrevo implements BulkMailerInterface
- {
- public function sendEmail(Email $email, array $usersArray)
- {
- $userSolver = UserSolver::getInstance();
- $client = new Client();
- $data = [
- 'sender' => [
- 'name' => $email->getFromName(),
- 'email' => $email->getFromEmail()
- ],
- 'bcc' => [],
- 'subject' => $email->getSubject(),
- 'htmlContent' => $email->getHtmlContent()
- ] ;
-
- foreach($usersArray 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)
- ]);
- }
- }
|