aboutsummaryrefslogtreecommitdiff
path: root/src/EmailService.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/EmailService.php')
-rw-r--r--src/EmailService.php30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/EmailService.php b/src/EmailService.php
index 1bcca0f..5d1b2eb 100644
--- a/src/EmailService.php
+++ b/src/EmailService.php
@@ -11,6 +11,8 @@ use App\Entity\NodeData;
11 11
12class EmailService 12class EmailService
13{ 13{
14 const KEEP_EMAILS_DEFAULT = false;
15
14 static public function send(EntityManager $entityManager, NodeData $form_data, bool $test_email, string $name = '', string $email = '', string $message = ''): bool 16 static public function send(EntityManager $entityManager, NodeData $form_data, bool $test_email, string $name = '', string $email = '', string $message = ''): bool
15 { 17 {
16 $mail = new PHPMailer(true); // true => exceptions 18 $mail = new PHPMailer(true); // true => exceptions
@@ -64,17 +66,37 @@ class EmailService
64 $mail->send(); 66 $mail->send();
65 67
66 // copie en BDD 68 // copie en BDD
67 if(!$test_email){ 69 if(!$test_email && ($form_data->getData()['keep_emails'] ?? self::KEEP_EMAILS_DEFAULT)){
68 $db_email = new Email($email, Config::$email_dest, $message); 70 $db_email = new Email($name, $email, Config::$email_dest, $message);
69 $entityManager->persist($db_email); 71 $entityManager->persist($db_email);
72 self::updateLastContactDate($entityManager, $email);
70 $entityManager->flush(); 73 $entityManager->flush();
71 } 74 }
72 75
73 return true; 76 return true;
74 } 77 }
75 catch(Exception $e){ 78 catch(Exception $e){
76 return false; 79 echo "Le message n'a pas pu être envoyé. Erreur : {$e} <br> {$mail->ErrorInfo}";
77 //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}"; 80 return false;
78 } 81 }
79 } 82 }
83
84 static public function updateLastContactDate(EntityManager $entityManager, string $sender): void
85 {
86 foreach($entityManager->getRepository('App\Entity\Email')->findAll() as $email){
87 $email->getSenderAddress() === $sender ? $email->updateLastContactDate() : null;
88 }
89 }
90
91 // peut être appelée par bin/clean_emails_cron.php
92 static public function cleanEmails(EntityManager $entityManager): void
93 {
94 $emails = $entityManager->getRepository('App\Entity\Email')->findAll();
95 foreach($emails as $email){
96 if($email->getDeletionDate() < new \DateTime()){
97 $entityManager->remove($email);
98 }
99 }
100 $entityManager->flush();
101 }
80} \ No newline at end of file 102} \ No newline at end of file