mail.php

main 56 lines · 1.7 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php
2
3use PHPMailer\PHPMailer\PHPMailer;
4use PHPMailer\PHPMailer\Exception;
5
6class Mail {
7 protected array $_config;
8
9 public function __construct(array $config) {
10 $this->_config = $config;
11 }
12
13 public function sendMail(string $to, string $title, string $html, string $accname = ''): bool
14 {
15 try {
16 $mail = new PHPMailer(true);
17
18 // SMTP
19 $mail->isSMTP();
20 $mail->SMTPDebug = !empty($this->_config['debug']) ? 2 : 0;
21 $mail->Host = $this->_config['host'];
22 $mail->Port = (int)$this->_config['port'];
23 $mail->SMTPAuth = ($this->_config['username'] ?? '') !== '';
24 $mail->Username = $this->_config['username'];
25 $mail->Password = $this->_config['password'];
26 $mail->CharSet = 'UTF-8';
27
28 // Security
29 $secure = $this->_config['securityType'] ?? '';
30 if (in_array($secure, ['ssl', 'tls'], true)) {
31 $mail->SMTPSecure = $secure;
32 }
33
34 // Sender / receiver
35 $mail->setFrom($this->_config['email'], $this->_config['fromName']);
36 $mail->addAddress($to, $accname);
37
38 // Content
39 $mail->isHTML(true);
40 $mail->Subject = $title;
41 $mail->Body = $html;
42
43 // AltBody
44 $plain = str_replace(['<br>', '<br/>', '<br />'], "\n", $html);
45 $plain = strip_tags($plain);
46 $mail->AltBody = $plain;
47
48 return $mail->send();
49
50 } catch (Exception $e) {
51 // Log plutôt qu'echo
52 error_log('Mail error: ' . $e->getMessage());
53 return false;
54 }
55 }
56}
Top