🚑️ Ajout du fonctionnement du formulaire de contact en production, utilisation de PHPMailer.

This commit is contained in:
2026-01-24 03:48:37 +01:00
parent 9180f116ec
commit db285e2006
8 changed files with 306 additions and 24 deletions

View File

@@ -3,6 +3,11 @@
* Fonctions helpers du portfolio
*/
require_once __DIR__ . '/../vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception as MailerException;
/**
* Inclut un template avec des données
* @param string $name Nom du template (sans .php)
@@ -410,6 +415,127 @@ function validateContactData(array $input): array
* @return bool True si envoyé avec succès
*/
function sendContactEmail(array $data): bool
{
// Vérifier config minimale
if (!defined('CONTACT_EMAIL') || empty(CONTACT_EMAIL)) {
error_log('CONTACT_EMAIL non configuré');
return false;
}
if (!defined('SMTP_HOST') || !defined('SMTP_PORT')) {
error_log('SMTP_HOST/SMTP_PORT non configurés');
return false;
}
// Autoload PHPMailer (Composer)
$autoload = __DIR__ . '/../vendor/autoload.php';
if (!file_exists($autoload)) {
error_log('PHPMailer autoload introuvable. As-tu installé Composer ?');
return false;
}
require_once $autoload;
$categorieLabels = [
'projet' => 'Projet freelance',
'poste' => 'Proposition de poste',
'autre' => 'Autre demande'
];
$categorie = $categorieLabels[$data['categorie']] ?? 'Autre';
$entreprise = $data['entreprise'] ?: 'Non renseignée';
$subject = "[Portfolio] {$categorie} - {$data['objet']}";
$body = "═══════════════════════════════════════════\n";
$body .= "NOUVEAU MESSAGE - PORTFOLIO\n";
$body .= "═══════════════════════════════════════════\n\n";
$body .= "DE: {$data['prenom']} {$data['nom']}\n";
$body .= "EMAIL: {$data['email']}\n";
$body .= "ENTREPRISE: {$entreprise}\n";
$body .= "CATÉGORIE: {$categorie}\n\n";
$body .= "───────────────────────────────────────────\n";
$body .= "OBJET: {$data['objet']}\n";
$body .= "───────────────────────────────────────────\n\n";
$body .= "MESSAGE:\n\n";
$body .= "{$data['message']}\n\n";
$body .= "═══════════════════════════════════════════\n";
$body .= "Envoyé le {$data['date']}\n";
$body .= "IP: {$data['ip']}\n";
$body .= "═══════════════════════════════════════════";
$mail = new PHPMailer(true);
try {
// Mode SMTP
$mail->isSMTP();
$mail->Host = SMTP_HOST;
$mail->Port = SMTP_PORT;
// Auth SMTP si configurée
$hasAuth = !empty(SMTP_USERNAME) && !empty(SMTP_PASSWORD);
$mail->SMTPAuth = $hasAuth;
if ($hasAuth) {
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
}
// Encryption
$enc = strtolower((string) SMTP_ENCRYPTION);
if ($enc === 'tls') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
} elseif ($enc === 'ssl') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
} else {
// none
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
}
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
// Expéditeur (doit être un alias autorisé si Proton)
$fromAddress = defined('MAIL_FROM_ADDRESS') ? MAIL_FROM_ADDRESS : SMTP_USERNAME;
$fromName = defined('MAIL_FROM_NAME') ? MAIL_FROM_NAME : 'Portfolio - Contact';
if (empty($fromAddress)) {
error_log('MAIL_FROM_ADDRESS/SMTP_USERNAME vide : impossible de définir From');
return false;
}
$mail->setFrom($fromAddress, $fromName);
// Reply-To = email du visiteur
if (!empty($data['email'])) {
$mail->addReplyTo($data['email'], trim(($data['prenom'] ?? '') . ' ' . ($data['nom'] ?? '')));
}
// Destinataire
$mail->addAddress(CONTACT_EMAIL);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->isHTML(false);
$mail->SMTPDebug = 2;
$mail->Debugoutput = function ($str, $level) {
error_log("PHPMailer debug($level): $str");
};
// Timeouts/log (utile en debug)
$mail->Timeout = 10;
$mail->send();
return true;
} catch (\Throwable $e) {
error_log('Mail send unexpected error: ' . $e->getMessage());
return false;
}
}
/*function sendContactEmail(array $data): bool
{
$categorieLabels = [
'projet' => 'Projet freelance',
@@ -460,4 +586,4 @@ function sendContactEmail(array $data): bool
}
return $result;
}
}*/