Browse Source

Merge branch 'develop'

master
Guillaume Bourgeois 7 months ago
parent
commit
bca326cb8b
9 changed files with 222 additions and 107 deletions
  1. +1
    -0
      backend/controllers/CommunicateController.php
  2. +50
    -107
      backend/models/MailForm.php
  3. +52
    -0
      common/components/BulkMailer/BulkMailerBrevo.php
  4. +11
    -0
      common/components/BulkMailer/BulkMailerInterface.php
  5. +53
    -0
      common/components/BulkMailer/BulkMailerMailjet.php
  6. +29
    -0
      common/components/BulkMailer/BulkMailerProxy.php
  7. +3
    -0
      common/config/main.php
  8. +2
    -0
      domain/Setting/SettingDetails/Admin/AdminSettingDefinition.php
  9. +21
    -0
      domain/Setting/SettingDetails/Admin/General/BulkMailerAdminSetting.php

+ 1
- 0
backend/controllers/CommunicateController.php View File

@@ -151,6 +151,7 @@ class CommunicateController extends BackendController
$mailForm->sendEmail($users);
$this->setFlash('success', 'Votre email a bien été envoyé.');

// @TODO : traiter les erreurs
/*if ($responseSendMail->success()) {
$this->setFlash('success', 'Votre email a bien été envoyé.');
} else {

+ 50
- 107
backend/models/MailForm.php View File

@@ -89,27 +89,12 @@ class MailForm extends Model
];
}

/**
* Envoie un email aux utilisateurs définis en paramètre.
*
* @param array $usersArray
* @param boolean $fromProducer
*/
public function sendEmailMailjet($usersArray, $fromProducer = true)
public function sendEmail($contactsArray, $fromProducer = true)
{
$productModule = ProductModule::getInstance();
$producerModule = ProducerModule::getInstance();
$distributionModule = DistributionModule::getInstance();

$mj = new \Mailjet\Client(
Mailjet::getApiKey('public'),
Mailjet::getApiKey('private'),
true,
['version' => 'v3.1']
);

$body = ['Messages' => []] ;
$messageAutoText = '' ;
$messageAutoHtml = '' ;

@@ -128,16 +113,16 @@ class MailForm extends Model
</style>';

if($this->id_distribution) {
$messageAutoText = '

' ;
$messageAutoHtml .= '<br /><br />' ;
$distribution = Distribution::searchOne(['id' => $this->id_distribution]) ;
if($distribution) {
$linkOrder = $distributionModule->getLinkOrder($distribution);
$dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
$messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ;
@@ -211,53 +196,28 @@ Me désinscrire : ".$linkUnsubscribe;
$fromName = 'Souke' ;
}

// Tests
/*$usersArray = [
['email' => 'contact@guillaumebourgeois.fr', 'name' => '', 'lastname' => '']
];*/

foreach($usersArray as $user) {
$body['Messages'][] = [
'From' => [
'Email' => $fromEmail,
'Name' => $fromName
],
'To' => [
[
'Email' => $user['email'],
'Name' => $user['name'].' '.$user['lastname']
]
],
'Subject' => $this->subject,
'TextPart' => $this->message.$messageAutoText,
'HTMLPart' => nl2br($this->message).$messageAutoHtml
] ;

if(count($body['Messages']) == 50) {
$response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]);
$body['Messages'] = [] ;
}
}

if(count($body['Messages']) > 0) {
$response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]);
}
$subject = $this->subject;
$htmlContent = nl2br($this->message).$messageAutoHtml;
$textContent = $this->message.$messageAutoText;

$success = (isset($response) && $response) ? $response->success() : false ;
if(!$success) {
Yii::error($response->getBody(), 'Mailjet');
}
return $response ;
Yii::$app->bulkMailer->sendEmails($contactsArray, $fromName, $fromEmail, $subject, $htmlContent, $textContent);
}

