'Champs obligatoire'],
[['integrate_product_list'], 'boolean'],
[['id_distribution'],'integer']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'subject' => 'Sujet',
'message' => 'Message',
'id_distribution' => 'Distribution',
'integrate_product_list' => 'Intégrer la liste des produits au message'
];
}
/**
* Envoie un email aux utilisateurs définis en paramètre.
*
* @param array $usersArray
* @param boolean $fromProducer
*/
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']
);
$body = ['Messages' => []] ;
$messageAutoText = '' ;
$messageAutoHtml = '' ;
$messageAutoHtml .= ' ';
if($this->id_distribution) {
$messageAutoText = '
' ;
$messageAutoHtml .= '
' ;
$distribution = Distribution::searchOne(['id' => $this->id_distribution]) ;
if($distribution) {
$linkOrder = $distributionModule->getLinkOrder($distribution);
$dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
$messageAutoHtml .= 'Passer ma commande du '.$dateOrder.'' ;
$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 .= '
Produits disponibles :
' ;
$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 .= '- '.Html::encode($productDescription).'
' ;
}
$messageAutoHtml .= '
' ;
}
}
}
}
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 .= "
--
";
$messageAutoHtml .= "Boutique : ".$linkProducer."
";
$messageAutoHtml .= "Me désinscrire : ".$linkUnsubscribe."";
}
else {
$fromEmail = 'contact@souke.fr' ;
$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]);
}
$success = (isset($response) && $response) ? $response->success() : false ;
if(!$success) {
Yii::error($response->getBody(), 'Mailjet');
}
return $response ;
}
public function sendEmail($usersArray, $fromProducer = true)
{
$productModule = ProductModule::getInstance();
$producerModule = ProducerModule::getInstance();
$distributionModule = DistributionModule::getInstance();
$messageAutoText = '' ;
$messageAutoHtml = '' ;
$messageAutoHtml .= ' ';
if($this->id_distribution) {
$messageAutoText = '
' ;
$messageAutoHtml .= '
' ;
$distribution = Distribution::searchOne(['id' => $this->id_distribution]) ;
if($distribution) {
$linkOrder = $distributionModule->getLinkOrder($distribution);
$dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
$messageAutoHtml .= 'Passer ma commande du '.$dateOrder.'' ;
$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 .= '
Produits disponibles :
' ;
$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 .= '- '.Html::encode($productDescription).'
' ;
}
$messageAutoHtml .= '
' ;
}
}
}
}
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 .= "
--
";
$messageAutoHtml .= "Boutique : ".$linkProducer."
";
$messageAutoHtml .= "Me désinscrire : ".$linkUnsubscribe."";
}
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)
]);
}
}