3use PHPMailer\PHPMailer\PHPMailer;
4use PHPMailer\PHPMailer\SMTP;
5use PHPMailer\PHPMailer\Exception;
14 private \Twig\Environment
$twig;
32 $configPath = __DIR__ .
'/../config/config.json';
33 if (!file_exists($configPath)) {
34 throw new Exception(
'Fichier de configuration introuvable.');
37 $config = json_decode(file_get_contents($configPath),
true);
38 if (!isset($config[
'mail'])) {
39 throw new Exception(
'Configuration email manquante dans config.json');
42 $this->mailConfig = $config[
'mail'];
51 $mail =
new PHPMailer(
true);
55 $mail->Host = $this->mailConfig[
'host'];
56 $mail->SMTPAuth =
true;
57 $mail->Username = $this->mailConfig[
'username'];
58 $mail->Password = $this->mailConfig[
'password'];
59 $mail->Port = $this->mailConfig[
'port'];
62 $encryption = $this->mailConfig[
'encryption'] ??
'tls';
63 if ($encryption ===
'tls') {
64 $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
65 } elseif ($encryption ===
'ssl') {
66 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
71 $this->mailConfig[
'from_email'],
72 $this->mailConfig[
'from_name']
76 $mail->CharSet =
'UTF-8';
92 public function sendEmail(
string $to,
string $toName,
string $subject,
string $htmlBody, ?
string $textBody =
null): bool
96 $mail->addAddress($to, $toName);
97 $mail->Subject = $subject;
98 $mail->Body = $htmlBody;
99 $mail->AltBody = $textBody ?? strip_tags($htmlBody);
101 return $mail->send();
102 }
catch (Exception $e) {
103 error_log(
"Erreur d'envoi d'email: " . $e->getMessage());
104 throw new Exception(
"Impossible d'envoyer l'email: " . $e->getMessage());
117 $subject =
"Bienvenue sur Paaxio, $pseudo !";
119 $htmlBody = $this->twig->render(
'emails/welcome.html.twig', [
126 $textBody =
"Bienvenue sur Paaxio, $pseudo !\n\n";
127 $textBody .=
"Votre compte $type a été créé avec succès.\n";
128 $textBody .=
"Vous pouvez dès maintenant vous connecter et profiter de toutes les fonctionnalités de Paaxio.\n\n";
129 $textBody .=
"À bientôt sur Paaxio !";
131 return $this->
sendEmail($email, $pseudo, $subject, $htmlBody, $textBody);
143 $subject =
"Confirmez votre inscription sur Paaxio";
145 $confirmationUrl = $this->
getSiteUrl() .
"/?controller=utilisateur&method=confirmer&token=" . urlencode($confirmationToken);
147 $htmlBody = $this->twig->render(
'emails/confirmation.html.twig', [
149 'confirmation_url' => $confirmationUrl,
153 $textBody =
"Bonjour $pseudo,\n\n";
154 $textBody .=
"Merci de vous être inscrit sur Paaxio !\n\n";
155 $textBody .=
"Pour confirmer votre inscription, veuillez cliquer sur le lien suivant :\n";
156 $textBody .=
"$confirmationUrl\n\n";
157 $textBody .=
"Ce lien est valable 24 heures.\n\n";
158 $textBody .=
"Si vous n'avez pas créé de compte sur Paaxio, ignorez cet email.";
160 return $this->
sendEmail($email, $pseudo, $subject, $htmlBody, $textBody);
172 $subject =
"Réinitialisation de votre mot de passe Paaxio";
174 $resetUrl = $this->
getSiteUrl() .
"/?controller=utilisateur&method=resetPassword&token=" . urlencode($resetToken);
176 $htmlBody = $this->twig->render(
'emails/password_reset.html.twig', [
178 'reset_url' => $resetUrl,
182 $textBody =
"Bonjour $pseudo,\n\n";
183 $textBody .=
"Vous avez demandé la réinitialisation de votre mot de passe sur Paaxio.\n\n";
184 $textBody .=
"Pour réinitialiser votre mot de passe, cliquez sur le lien suivant :\n";
185 $textBody .=
"$resetUrl\n\n";
186 $textBody .=
"Ce lien est valable 1 heure.\n\n";
187 $textBody .=
"Si vous n'avez pas demandé cette réinitialisation, ignorez cet email.";
189 return $this->
sendEmail($email, $pseudo, $subject, $htmlBody, $textBody);
200 public function sendNewsletterEmail(
string $email,
string $subject,
string $content, ?
string $unsubscribeToken =
null): bool
202 $unsubscribeUrl = $unsubscribeToken
203 ? $this->
getSiteUrl() .
"/?controller=newsletter&method=desinscrire&token=" . urlencode($unsubscribeToken)
206 $htmlBody = $this->twig->render(
'emails/newsletter.html.twig', [
207 'content' => $content,
208 'unsubscribe_url' => $unsubscribeUrl,
212 $textBody = strip_tags($content);
213 if ($unsubscribeUrl) {
214 $textBody .=
"\n\n---\nPour vous désinscrire : $unsubscribeUrl";
217 return $this->
sendEmail($email,
'', $subject, $htmlBody, $textBody);
231 'new_album' =>
'Nouvel album disponible !',
232 'new_follower' =>
'Vous avez un nouveau follower !',
233 'new_like' =>
'Quelqu\'un a aimé votre musique !',
234 'battle_invite' =>
'Invitation à une battle musicale !',
235 'battle_result' =>
'Résultats de la battle musicale'
238 $subject = $subjects[$type] ??
'Notification Paaxio';
240 $htmlBody = $this->twig->render(
"emails/notification_$type.html.twig", array_merge([
245 return $this->
sendEmail($email, $pseudo, $subject, $htmlBody);
256 public function sendContactEmail(
string $fromEmail,
string $fromName,
string $subject,
string $message): bool
258 $contactEmail = $this->mailConfig[
'from_email'];
260 $htmlBody = $this->twig->render(
'emails/contact.html.twig', [
261 'from_email' => $fromEmail,
262 'from_name' => $fromName,
263 'subject' => $subject,
264 'message' => $message,
268 $textBody =
"Message de contact reçu\n\n";
269 $textBody .=
"De: $fromName <$fromEmail>\n";
270 $textBody .=
"Sujet: $subject\n\n";
271 $textBody .=
"Message:\n$message";
275 $mail->addAddress($contactEmail,
'Paaxio Contact');
276 $mail->addReplyTo($fromEmail, $fromName);
277 $mail->Subject =
"[Contact] $subject";
278 $mail->Body = $htmlBody;
279 $mail->AltBody = $textBody;
281 return $mail->send();
282 }
catch (Exception $e) {
283 error_log(
"Erreur d'envoi d'email de contact: " . $e->getMessage());
284 throw new Exception(
"Impossible d'envoyer le message de contact: " . $e->getMessage());
295 public function sendBulkEmail(array $recipients,
string $subject,
string $content): array
303 foreach ($recipients as $recipient) {
305 $email = is_array($recipient) ? $recipient[
'email'] : $recipient;
306 $token = is_array($recipient) ? ($recipient[
'token'] ??
null) :
null;
309 $results[
'success']++;
311 $results[
'failed']++;
312 $results[
'errors'][] =
"Échec de l'envoi à $email";
317 }
catch (Exception $e) {
318 $results[
'failed']++;
319 $results[
'errors'][] =
"Erreur pour $email: " . $e->getMessage();
332 $protocol = isset($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] ===
'on' ?
'https' :
'http';
333 $host = $_SERVER[
'HTTP_HOST'] ??
'localhost';
334 return "$protocol://$host";
344 return bin2hex(random_bytes($length));
355 $mail->SMTPDebug = SMTP::DEBUG_CONNECTION;
358 $mail->smtpConnect();
360 $debug = ob_get_clean();
364 'message' =>
'Connexion SMTP réussie',
367 }
catch (Exception $e) {
370 'message' =>
'Échec de la connexion SMTP: ' . $e->getMessage()
382 $fakeName =
"Angel Ramirez";
383 $fakeEmail =
"contact@angelbatalla.com";
384 $fakeType =
"auditeur";
391 'message' =>
"L'email de bienvenue a bien été envoyé à $fakeEmail."
396 'message' =>
"L'envoi de l'email de bienvenue a échoué pour $fakeEmail."
399 }
catch (Exception $e) {
402 'message' =>
"Erreur lors de l'envoi de l'email : " . $e->getMessage()
Classe de gestion des emails pour Paaxio Utilise PHPMailer pour l'envoi d'emails via SMTP.
sendContactEmail(string $fromEmail, string $fromName, string $subject, string $message)
Envoie un email de contact (depuis le formulaire de contact)
sendNotificationEmail(string $email, string $pseudo, string $type, array $data=[])
Envoie un email de notification (nouvel album, nouveau follower, etc.)
sendNewsletterEmail(string $email, string $subject, string $content, ?string $unsubscribeToken=null)
Envoie un email de newsletter.
sendWelcomeEmail(string $email, string $pseudo, string $type)
Envoie un email de bienvenue à un nouvel utilisateur.
sendPasswordResetEmail(string $email, string $pseudo, string $resetToken)
Envoie un email de réinitialisation de mot de passe.
__construct(\Twig\Environment $twig)
Constructeur de la classe Email.
testEmail()
Fonction de test simple de la classe Email avec un nom et un email factices.
sendConfirmationEmail(string $email, string $pseudo, string $confirmationToken)
Envoie un email de confirmation d'inscription.
sendEmail(string $to, string $toName, string $subject, string $htmlBody, ?string $textBody=null)
Envoie un email générique.
loadMailConfig()
Charge la configuration email depuis le fichier config.json.
sendBulkEmail(array $recipients, string $subject, string $content)
Envoie un email en masse à plusieurs destinataires (pour la newsletter)
createMailer()
Crée et configure une instance PHPMailer.
static generateToken(int $length=32)
Génère un token sécurisé
testSmtpConnection()
Teste la configuration SMTP.
getSiteUrl()
Récupère l'URL du site.