public function sendEmail($usersArray, $fromProducer = true)
/*

public function sendEmailMailjet($usersArray, $fromProducer = true)
{
$productModule = ProductModule::getInstance();
$producerModule = ProducerModule::getInstance();
$distributionModule = DistributionModule::getInstance();

$mj = new \Mailjet\Client(
Mailjet::getApiKey('public'),
Mailjet::getApiKey('private'),
true,
['version' => 'v3.1']
);

$messageAutoText = '' ;
$messageAutoHtml = '' ;

@@ -267,7 +227,7 @@ Me désinscrire : ".$linkUnsubscribe;
margin: 0px;
margin-bottom: 10px;
}
p {
margin: 0px;
padding: 0px;
@@ -359,60 +319,43 @@ Me désinscrire : ".$linkUnsubscribe;
$fromName = 'Souke' ;
}

$htmlContent = nl2br($this->message).$messageAutoHtml;

// Tests
/*$usersArray = [
['email' => 'contact@guillaumebourgeois.fr', 'name' => '', 'lastname' => '']
];*/

// Envoi via Brevo
$this->sendEmailsViaBrevoApi($usersArray, $fromName, $fromEmail, $htmlContent);

// Envoi via Mailjet
//$this->sendEmailsViaMailjetApi($usersArray, $fromName, $fromEmail, $htmlContent);
}

public function sendEmailsViaBrevoApi(array $usersArray, string $fromName, string $fromEmail, string $htmlContent)
{
$client = new Client();
$data = [
'sender' => [
'name' => $fromName,
'email' => $fromEmail
],
'to' => [],
'subject' => $this->subject,
'htmlContent' => $htmlContent
] ;
$body = ['Messages' => []] ;

foreach($usersArray as $user) {
$body['Messages'][] = [
'From' => [
'Email' => $fromEmail,
'Name' => $fromName
],
'To' => [
[
'Email' => $user['email'],
'Name' => $user['name'].' '.$user['lastname']
]
],
'Subject' => $this->subject,
'TextPart' => $this->message.$messageAutoText,
'HTMLPart' => nl2br($this->message).$messageAutoHtml
] ;

$data['to'][] = [
'name' => $user['name'].' '.$user['lastname'],
'email' => $user['email']
];

if(count($data['to']) == 50) {
$this->requestPostEmailViaBrevoApi($client, $data);
$data['to'] = [] ;
if(count($body['Messages']) == 50) {
$response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]);
$body['Messages'] = [] ;
}
}

if(count($data['to']) > 0) {
$this->requestPostEmailViaBrevoApi($client, $data);
if(count($body['Messages']) > 0) {
$response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]);
}

$success = (isset($response) && $response) ? $response->success() : false ;

if(!$success) {
Yii::error($response->getBody(), 'Mailjet');
}
}

public function requestPostEmailViaBrevoApi(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' => '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',
],
'body' => json_encode($data)
]);
return $response ;
}

*/
}

+ 52
- 0
common/components/BulkMailer/BulkMailerBrevo.php View File

@@ -0,0 +1,52 @@
<?php

namespace common\components\BulkMailer;

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)
{
$client = new Client();
$data = [
'sender' => [
'name' => $fromName,
'email' => $fromEmail
],
'to' => [],
'subject' => $subject,
'htmlContent' => $htmlContent
] ;

foreach($contactsArray as $user) {

$data['to'][] = [
'name' => $user['name'].' '.$user['lastname'],
'email' => $user['email']
];

if(count($data['to']) == 50) {
$this->requestPostEmailViaApi($client, $data);
$data['to'] = [] ;
}
}

if(count($data['to']) > 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)
]);
}
}

+ 11
- 0
common/components/BulkMailer/BulkMailerInterface.php View File

@@ -0,0 +1,11 @@
<?php

namespace common\components\BulkMailer;

interface BulkMailerInterface
{
const MAILJET = 'mailjet';
const BREVO = 'brevo';

public function sendEmails(array $contactsArray, string $fromName, string $fromEmail, string $subject, string $htmlContent, string $textContent = null);
}

+ 53
- 0
common/components/BulkMailer/BulkMailerMailjet.php View File

@@ -0,0 +1,53 @@
<?php

namespace common\components\BulkMailer;

use common\helpers\Mailjet;
use Mailjet\Client;

