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.php104
1 files changed, 0 insertions, 104 deletions
diff --git a/src/service/EmailService.php b/src/service/EmailService.php
deleted file mode 100644
index 1579bdb..0000000
--- a/src/service/EmailService.php
+++ /dev/null
@@ -1,104 +0,0 @@
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\EmailForm;
11
12class EmailService
13{
14 const KEEP_EMAILS_DEFAULT = false;
15
16 static public function send(EntityManager $entityManager, EmailForm $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
26 $email_from = $form_data->getData()['email_from'] ?? Config::$email_from; // adresse de l'expéditeur affichée
27 $email_from = empty($email_from) ? $smtp_username : $email_from; // si vide
28 $email_from_name = $form_data->getData()['email_from_name'] ?? Config::$email_from_name; // = nom de l'expéditeur affiché
29 $email_dest = $form_data->getData()['email_dest'] ?? Config::$email_dest;
30 $email_dest_name = $form_data->getData()['email_dest_name'] ?? Config::$email_dest_name; // = destinataire formulaire
31
32 try{
33 // Paramètres du serveur
34 $mail->isSMTP();
35 $mail->Host = $smtp_host;
36 $mail->SMTPAuth = true;
37 $mail->Port = 25;
38
39 if($mail->SMTPAuth){
40 $mail->Username = $smtp_username; // e-mail
41 $mail->Password = $smtp_password;
42 $mail->SMTPSecure = $smtp_secure; // tls (starttls) ou ssl (smtps)
43 if($mail->SMTPSecure === 'tls'){
44 $mail->Port = 587;
45 }
46 elseif($mail->SMTPSecure === 'ssl'){
47 $mail->Port = 465;
48 }
49 }
50 //var_dump($mail->smtpConnect());die; // test de connexion
51
52 // Expéditeur et destinataire
53 // $email_from, $email_from_name et $email_dest_name sont modifiables uniquement dans le config.ini pour l'instant
54 $mail->setFrom(strtolower($email_from), $email_from_name);
55 $mail->addAddress(strtolower($email_dest), $email_dest_name);
56
57 // Contenu
58 $mail->isHTML(true);
59 if($test_email){
60 $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web";
61 }
62 else{
63 $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web';
64 }
65 $mail->Body = $message;
66 $mail->AltBody = $message;
67
68 $mail->send();
69
70 // copie en BDD
71 if(!$test_email && ($form_data->getData()['keep_emails'] ?? self::KEEP_EMAILS_DEFAULT)){
72 $db_email = new Email($name, $email, $email_dest, $message, $form_data);
73 $entityManager->persist($db_email);
74 self::updateLastContactDate($entityManager, $email);
75 $entityManager->flush();
76 }
77
78 return true;
79 }
80 catch(Exception $e){
81 echo "Le message n'a pas pu être envoyé. Erreur : {$e} <br> {$mail->ErrorInfo}";
82 return false;
83 }
84 }
85
86 static public function updateLastContactDate(EntityManager $entityManager, string $sender): void
87 {
88 foreach($entityManager->getRepository('App\Entity\Email')->findAll() as $email){
89 $email->getSenderAddress() === $sender ? $email->updateLastContactDate() : null;
90 }
91 }
92
93 // peut être appelée par bin/clean_emails_cron.php
94 static public function cleanEmails(EntityManager $entityManager): void
95 {
96 $emails = $entityManager->getRepository('App\Entity\Email')->findAll();
97 foreach($emails as $email){
98 if($email->getDeletionDate() < new \DateTime()){
99 $entityManager->remove($email);
100 }
101 }
102 $entityManager->flush();
103 }
104} \ No newline at end of file