eadl:bloc3:dev_av:td4

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
eadl:bloc3:dev_av:td4 [2025/11/09 23:20] – [7.4 Test] jcheroneadl:bloc3:dev_av:td4 [2025/11/10 16:28] (Version actuelle) – [Concepts clés à retenir] jcheron
Ligne 1306: Ligne 1306:
 <html></div></html> <html></div></html>
  
-===== Partie 7 : Visualiser les emails avec MailHog (Bonus : 10min) =====+===== Partie 7 : Visualiser les emails avec MailPit (Bonus : 10min) =====
  
 <WRAP round bloc info> <WRAP round bloc info>
-**MailHog** = Serveur SMTP factice avec interface web +**Mailpit** = Serveur SMTP de test avec interface web moderne 
-  * Pas de configuration SMTP réelle +  * Capture tous les emails envoyés par l'application 
-  * Interface web pour voir tous les emails +  * Interface web pour visualiser les emails 
-  * Pas de risque d'envoyer de vrais emails+  * Aucune configuration SMTP complexe 
 +  * Parfait pour le développement
 </WRAP> </WRAP>
  
-==== 7.1 Ajouter MailHog au docker-compose.yml ====+==== 7.1 Ajouter Mailpit au docker-compose.yml ==== 
 + 
 +Ajouter ce service dans votre fichier **''docker-compose.yml''** :
  
 <sxh yaml> <sxh yaml>
-  mailhog+  mailpit
-    image: mailhog/mailhog:latest +    image: axllent/mailpit:latest 
-    container_name: ecommerce-mailhog+    container_name: ecommerce-mailpit
     ports:     ports:
       - "1025:1025"  # SMTP       - "1025:1025"  # SMTP
Ligne 1326: Ligne 1329:
     networks:     networks:
       - ecommerce-network       - ecommerce-network
 +</sxh>
 +
 +<sxh bash>
 +# Démarrer Mailpit
 +docker-compose up -d mailpit
 +
 +# Vérifier que Mailpit est démarré
 +docker ps | grep mailpit
 </sxh> </sxh>
  
 ==== 7.2 Configuration Spring ==== ==== 7.2 Configuration Spring ====
  
-Modifier **''src/main/resources/application-dev.properties''** :+Modifier le fichier **''src/main/resources/application-dev.properties''** :
  
 <sxh properties> <sxh properties>
-MailHog+Mailpit configuration
 spring.mail.host=localhost spring.mail.host=localhost
 spring.mail.port=1025 spring.mail.port=1025
Ligne 1341: Ligne 1352:
 spring.mail.properties.mail.smtp.starttls.enable=false spring.mail.properties.mail.smtp.starttls.enable=false
  
 +# Notification settings
 notification.email.enabled=true notification.email.enabled=true
 notification.email.from=noreply@ecommerce-demo.com notification.email.from=noreply@ecommerce-demo.com
Ligne 1346: Ligne 1358:
 # Logs pour voir les envois # Logs pour voir les envois
 logging.level.org.springframework.mail=DEBUG logging.level.org.springframework.mail=DEBUG
 +</sxh>
 +
 +Modifier également **''src/test/resources/application-test.properties''** :
 +
 +<sxh properties>
 +# Mailpit pour les tests
 +spring.mail.host=localhost
 +spring.mail.port=1025
 +
 +notification.email.enabled=true
 +notification.email.from=test@ecommerce-demo.com
 </sxh> </sxh>
  
 ==== 7.3 Améliorer les logs dans EmailNotificationSender ==== ==== 7.3 Améliorer les logs dans EmailNotificationSender ====
 +
 +Modifier la méthode **''send()''** dans **''EmailNotificationSender.kt''** :
  
 <sxh kotlin> <sxh kotlin>
Ligne 1363: Ligne 1388:
         mailSender.send(message)         mailSender.send(message)
                  
-        logger.info("✅ Email sent to: $recipient"+        logger.info("✅ Email sent to: $recipient - Subject: $subject") 
-        logger.debug("🌐 Check MailHog UI: http://localhost:8025")+        logger.info("🌐 View in Mailpit: http://localhost:8025")
                  
     } catch (e: Exception) {     } catch (e: Exception) {
-        logger.error("❌ Failed to send email", e)+        logger.error("❌ Failed to send email to $recipient", e)
         throw RuntimeException("Email sending failed", e)         throw RuntimeException("Email sending failed", e)
     }     }
Ligne 1373: Ligne 1398:
 </sxh> </sxh>
  
-==== 7.4 Test ====+==== 7.4 Test manuel ====
  
 <sxh bash> <sxh bash>
-# 1. Démarrer MailHog +# 1. Démarrer Mailpit (si pas déjà fait) 
-docker-compose up -d mailhog+docker-compose up -d mailpit
  
 # 2. Lancer l'application avec le profil dev # 2. Lancer l'application avec le profil dev
 mvn spring-boot:run -P dev mvn spring-boot:run -P dev
  
-# 3. Créer une commande+# 3. Créer une commande pour déclencher l'envoi d'email
 curl -X POST http://localhost:8080/api/orders \ curl -X POST http://localhost:8080/api/orders \
   -H "Content-Type: application/json" \   -H "Content-Type: application/json" \
Ligne 1388: Ligne 1413:
     "customerId": 1,     "customerId": 1,
     "customerEmail": "test@example.com",     "customerEmail": "test@example.com",
-    "items": [{"productId": 1, "quantity": 2, "price": 29.99}]+    "items": [ 
 +      { 
 +        "productId": 1, 
 +        "quantity": 2, 
 +        "price": 29.99 
 +      } 
 +    ]
   }'   }'
  
