浏览代码

[Administration] Communiquer > Email : gestion envois via Brevo

feature/rotating_product
Guillaume Bourgeois 6 个月前
父节点
当前提交
332521e885
共有 2 个文件被更改,包括 174 次插入5 次删除
  1. +5
    -3
      backend/controllers/CommunicateController.php
  2. +169
    -2
      backend/models/MailForm.php

+ 5
- 3
backend/controllers/CommunicateController.php 查看文件

@@ -148,8 +148,10 @@ class CommunicateController extends BackendController


if ($mailForm->load(\Yii::$app->request->post()) && $mailForm->validate()) {
$responseSendMail = $mailForm->sendEmail($users);
if ($responseSendMail->success()) {
$mailForm->sendEmail($users);
$this->setFlash('success', 'Votre email a bien été envoyé.');

/*if ($responseSendMail->success()) {
$this->setFlash('success', 'Votre email a bien été envoyé.');
} else {
$bodyResponseSendMail = $responseSendMail->getBody();
@@ -168,7 +170,7 @@ class CommunicateController extends BackendController
$messageError .= '<br />Problème détecté : ' . implode(',', $emailsErrorArray);
}
$this->setFlash('error', $messageError);
}
}*/

return $this->redirect(['email', 'idPointSale' => $idPointSale]);
}

+ 169
- 2
backend/models/MailForm.php 查看文件

@@ -47,6 +47,9 @@ use domain\Distribution\Distribution\DistributionModule;
use domain\Producer\Producer\ProducerModule;
use domain\Product\Product\Product;
use domain\Product\Product\ProductModule;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Yii;
use yii\base\Model;
use yii\helpers\Html;
@@ -92,11 +95,11 @@ class MailForm extends Model
* @param array $usersArray
* @param boolean $fromProducer
*/
public function sendEmail($usersArray, $fromProducer = true)
public function sendEmailMailjet($usersArray, $fromProducer = true)
{
$productModule = ProductModule::getInstance();
$producerModule = ProducerModule::getInstance();
$distributionModule =DistributionModule::getInstance();
$distributionModule = DistributionModule::getInstance();

$mj = new \Mailjet\Client(
Mailjet::getApiKey('public'),
@@ -248,4 +251,168 @@ Me désinscrire : ".$linkUnsubscribe;
return $response ;
}

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

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

$messageAutoHtml .= ' <style type="text/css">
h1, h2, h3, h4, h5, h6 {
padding: 0px;
margin: 0px;
margin-bottom: 10px;
}
p {
margin: 0px;
padding: 0px;
margin-bottom: 5px;
}
</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>' ;
$messageAutoText .= 'Suivez ce lien pour passer votre commande du '.$dateOrder.' :
'.$linkOrder ;

if($this->integrate_product_list) {
$productsArray = Product::find()
->where([
'id_producer' => GlobalParam::getCurrentProducerId(),
])
->andWhere('status >= :status')
->addParams(['status' => Product::STATUS_OFFLINE])
->innerJoinWith(['productDistribution' => function($query) use($distribution) {
$query->andOnCondition([
'product_distribution.id_distribution' => $distribution->id,
'product_distribution.active' => 1
]);
}])
->orderBy('product.name ASC')
->all();

if(count($productsArray) > 1) {
$messageAutoHtml .= '<br /><br />Produits disponibles : <br /><ul>' ;
$messageAutoText .= '

Produits disponibles :
' ;
foreach($productsArray as $product) {

$productDescription = $product->name ;
if(strlen($product->description)) {
$productDescription .= ' / '.$product->description ;
}
if($product->price) {
$productDescription .= ' / '.Price::format($productModule->getPriceWithTax($product)) ;
$productDescription .= ' ('. $productModule->getSolver()->strUnit($product, UnitDefinition::WORDING_UNIT).')' ;
}

$messageAutoText .= '- '.$productDescription.'
' ;
$messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ;
}
$messageAutoHtml .= '</ul>' ;
}
}
}
}

if($fromProducer) {
$producer = GlobalParam::getCurrentProducer() ;
$fromEmail = $producerModule->getProducerEmailPlatform($producer) ;
$fromName = $producer->name ;

$linkProducer = 'https://'.$producer->slug.'.souke.fr';
$linkUnsubscribe = Yii::$app->urlManagerProducer->createAbsoluteUrl(['newsletter/unsubscribe', 'slug_producer' => $producer->slug]);

// Message inscription newsletter
$messageAutoText .= "

--
Boutique : ".$linkProducer."
Me désinscrire : ".$linkUnsubscribe;

$messageAutoHtml .= "<br /><br />--<br>";
$messageAutoHtml .= "Boutique : <a href=\"".$linkProducer."\">".$linkProducer."</a><br>";
$messageAutoHtml .= "Me désinscrire : <a href=\"".$linkUnsubscribe."\">".$linkUnsubscribe."</a>";
}
else {
$fromEmail = 'contact@souke.fr' ;
$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
] ;

foreach($usersArray as $user) {

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

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

if(count($data['to']) > 0) {
$this->requestPostEmailViaBrevoApi($client, $data);
}
}

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

正在加载...
取消
保存