aboutsummaryrefslogtreecommitdiff
path: root/src/service/EmailService.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/EmailService.php')
-rw-r--r--src/service/EmailService.php102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/service/EmailService.php b/src/service/EmailService.php
new file mode 100644
index 0000000..6f4e93d
--- /dev/null
+++ b/src/service/EmailService.php
@@ -0,0 +1,102 @@
1<?php
2// src/service/EmailService.php
3
4declare(strict_types=1);
5
6use PHPMailer\PHPMailer\PHPMailer;
7//use PHPMailer\PHPMailer\Exception;
8use Doctrine\ORM\EntityManager;
9use App\Entity\Email;
10use App\Entity\NodeData;
11
12class EmailService
13{
14 const KEEP_EMAILS_DEFAULT = false;
15
16 static public function send(EntityManager $entityManager, NodeData $form_data, bool $test_email, string $name = '', string $email = '', string $message = ''): bool
17 {
18 $mail = new PHPMailer(true); // true => exceptions
19 $mail->CharSet = 'UTF-8';
20
21 $smtp_host = $form_data->getData()['smtp_host'] ?? Config::$smtp_host;
22 $smtp_secure = $form_data->getData()['smtp_secure'] ?? Config::$smtp_secure;
23 $smtp_username = $form_data->getData()['smtp_username'] ?? Config::$smtp_username;
24 $smtp_password = $form_data->getData()['smtp_password'] ?? Config::$smtp_password;
25 $email_from = $form_data->getData()['email_from'] ?? Config::$email_from; // une adresse bidon est donnée à setFrom()
26 $email_from_name = $form_data->getData()['email_from_name'] ?? Config::$email_from_name; // = site web
27 $email_dest = $form_data->getData()['email_dest'] ?? Config::$email_dest;
28 $email_dest_name = $form_data->getData()['email_dest_name'] ?? Config::$email_dest_name; // = destinataire formulaire
29
30 try{
31 // Paramètres du serveur
32 $mail->isSMTP();
33 $mail->Host = $smtp_host;
34 $mail->SMTPAuth = true;
35 $mail->Port = 25;
36
37 if($mail->SMTPAuth){
38 $mail->Username = $smtp_username; // e-mail
39 $mail->Password = $smtp_password;
40 $mail->SMTPSecure = $smtp_secure; // tls (starttls) ou ssl (smtps)
41 if($mail->SMTPSecure === 'tls'){
42 $mail->Port = 587;
43 }
44 elseif($mail->SMTPSecure === 'ssl'){
45 $mail->Port = 465;
46 }
47 }
48 //var_dump($mail->smtpConnect());die; // test de connexion
49
50 // Expéditeur et destinataire
51 // $email_from, $email_from_name et $email_dest_name sont modifiables uniquement dans le config.ini pour l'instant
52 $mail->setFrom(strtolower($email_from), $email_from_name);
53 $mail->addAddress(strtolower($email_dest), $email_dest_name);
54
55 // Contenu
56 $mail->isHTML(true);
57 if($test_email){
58 $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web";
59 }
60 else{
61 $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web';
62 }
63 $mail->Body = $message;
64 $mail->AltBody = $message;
65
66 $mail->send();
67
68 // copie en BDD
69 if(!$test_email && ($form_data->getData()['keep_emails'] ?? self::KEEP_EMAILS_DEFAULT)){
70 $db_email = new Email($name, $email, Config::$email_dest, $message, $form_data);
71 $entityManager->persist($db_email);
72 self::updateLastContactDate($entityManager, $email);
73 $entityManager->flush();
74 }
75
76 return true;
77 }
78 catch(Exception $e){
79 echo "Le message n'a pas pu être envoyé. Erreur : {$e} <br> {$mail->ErrorInfo}";
80 return false;
81 }
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 }
102} \ No newline at end of file