-4. Ouvrir MailHog+7.Ouvrir l'interface Mailpit
 open http://localhost:8025 open http://localhost:8025
 +# Ou dans votre navigateur : http://localhost:8025
 </sxh> </sxh>
  
 <WRAP round bloc todo> <WRAP round bloc todo>
-**Résultat attendu :** +**Vérifications à effectuer :** 
-  * Email visible dans MailHog en < secondes +  1. Ouvrir http://localhost:8025 dans votre navigateur 
-  * Sujet : "Order Confirmation #XXX" +  2. Vérifier qu'un email apparaît dans la liste 
-  * Contenu : Détails de la commande en HTML+  3. Cliquer sur l'email pour voir son contenu 
 +  4. Vérifier que le contenu HTML est correct 
 +  5. Vérifier le sujet : "Order Confirmation #XXX" 
 +  6. Vérifier les détails de la commande dans l'email
 </WRAP> </WRAP>
 +
 +==== 7.5 Interface Mailpit ====
 +
 +L'interface web de Mailpit (http://localhost:8025) permet de :
 +  * Voir tous les emails envoyés
 +  * Rechercher dans les emails
 +  * Prévisualiser le HTML et le texte brut
 +  * Voir les pièces jointes
 +  * Supprimer les emails
 +  * Tester le responsive design des emails
 +
 +==== 7.6 Configuration pour la production ====
  
 <WRAP round bloc important> <WRAP round bloc important>
-**Production vs Développement :**+** Mailpit est uniquement pour le développement !**
  
-En **développement** (MailHog) : +En **développement** (Mailpit) : 
-<code properties>+<sxh properties>
 spring.mail.host=localhost spring.mail.host=localhost
 spring.mail.port=1025 spring.mail.port=1025
-</code>+notification.email.enabled=true 
 +</sxh>
  
-En **production** (SMTP réel) : +En **production** (SMTP réel - exemple avec Gmail) : 
-<code properties>+<sxh properties>
 spring.mail.host=smtp.gmail.com spring.mail.host=smtp.gmail.com
 spring.mail.port=587 spring.mail.port=587
Ligne 1419: Ligne 1467:
 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.auth=true
 spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.enable=true
-</code> 
  
-⚠️ **Ne jamais commiter les credentials SMTP !**+notification.email.enabled=true 
 +notification.email.from=${EMAIL_FROM} 
 +</sxh> 
 + 
 +** Bonnes pratiques :** 
 +  * Ne **jamais** commiter les credentials SMTP dans le code 
 +  * Utiliser des **variables d'environnement** 
 +  * Activer **TLS/SSL** en production 
 +  * Utiliser des **app passwords** (Gmail, Outlook, etc.)
 </WRAP> </WRAP>
 +
 +==== 7.7 Commandes utiles ====
 +
 +<sxh bash>
 +# Démarrer uniquement Mailpit
 +docker-compose up -d mailpit
 +
 +# Voir les logs de Mailpit
 +docker logs -f ecommerce-mailpit
 +
 +# Redémarrer Mailpit
 +docker-compose restart mailpit
 +
 +# Arrêter Mailpit
 +docker-compose stop mailpit
 +
 +# Supprimer le conteneur Mailpit
 +docker-compose down mailpit
 +</sxh>
 +
 +==== 7.8 Dépannage ====
 +
 +**Problème : Les emails n'apparaissent pas dans Mailpit**
 +
 +<sxh bash>
 +# 1. Vérifier que Mailpit est démarré
 +docker ps | grep mailpit
 +
 +# 2. Vérifier les logs de l'application
 +# Rechercher : "Email sent to:" ou "Failed to send email"
 +
 +# 3. Vérifier que le profil dev est actif
 +# Dans les logs au démarrage : "The following profiles are active: dev"
 +
 +# 4. Tester la connexion SMTP
 +telnet localhost 1025
 +</sxh>
 +
 +**Problème : "Connection refused" sur le port 1025**
 +
 +<code>
 +# Vérifier que le port 1025 n'est pas déjà utilisé
 +lsof -i :1025
 +
 +# Si occupé, changer le port dans docker-compose.yml et application-dev.properties
 +</code>
 +
 +
 +
 +
 +
  
 ===== Livrables attendus ===== ===== Livrables attendus =====
Ligne 1458: Ligne 1564:
  
   * Ajout du canal SMS   * Ajout du canal SMS
-  * Template d'email avec Thymeleaf+  * Template d'email avec Thymeleaf/mustache
   * Retry automatique en cas d'échec   * Retry automatique en cas d'échec
   * Dashboard des notifications dans H2 console   * Dashboard des notifications dans H2 console
Ligne 1471: Ligne 1577:
   * **Observer** : Spring Events pour la communication inter-domaines   * **Observer** : Spring Events pour la communication inter-domaines
   * **Strategy** : ''NotificationSender'' avec différentes implémentations   * **Strategy** : ''NotificationSender'' avec différentes implémentations
-  * **Factory** : Injection automatique de tous les senders+  * **Factory** : Injection automatique de tous les senders (pas le pattern Factory en lui-même)
   * **Dependency Inversion** : ''OrderService'' ne dépend que d'abstractions   * **Dependency Inversion** : ''OrderService'' ne dépend que d'abstractions
  
  • eadl/bloc3/dev_av/td4.1762726810.txt.gz
  • Dernière modification : il y a 3 mois
  • de jcheron