diff options
Diffstat (limited to 'src/controller/ajax_email.php')
-rw-r--r-- | src/controller/ajax_email.php | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/src/controller/ajax_email.php b/src/controller/ajax_email.php deleted file mode 100644 index 1138e04..0000000 --- a/src/controller/ajax_email.php +++ /dev/null | |||
@@ -1,105 +0,0 @@ | |||
1 | <?php | ||
2 | // src/controller/ajax.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 | // mettre ça ailleurs? | ||
12 | function sendEmail(string $recipient, bool $true_email, string $name = '', string $email = '', string $message = ''): bool | ||
13 | { | ||
14 | $mail = new PHPMailer(true); // true => exceptions | ||
15 | $mail->CharSet = 'UTF-8'; | ||
16 | |||
17 | try{ | ||
18 | // Paramètres du serveur | ||
19 | $mail->isSMTP(); | ||
20 | $mail->Host = Config::$smtp_host; | ||
21 | $mail->SMTPAuth = true; | ||
22 | $mail->Port = 25; | ||
23 | |||
24 | if($mail->SMTPAuth){ | ||
25 | $mail->Username = Config::$smtp_username; // e-mail | ||
26 | $mail->Password = Config::$smtp_password; | ||
27 | $mail->SMTPSecure = Config::$smtp_secure; // tls (starttls) ou ssl (smtps) | ||
28 | if($mail->SMTPSecure === 'tls'){ | ||
29 | $mail->Port = 587; | ||
30 | } | ||
31 | elseif($mail->SMTPSecure === 'ssl'){ | ||
32 | $mail->Port = 465; | ||
33 | } | ||
34 | } | ||
35 | //var_dump($mail->smtpConnect());die; // test de connexion | ||
36 | |||
37 | // Expéditeur et destinataire | ||
38 | $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur | ||
39 | $mail->addAddress(strtolower($recipient), Config::$email_dest_name); // destinataire | ||
40 | |||
41 | // Contenu | ||
42 | $mail->isHTML(true); | ||
43 | if($true_email){ | ||
44 | $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; | ||
45 | |||
46 | } | ||
47 | else{ | ||
48 | $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; | ||
49 | } | ||
50 | $mail->Body = $message; | ||
51 | $mail->AltBody = $message; | ||
52 | |||
53 | $mail->send(); | ||
54 | return true; | ||
55 | } | ||
56 | catch(Exception $e){ | ||
57 | return false; | ||
58 | //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}"; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | function submitEmail(array $json, EntityManager $entityManager): void | ||
63 | { | ||
64 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; | ||
65 | $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0; | ||
66 | |||
67 | // contrôles des entrées | ||
68 | $name = htmlspecialchars(trim($json['name'])); | ||
69 | $email = strtolower(htmlspecialchars(trim($json['email']))); | ||
70 | $message = htmlspecialchars(trim($json['message'])); | ||
71 | |||
72 | // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur | ||
73 | $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); | ||
74 | $recipient = $form_data->getData()['email'] ?? Config::$email_dest; | ||
75 | |||
76 | if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution) | ||
77 | && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden']) | ||
78 | && sendEmail($recipient, true, $name, $email, $message)) | ||
79 | { | ||
80 | $db_email = new Email($email, Config::$email_dest, $message); | ||
81 | $entityManager->persist($db_email); | ||
82 | $entityManager->flush(); | ||
83 | echo json_encode(['success' => true]); | ||
84 | } | ||
85 | else{ | ||
86 | echo json_encode(['success' => false]); | ||
87 | } | ||
88 | die; | ||
89 | } | ||
90 | |||
91 | |||
92 | // détection des requêtes envoyées avec fetch (application/json) et récupération du JSON | ||
93 | if($_SERVER['CONTENT_TYPE'] === 'application/json') | ||
94 | { | ||
95 | $data = file_get_contents('php://input'); | ||
96 | $json = json_decode($data, true); | ||
97 | |||
98 | if(isset($_GET['action'])) | ||
99 | { | ||
100 | // formulaire de contact | ||
101 | if($_GET['action'] === 'send_email'){ | ||
102 | submitEmail($json, $entityManager); | ||
103 | } | ||
104 | } | ||
105 | } \ No newline at end of file | ||