|
|
@@ -6,8 +6,10 @@ namespace App\Service\Mailer;
|
|
|
use App\Entity\Message\ReportEmail;
|
|
|
use App\Enum\Core\EmailSendingTypeEnum;
|
|
|
use App\Enum\Message\MessageStatusEnum;
|
|
|
+use App\Enum\Utils\EnvironnementVarEnum;
|
|
|
use App\Service\Mailer\Model\MailerModelInterface;
|
|
|
use App\Service\ServiceIterator\Mailer\BuilderIterator;
|
|
|
+use App\Service\Utils\Environnement;
|
|
|
use App\Service\Utils\StringsUtils;
|
|
|
use App\Enum\Message\ReportMessageSatusEnum;
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
|
@@ -28,7 +30,8 @@ class Mailer
|
|
|
private string $opentalentNoReplyEmailAddress,
|
|
|
private BuilderIterator $builderIterator,
|
|
|
private StringsUtils $stringsUtils,
|
|
|
- private EntityManagerInterface $entityManager
|
|
|
+ private EntityManagerInterface $entityManager,
|
|
|
+ private Environnement $environnement
|
|
|
)
|
|
|
{
|
|
|
}
|
|
|
@@ -42,22 +45,33 @@ class Mailer
|
|
|
* @param MailerModelInterface $mailerModel
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
- public function main(MailerModelInterface $mailerModel): void{
|
|
|
+ public function main(MailerModelInterface $mailerModel): ArrayCollection{
|
|
|
$builderService = $this->builderIterator->getBuilderFor($mailerModel);
|
|
|
$emailsCollection = $builderService->build($mailerModel);
|
|
|
|
|
|
- // @todo
|
|
|
- //$emailsCollection = $this->reduceEmailsCollectionInPreproduction($emailsCollection);
|
|
|
+ $emailsCollection = $this->reduceEmailsCollectionInPreproduction($emailsCollection);
|
|
|
|
|
|
+ /** @var Email $email */
|
|
|
foreach ($emailsCollection as $email){
|
|
|
+ //si l'email n'a pas de destinataire, on ne l'envoi pas...
|
|
|
+ if(empty($email->getEmailRecipients())){
|
|
|
+ $email->getEmailEntity()->setStatus(MessageStatusEnum::FAILED()->getValue());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ //Envoi du mail
|
|
|
$this->send($email);
|
|
|
|
|
|
+ //Persistance de l'entité Email
|
|
|
$this->persistEmailEntity($email);
|
|
|
}
|
|
|
|
|
|
+ //Envoi du rapport
|
|
|
$this->report($emailsCollection);
|
|
|
|
|
|
$this->entityManager->flush();
|
|
|
+
|
|
|
+ return $emailsCollection;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -70,11 +84,14 @@ class Mailer
|
|
|
|
|
|
$this->addRecipients($symfonyMail, $email);
|
|
|
|
|
|
+ $this->setAntiSpam($email, $symfonyMail->getTo());
|
|
|
+
|
|
|
$this->addHeaders($symfonyMail, $email);
|
|
|
|
|
|
// @todo
|
|
|
//$this->addAttachments();
|
|
|
|
|
|
+
|
|
|
//On tente d'envoyer
|
|
|
try {
|
|
|
$this->symfonyMailer->send($symfonyMail);
|
|
|
@@ -152,7 +169,7 @@ class Mailer
|
|
|
* @param ArrayCollection $emailsCollection
|
|
|
*/
|
|
|
public function reduceEmailsCollectionInPreproduction(ArrayCollection $emailsCollection): ArrayCollection {
|
|
|
- if($_ENV['env'] === 'prod') return $emailsCollection;
|
|
|
+ if($this->environnement->get(EnvironnementVarEnum::APP_ENV()->getValue()) === 'prod') return $emailsCollection;
|
|
|
|
|
|
$startEmails = $emailsCollection->slice(0, 10);
|
|
|
$endEmails = $emailsCollection->slice($emailsCollection->count() - 11, 10);
|
|
|
@@ -165,11 +182,24 @@ class Mailer
|
|
|
* @param Email $email
|
|
|
*/
|
|
|
public function addHeaders(SymfonyEmail $symfonyMail, Email $email){
|
|
|
- // $symfonyMail->getHeaders()->addTextHeader('List-Unsubscribe','mailto:'.$email->getOriginator().'?subject=désabonnement');
|
|
|
-
|
|
|
+ $symfonyMail->getHeaders()->addTextHeader('List-Unsubscribe','mailto:'.$email->getFrom().'?subject=désabonnement');
|
|
|
$symfonyMail->getHeaders()->addTextHeader('X-ID-OT', $email->getEmailEntity()->getUniqueSendId());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * On change le #__#ANTISPAM_PERSON_EMAIL#__# par la liste des emails afin d'éviter le spamming sur l'envoi en masse
|
|
|
+ * @param Email $email
|
|
|
+ * @param array $to
|
|
|
+ */
|
|
|
+ public function setAntiSpam(Email $email, array $addresses){
|
|
|
+ // map des Address pour ne conserver qu'un tableau d'email
|
|
|
+ $to = array_map(function(Address $address){
|
|
|
+ return $address->getAddress();
|
|
|
+ }, $addresses);
|
|
|
+
|
|
|
+ $email->setContent(str_replace('#__#ANTISPAM_PERSON_EMAIL#__#', implode(',', $to), $email->getContent()));
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Création du Mail qui sera envoyé via le Mailer de Symfony
|
|
|
@@ -177,8 +207,12 @@ class Mailer
|
|
|
* @return SymfonyEmail
|
|
|
*/
|
|
|
public function createSymfonyEmail(Email $email): SymfonyEmail{
|
|
|
+ $addressMailFrom = new Address($email->getFrom(), $email->getEmailEntity()->getAuthor()->getPerson()->getFullName());
|
|
|
+
|
|
|
return (new SymfonyEmail())
|
|
|
- ->from($email->getFrom())
|
|
|
+ ->from($addressMailFrom)
|
|
|
+ ->replyTo($addressMailFrom)
|
|
|
+ ->returnPath(Address::create("mail.report@opentalent.fr"))
|
|
|
->subject($email->getEmailEntity()->getAbout())
|
|
|
->html($email->getContent())
|
|
|
->text($this->stringsUtils->convertHtmlToText($email->getContent()))
|
|
|
@@ -191,19 +225,25 @@ class Mailer
|
|
|
* @param Address $addressesMail
|
|
|
*/
|
|
|
public function addRecipients(SymfonyEmail $symfonyMail, Email $email): void{
|
|
|
+ $allReadySend = [];
|
|
|
+
|
|
|
foreach ($email->getEmailRecipients() as $emailRecipient){
|
|
|
$addressMail = new Address($emailRecipient->getEmailAddress(), $emailRecipient->getName());
|
|
|
-
|
|
|
- switch($emailRecipient->getSendType()){
|
|
|
- case EmailSendingTypeEnum::TO()->getValue():
|
|
|
- $symfonyMail->addTo($addressMail);
|
|
|
- break;
|
|
|
- case EmailSendingTypeEnum::BBC()->getValue():
|
|
|
- $symfonyMail->addBcc($addressMail);
|
|
|
- break;
|
|
|
- case EmailSendingTypeEnum::CC()->getValue():
|
|
|
- $symfonyMail->addCc($addressMail);
|
|
|
- break;
|
|
|
+ //On envoi pas en double
|
|
|
+ if(!in_array($addressMail, $allReadySend)){
|
|
|
+ $allReadySend[] = $addressMail;
|
|
|
+
|
|
|
+ switch($emailRecipient->getSendType()){
|
|
|
+ case EmailSendingTypeEnum::TO()->getValue():
|
|
|
+ $symfonyMail->addTo($addressMail);
|
|
|
+ break;
|
|
|
+ case EmailSendingTypeEnum::BBC()->getValue():
|
|
|
+ $symfonyMail->addBcc($addressMail);
|
|
|
+ break;
|
|
|
+ case EmailSendingTypeEnum::CC()->getValue():
|
|
|
+ $symfonyMail->addCc($addressMail);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|