$mailForm->sendEmail($users); | $mailForm->sendEmail($users); | ||||
$this->setFlash('success', 'Votre email a bien été envoyé.'); | $this->setFlash('success', 'Votre email a bien été envoyé.'); | ||||
// @TODO : traiter les erreurs | |||||
/*if ($responseSendMail->success()) { | /*if ($responseSendMail->success()) { | ||||
$this->setFlash('success', 'Votre email a bien été envoyé.'); | $this->setFlash('success', 'Votre email a bien été envoyé.'); | ||||
} else { | } else { |
]; | ]; | ||||
} | } | ||||
/** | |||||
* 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(); | $productModule = ProductModule::getInstance(); | ||||
$producerModule = ProducerModule::getInstance(); | $producerModule = ProducerModule::getInstance(); | ||||
$distributionModule = DistributionModule::getInstance(); | $distributionModule = DistributionModule::getInstance(); | ||||
$mj = new \Mailjet\Client( | |||||
Mailjet::getApiKey('public'), | |||||
Mailjet::getApiKey('private'), | |||||
true, | |||||
['version' => 'v3.1'] | |||||
); | |||||
$body = ['Messages' => []] ; | |||||
$messageAutoText = '' ; | $messageAutoText = '' ; | ||||
$messageAutoHtml = '' ; | $messageAutoHtml = '' ; | ||||
</style>'; | </style>'; | ||||
if($this->id_distribution) { | if($this->id_distribution) { | ||||
$messageAutoText = ' | $messageAutoText = ' | ||||
' ; | ' ; | ||||
$messageAutoHtml .= '<br /><br />' ; | $messageAutoHtml .= '<br /><br />' ; | ||||
$distribution = Distribution::searchOne(['id' => $this->id_distribution]) ; | $distribution = Distribution::searchOne(['id' => $this->id_distribution]) ; | ||||
if($distribution) { | if($distribution) { | ||||
$linkOrder = $distributionModule->getLinkOrder($distribution); | $linkOrder = $distributionModule->getLinkOrder($distribution); | ||||
$dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ; | $dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ; | ||||
$messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ; | $messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ; | ||||
$fromName = 'Souke' ; | $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(); | $productModule = ProductModule::getInstance(); | ||||
$producerModule = ProducerModule::getInstance(); | $producerModule = ProducerModule::getInstance(); | ||||
$distributionModule = DistributionModule::getInstance(); | $distributionModule = DistributionModule::getInstance(); | ||||
$mj = new \Mailjet\Client( | |||||
Mailjet::getApiKey('public'), | |||||
Mailjet::getApiKey('private'), | |||||
true, | |||||
['version' => 'v3.1'] | |||||
); | |||||
$messageAutoText = '' ; | $messageAutoText = '' ; | ||||
$messageAutoHtml = '' ; | $messageAutoHtml = '' ; | ||||
margin: 0px; | margin: 0px; | ||||
margin-bottom: 10px; | margin-bottom: 10px; | ||||
} | } | ||||
p { | p { | ||||
margin: 0px; | margin: 0px; | ||||
padding: 0px; | padding: 0px; | ||||
$fromName = 'Souke' ; | $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) { | 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 ; | |||||
} | } | ||||
*/ | |||||
} | } |
<?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) | |||||
]); | |||||
} | |||||
} |
<?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); | |||||
} |
<?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]); | |||||
} | |||||
} |
<?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); | |||||
} | |||||
} |
'mailerService' => [ | 'mailerService' => [ | ||||
'class' => 'common\components\MailerService' | 'class' => 'common\components\MailerService' | ||||
], | ], | ||||
'bulkMailer' => [ | |||||
'class' => 'common\components\BulkMailer\BulkMailerProxy' | |||||
], | |||||
'view' => [ | 'view' => [ | ||||
'class' => 'yii\web\View', | 'class' => 'yii\web\View', | ||||
'renderers' => [ | 'renderers' => [ |
use domain\Setting\SettingDetails\Admin\AlertMessage\AdminSupportAlertMessageTypeSetting; | use domain\Setting\SettingDetails\Admin\AlertMessage\AdminSupportAlertMessageTypeSetting; | ||||
use domain\Setting\SettingDetails\Admin\General\AdministratorEmailAdminSetting; | use domain\Setting\SettingDetails\Admin\General\AdministratorEmailAdminSetting; | ||||
use domain\Setting\SettingDetails\Admin\General\AdministratorPhoneNumberAdminSetting; | 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\ForumFlarumUrlAdminSetting; | ||||
use domain\Setting\SettingDetails\Admin\General\MaximumNumberProducersAdminSetting; | use domain\Setting\SettingDetails\Admin\General\MaximumNumberProducersAdminSetting; | ||||
use domain\Setting\SettingDetails\Admin\General\SupportOnlineAdminSetting; | use domain\Setting\SettingDetails\Admin\General\SupportOnlineAdminSetting; | ||||
new AdministratorPhoneNumberAdminSetting(), | new AdministratorPhoneNumberAdminSetting(), | ||||
new MaximumNumberProducersAdminSetting(), | new MaximumNumberProducersAdminSetting(), | ||||
new ForumFlarumUrlAdminSetting(), | new ForumFlarumUrlAdminSetting(), | ||||
new BulkMailerAdminSetting(), | |||||
] | ] | ||||
], | ], | ||||
self::SECTION_ALERT_MESSAGE => [ | self::SECTION_ALERT_MESSAGE => [ |
<?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', | |||||
]); | |||||
} | |||||
} |