website/SMTP_CONFIG_GUIDE.md

272 lines
7.5 KiB
Markdown
Raw Normal View History

2026-07-01 18:02:52 +02:00
# ⚡ Guide de Configuration SMTP pour send_mail.py (Auto-hébergement Linux + Apache)
*Dernière mise à jour : 2025-05-20*
*Fichier : `assets/python/send_mail.py`*
---
## 📋 **CHECKLIST RAPIDE**
- [ ] Postfix installé et démarré
- [ ] Apache configuré pour CGI Python
- [ ] send_mail.py a les permissions d'exécution
- [ ] Configuration SMTP correcte dans send_mail.py
- [ ] Test de connexion SMTP réussi
---
## 🎯 **ÉTAPES COMPLÈTES**
### **Partie 1 : Installation de Postfix (Serveur SMTP)**
```bash
# 1. Installer Postfix
sudo apt update && sudo apt install postfix mailutils -y
# 2. Pendant l'installation :
# - Sélectionner : "Site Internet"
# - Nom de domaine : omegakube.fr
# 3. Configurer Postfix
sudo nano /etc/postfix/main.cf
```
**Dans `/etc/postfix/main.cf`, modifier/modifier :**
```ini
myhostname = omegakube.fr
mydomain = omegakube.fr
myorigin = $mydomain
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
inet_interfaces = loopback-only
smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination
```
```bash
# 4. Redémarrer Postfix
sudo systemctl restart postfix
# 5. Vérifier que Postfix écoute sur le port 25
sudo netstat -tulnp | grep 25
# → Doit afficher : tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
# 6. Tester l'envoi local
sudo apt install mailutils -y # Si pas déjà installé
echo "Test email" | mail -s "Test Postfix" votre@email.com
```
---
### **Partie 2 : Configuration Apache pour CGI Python**
```bash
# 1. Activer le module CGI
sudo a2enmod cgi
# 2. Donner les permissions au script
chmod +x /home/gameurpro12/Documents/Mes_projets/site_Omega_Kube/website-main/assets/python/send_mail.py
# 3. Donner la propriété à www-data (utilisateur Apache)
sudo chown -R www-data:www-data /home/gameurpro12/Documents/Mes_projets/site_Omega_Kube/website-main/assets/python/
# 4. Configurer Apache pour permettre CGI dans le répertoire Python
sudo nano /etc/apache2/sites-available/omegakube.conf
```
**Ajouter dans la configuration Apache :**
```apache
<Directory "/home/gameurpro12/Documents/Mes_projets/site_Omega_Kube/website-main/assets/python">
Options +ExecCGI
AddHandler cgi-script .py
Require all granted
</Directory>
```
```bash
# 5. Redémarrer Apache
sudo systemctl restart apache2
# 6. Vérifier que CGI est activé
sudo apache2ctl -M | grep cgi
# → Doit afficher : cgi_module (shared)
```
---
### **Partie 3 : Configurer send_mail.py**
**Éditer le fichier :**
```bash
nano /home/gameurpro12/Documents/Mes_projets/site_Omega_Kube/website-main/assets/python/send_mail.py
```
**Configurer ces lignes (pour Postfix local) :**
```python
TO_EMAIL = "contact@omegakube.fr"
FROM_EMAIL = "noreply@omegakube.fr"
SMTP_HOST = "localhost" # Ou "127.0.0.1"
SMTP_PORT = 25
SMTP_SSL = False # Pas de SSL pour localhost
SMTP_USER = "" # Pas d'authentification
SMTP_PASSWORD = "" # Pas d'authentification
```
**Pour d'autres fournisseurs (Gmail, OVH, etc.) :**
```python
# Exemple pour Gmail
SMTP_HOST = "smtp.gmail.com"
SMTP_PORT = 587
SMTP_SSL = False
SMTP_USER = "votre@gmail.com"
SMTP_PASSWORD = "votre_app_password" # Utilisez un "App Password" si 2FA
```
---
### **Partie 4 : Tests**
#### **Test 1 : Vérifier Postfix**
```bash
# Vérifier le statut
sudo systemctl status postfix
# Vérifier les logs SMTP
tail -f /var/log/mail.log
```
#### **Test 2 : Tester send_mail.py manuellement**
```bash
# Créer un fichier de test
cd /home/gameurpro12/Documents/Mes_projets/site_Omega_Kube/website-main/assets/python
echo '{"name": "Test", "email": "votre@email.com", "subject": "Test SMTP", "message": "Ceci est un test"}' > /tmp/test_data.json
# Exécuter avec simulation de requête POST
REQUEST_METHOD=POST CONTENT_LENGTH=85 HTTP_ORIGIN="https://omegakube.fr" python3 send_mail.py < /tmp/test_data.json
```
**Résultat attendu :**
```json
{"success": true, "message": "Votre message a bien été envoyé ! Nous vous répondrons rapidement."}
```
#### **Test 3 : Tester via le formulaire web**
1. Ouvrir le site dans un navigateur
2. Remplir le formulaire de contact
3. Soumettre
4. **Vérifier les logs :**
```bash
tail -f /var/log/mail.log
tail -f /var/log/apache2/error.log
```
---
## 🔍 **DÉPANNAGE**
### **Problème : "Connection refused" sur localhost:25**
```bash
# Solution 1 : Vérifier que Postfix écoute
sudo netstat -tulnp | grep 25
# Solution 2 : Redémarrer Postfix
sudo systemctl restart postfix
# Solution 3 : Vérifier la configuration
sudo postconf -n mynetworks
# → Doit contenir : 127.0.0.0/8
```
### **Problème : "Relay access denied"**
```bash
# Modifier la configuration Postfix
sudo nano /etc/postfix/main.cf
```
```ini
# Remplacer par :
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
```
```bash
sudo systemctl restart postfix
```
### **Problème : send_mail.py retourne 500 Error**
```bash
# Vérifier les permissions
ls -l /home/gameurpro12/Documents/Mes_projets/site_Omega_Kube/website-main/assets/python/send_mail.py
# → Doit afficher : -rwxr-xr-x
# Vérifier que CGI est activé
sudo apache2ctl -M | grep cgi
# Vérifier le shebang dans send_mail.py
head -1 /home/gameurpro12/Documents/Mes_projets/site_Omega_Kube/website-main/assets/python/send_mail.py
# → Doit être : #!/usr/bin/env python3
```
### **Problème : Emails marqués comme spam**
```bash
# Configurer SPF (DNS)
# Ajouter cet enregistrement TXT pour omegakube.fr :
# v=spf1 a mx ~all
# Vérifier votre IP sur les blacklists
# https://mxtoolbox.com/blacklists.aspx
```
---
## 📚 **CONFIGURATIONS SMTP PAR FOURNISSEUR**
| Fournisseur | SMTP_HOST | SMTP_PORT | SMTP_SSL | Authentification | User | Password |
|------------|-----------|-----------|----------|-----------------|------|----------|
| **Local (Postfix)** | localhost | 25 | False | Non | - | - |
| **Gmail** | smtp.gmail.com | 587 | False | Oui | email@gmail.com | App Password |
| **Gmail SSL** | smtp.gmail.com | 465 | True | Oui | email@gmail.com | App Password |
| **OVH** | ssl0.ovh.net | 587 | False | Oui | contact@omegakube.fr | mot de passe |
| **Mailjet** | in-v3.mailjet.com | 587 | False | Oui | API Key | API Secret |
| **SendGrid** | smtp.sendgrid.net | 587 | False | Oui | apikey | API Key |
---
## 🎯 **RÉSUMÉ DES COMMANDES CLÉS**
```bash
# Installer Postfix
sudo apt install postfix mailutils -y
# Activer CGI dans Apache
sudo a2enmod cgi && sudo systemctl restart apache2
# Permissions
chmod +x assets/python/send_mail.py
sudo chown -R www-data:www-data assets/python/
# Redémarrer les services
sudo systemctl restart postfix apache2
# Vérifier
sudo systemctl status postfix apache2
sudo netstat -tulnp | grep 25
sudo apache2ctl -M | grep cgi
# Tester
REQUEST_METHOD=POST CONTENT_LENGTH=85 HTTP_ORIGIN="https://omegakube.fr" python3 assets/python/send_mail.py < /tmp/test_data.json
# Voir les logs
tail -f /var/log/mail.log
```
---
## 📌 **NOTES IMPORTANTES**
1. **Pour Gmail** : Activez "App Password" si vous avez la 2FA : [https://myaccount.google.com/apppasswords](https://myaccount.google.com/apppasswords)
2. **Sécurité** : Ne jamais utiliser votre mot de passe Gmail normal, toujours un "App Password"
3. **SPF/DKIM** : Configurez ces enregistrements DNS pour éviter que vos emails aient comme spam
4. **Backups** : Sauvegardez `/etc/postfix/main.cf` avant toute modification
---
**✅ Une fois tout configuré, votre formulaire de contact devrait fonctionner parfaitement !**
*Besoin d'aide ? Vérifiez les logs avec `tail -f /var/log/mail.log` et `tail -f /var/log/apache2/error.log`*