From f27cca08ff786ceff1870147f6d6bad14d71117f Mon Sep 17 00:00:00 2001 From: polo Date: Fri, 23 Oct 2026 00:47:32 +0200 Subject: =?UTF-8?q?dossiers=20avec=20majuscule,=20namespace=20pour=20toute?= =?UTF-8?q?s=20les=20classes,=20autoload=20psr-4,=20UserDataService::creat?= =?UTF-8?q?eZip=20d=C3=A9plac=C3=A9=20dans=20Backup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Service/EmailService.php | 106 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/Service/EmailService.php (limited to 'src/Service/EmailService.php') diff --git a/src/Service/EmailService.php b/src/Service/EmailService.php new file mode 100644 index 0000000..38b4cf8 --- /dev/null +++ b/src/Service/EmailService.php @@ -0,0 +1,106 @@ + exceptions + $mail->CharSet = 'UTF-8'; + + $smtp_host = $form_data->getData()['smtp_host'] ?? Config::$smtp_host; + $smtp_secure = $form_data->getData()['smtp_secure'] ?? Config::$smtp_secure; + $smtp_username = $form_data->getData()['smtp_username'] ?? Config::$smtp_username; + $smtp_password = $form_data->getData()['smtp_password'] ?? Config::$smtp_password; + + $email_from = $form_data->getData()['email_from'] ?? Config::$email_from; // adresse de l'expéditeur affichée + $email_from = empty($email_from) ? $smtp_username : $email_from; // si vide + $email_from_name = $form_data->getData()['email_from_name'] ?? Config::$email_from_name; // = nom de l'expéditeur affiché + $email_dest = $form_data->getData()['email_dest'] ?? Config::$email_dest; + $email_dest_name = $form_data->getData()['email_dest_name'] ?? Config::$email_dest_name; // = destinataire formulaire + + try{ + // Paramètres du serveur + $mail->isSMTP(); + $mail->Host = $smtp_host; + $mail->SMTPAuth = true; + $mail->Port = 25; + + if($mail->SMTPAuth){ + $mail->Username = $smtp_username; // e-mail + $mail->Password = $smtp_password; + $mail->SMTPSecure = $smtp_secure; // tls (starttls) ou ssl (smtps) + if($mail->SMTPSecure === 'tls'){ + $mail->Port = 587; + } + elseif($mail->SMTPSecure === 'ssl'){ + $mail->Port = 465; + } + } + //var_dump($mail->smtpConnect());die; // test de connexion + + // Expéditeur et destinataire + // $email_from, $email_from_name et $email_dest_name sont modifiables uniquement dans le config.ini pour l'instant + $mail->setFrom(strtolower($email_from), $email_from_name); + $mail->addAddress(strtolower($email_dest), $email_dest_name); + + // Contenu + $mail->isHTML(true); + if($test_email){ + $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; + } + else{ + $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; + } + $mail->Body = $message; + $mail->AltBody = $message; + + $mail->send(); + + // copie en BDD + if(!$test_email && ($form_data->getData()['keep_emails'] ?? self::KEEP_EMAILS_DEFAULT)){ + $db_email = new Email($name, $email, $email_dest, $message, $form_data); + $entityManager->persist($db_email); + self::updateLastContactDate($entityManager, $email); + $entityManager->flush(); + } + + return true; + } + catch(\Exception $e){ + echo "Le message n'a pas pu être envoyé. Erreur : {$e}
{$mail->ErrorInfo}"; + return false; + } + } + + static public function updateLastContactDate(EntityManager $entityManager, string $sender): void + { + foreach($entityManager->getRepository('App\Entity\Email')->findAll() as $email){ + $email->getSenderAddress() === $sender ? $email->updateLastContactDate() : null; + } + } + + // peut être appelée par bin/clean_emails_cron.php + static public function cleanEmails(EntityManager $entityManager): void + { + $emails = $entityManager->getRepository('App\Entity\Email')->findAll(); + foreach($emails as $email){ + if($email->getDeletionDate() < new \DateTime()){ + $entityManager->remove($email); + } + } + $entityManager->flush(); + } +} \ No newline at end of file -- cgit v1.2.3