class BulkMailerMailjet implements BulkMailerInterface
{
public function sendEmails(array $contactsArray, string $fromName, string $fromEmail, string $subject, string $htmlContent, string $textContent = null)
{
$client = new Client(
Mailjet::getApiKey('public'),
Mailjet::getApiKey('private'),
true,
['version' => 'v3.1']
);

$data = ['Messages' => []] ;

foreach($contactsArray as $user) {
$data['Messages'][] = [
'From' => [
'Email' => $fromEmail,
'Name' => $fromName
],
'To' => [
[
'Email' => $user['email'],
'Name' => $user['name'].' '.$user['lastname']
]
],
'Subject' => $subject,
'HTMLPart' => $htmlContent,
'TextPart' => $textContent
] ;

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

+ 29
- 0
common/components/BulkMailer/BulkMailerProxy.php View File

@@ -0,0 +1,29 @@
<?php

namespace common\components\BulkMailer;

use domain\Setting\AdminSettingBag;
use yii\base\ErrorException;

class BulkMailerProxy implements BulkMailerInterface
{
public function getBulkMailer(): BulkMailerInterface
{
$bulkMailerSetting = AdminSettingBag::getInstance()->get('bulkMailer');

if($bulkMailerSetting == BulkMailerInterface::MAILJET) {
return new BulkMailerMailjet();
}
elseif($bulkMailerSetting == BulkMailerInterface::BREVO) {
return new BulkMailerBrevo();
}
else {
throw new ErrorException("Le mailer pour les emails en masse n'est pas défini dans les paramètres admin.");
}
}

public function sendEmails(array $contactsArray, string $fromName, string $fromEmail, string $subject, string $htmlContent, string $textContent = null)
{
$this->getBulkMailer()->sendEmails($contactsArray, $fromName, $fromEmail, $subject, $htmlContent, $textContent);
}
}

+ 3
- 0
common/config/main.php View File

@@ -126,6 +126,9 @@ return [
'mailerService' => [
'class' => 'common\components\MailerService'
],
'bulkMailer' => [
'class' => 'common\components\BulkMailer\BulkMailerProxy'
],
'view' => [
'class' => 'yii\web\View',
'renderers' => [

+ 2
- 0
domain/Setting/SettingDetails/Admin/AdminSettingDefinition.php View File

@@ -14,6 +14,7 @@ use domain\Setting\SettingDetails\Admin\AlertMessage\AdminSupportAlertMessageTit
use domain\Setting\SettingDetails\Admin\AlertMessage\AdminSupportAlertMessageTypeSetting;
use domain\Setting\SettingDetails\Admin\General\AdministratorEmailAdminSetting;
use domain\Setting\SettingDetails\Admin\General\AdministratorPhoneNumberAdminSetting;
use domain\Setting\SettingDetails\Admin\General\BulkMailerAdminSetting;
use domain\Setting\SettingDetails\Admin\General\ForumFlarumUrlAdminSetting;
use domain\Setting\SettingDetails\Admin\General\MaximumNumberProducersAdminSetting;
use domain\Setting\SettingDetails\Admin\General\SupportOnlineAdminSetting;
@@ -38,6 +39,7 @@ class AdminSettingDefinition extends SettingDefinition
new AdministratorPhoneNumberAdminSetting(),
new MaximumNumberProducersAdminSetting(),
new ForumFlarumUrlAdminSetting(),
new BulkMailerAdminSetting(),
]
],
self::SECTION_ALERT_MESSAGE => [

+ 21
- 0
domain/Setting/SettingDetails/Admin/General/BulkMailerAdminSetting.php View File

@@ -0,0 +1,21 @@
<?php

namespace domain\Setting\SettingDetails\Admin\General;

use common\components\BulkMailer\BulkMailerInterface;
use domain\Setting\SettingDetails\AbstractSettingDetail;

class BulkMailerAdminSetting extends AbstractSettingDetail
{
public function __construct()
{
$this
->setName('bulkMailer')
->setLabel("Mailer emails en masse")
->setTypeString()
->setFormTypeSelect([
BulkMailerInterface::MAILJET => 'Mailjet',
BulkMailerInterface::BREVO => 'Brevo',
]);
}
}

Loading…
Cancel
Save