diff options
author | polo <ordipolo@gmx.fr> | 2025-08-03 00:23:11 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2025-08-03 00:23:11 +0200 |
commit | 547d7feed68e89957f062b8ed9b988f28c5830ce (patch) | |
tree | 7e07ea2b2065468900201cddacad5db559446a7d /src/controller/EmailController.php | |
parent | 9934a32f7e02c484d6b122c9af860ab1ca9b2dca (diff) | |
download | cms-547d7feed68e89957f062b8ed9b988f28c5830ce.zip |
réorganisation 3: classes controller
Diffstat (limited to 'src/controller/EmailController.php')
-rw-r--r-- | src/controller/EmailController.php | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/controller/EmailController.php b/src/controller/EmailController.php new file mode 100644 index 0000000..1eea257 --- /dev/null +++ b/src/controller/EmailController.php | |||
@@ -0,0 +1,91 @@ | |||
1 | <?php | ||
2 | // src/controller/EmailController.php | ||
3 | |||
4 | declare(strict_types=1); | ||
5 | |||
6 | use PHPMailer\PHPMailer\PHPMailer; | ||
7 | use PHPMailer\PHPMailer\Exception; | ||
8 | use App\Entity\Email; | ||
9 | use Doctrine\ORM\EntityManager; | ||
10 | |||
11 | class EmailController | ||
12 | { | ||
13 | static public function send(string $recipient, bool $true_email, string $name = '', string $email = '', string $message = ''): bool | ||
14 | { | ||
15 | $mail = new PHPMailer(true); // true => exceptions | ||
16 | $mail->CharSet = 'UTF-8'; | ||
17 | |||
18 | try{ | ||
19 | // Paramètres du serveur | ||
20 | $mail->isSMTP(); | ||
21 | $mail->Host = Config::$smtp_host; | ||
22 | $mail->SMTPAuth = true; | ||
23 | $mail->Port = 25; | ||
24 | |||
25 | if($mail->SMTPAuth){ | ||
26 | $mail->Username = Config::$smtp_username; // e-mail | ||
27 | $mail->Password = Config::$smtp_password; | ||
28 | $mail->SMTPSecure = Config::$smtp_secure; // tls (starttls) ou ssl (smtps) | ||
29 | if($mail->SMTPSecure === 'tls'){ | ||
30 | $mail->Port = 587; | ||
31 | } | ||
32 | elseif($mail->SMTPSecure === 'ssl'){ | ||
33 | $mail->Port = 465; | ||
34 | } | ||
35 | } | ||
36 | //var_dump($mail->smtpConnect());die; // test de connexion | ||
37 | |||
38 | // Expéditeur et destinataire | ||
39 | $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur | ||
40 | $mail->addAddress(strtolower($recipient), Config::$email_dest_name); // destinataire | ||
41 | |||
42 | // Contenu | ||
43 | $mail->isHTML(true); | ||
44 | if($true_email){ | ||
45 | $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; | ||
46 | |||
47 | } | ||
48 | else{ | ||
49 | $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; | ||
50 | } | ||
51 | $mail->Body = $message; | ||
52 | $mail->AltBody = $message; | ||
53 | |||
54 | $mail->send(); | ||
55 | return true; | ||
56 | } | ||
57 | catch(Exception $e){ | ||
58 | return false; | ||
59 | //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}"; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | static public function submit(array $json, EntityManager $entityManager): void | ||
64 | { | ||
65 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; | ||
66 | $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0; | ||
67 | |||
68 | // contrôles des entrées | ||
69 | $name = htmlspecialchars(trim($json['name'])); | ||
70 | $email = strtolower(htmlspecialchars(trim($json['email']))); | ||
71 | $message = htmlspecialchars(trim($json['message'])); | ||
72 | |||
73 | // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur | ||
74 | $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); | ||
75 | $recipient = $form_data->getData()['email'] ?? Config::$email_dest; | ||
76 | |||
77 | if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution) | ||
78 | && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden']) | ||
79 | && self::send($recipient, true, $name, $email, $message)) | ||
80 | { | ||
81 | $db_email = new Email($email, Config::$email_dest, $message); | ||
82 | $entityManager->persist($db_email); | ||
83 | $entityManager->flush(); | ||
84 | echo json_encode(['success' => true]); | ||
85 | } | ||
86 | else{ | ||
87 | echo json_encode(['success' => false]); | ||
88 | } | ||
89 | die; | ||
90 | } | ||
91 | } \ No newline at end of file | ||