diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index f819a3e8..d3cf4974 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -68,8 +68,18 @@ jobs: AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} S3_URL_ENDPOINT: https://s3.gra.io.cloud.ovh.net/ S3_BUCKET_NAME: basegun-s3 + EMAIL_HOST: mailpit + EMAIL_PORT: 1025 steps: - run: cd /app && pytest + services: + mailpit: + image: axllent/mailpit + ports: + - 1025:1025 + env: + MP_SMTP_AUTH_ACCEPT_ANY: 1 + MP_SMTP_AUTH_ALLOW_INSECURE: 1 test-frontend-format: name: Test Frontend Formatting diff --git a/backend/src/config.py b/backend/src/config.py index 2edcc82d..41b86c2b 100644 --- a/backend/src/config.py +++ b/backend/src/config.py @@ -1,5 +1,6 @@ import os from datetime import datetime +from smtplib import SMTP import boto3 from gelfformatter import GelfFormatter @@ -119,4 +120,7 @@ def get_base_logs(user_agent, user_id: str) -> dict: "epaule_a_un_coup_par_canon", "epaule_a_verrou", "epaule_semi_auto_style_chasse", -] \ No newline at end of file +] + +# Emails +SMTPClient = SMTP(os.environ["EMAIL_HOST"], os.environ["EMAIL_PORT"]) diff --git a/backend/src/utils.py b/backend/src/utils.py index 8ac2eaa5..f69f5802 100644 --- a/backend/src/utils.py +++ b/backend/src/utils.py @@ -1,6 +1,9 @@ import logging import time from datetime import datetime +from email.message import EmailMessage + +from src.config import SMTPClient from .config import S3, S3_BUCKET_NAME @@ -21,3 +24,12 @@ def upload_image(content: bytes, image_key: str): "bg_image_url": image_key, } logging.info("Upload successful", extra=extras_logging) + + +def send_mail(subject: str, to: str, message: str): + msg = EmailMessage() + msg["Subject"] = subject + msg["From"] = "noreply@interieur.gouv.fr" + msg["To"] = to + msg.set_content(message) + SMTPClient.send_message(msg) diff --git a/backend/tests/test_email.py b/backend/tests/test_email.py new file mode 100644 index 00000000..19d9829f --- /dev/null +++ b/backend/tests/test_email.py @@ -0,0 +1,14 @@ +from fastapi.testclient import TestClient +from src.main import app +from src.utils import send_mail + +client = TestClient(app) + + +class TestEmail: + def test_email(self): + send_mail( + subject="Demande d'avis à un expert", + to="maildelircgn@interieur.gouv.fr", + message="Message de test", + ) diff --git a/docker-compose.yml b/docker-compose.yml index c74aab11..fa309953 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,8 @@ services: - S3_BUCKET_NAME=basegun-s3 - AWS_ACCESS_KEY_ID=minioadmin - AWS_SECRET_ACCESS_KEY=minioadmin + - EMAIL_HOST=mailpit + - EMAIL_PORT=1025 - http_proxy - https_proxy - no_proxy @@ -58,4 +60,14 @@ services: - KEYCLOAK_ADMIN=admin - KEYCLOAK_ADMIN_PASSWORD=password ports: - - 8080:8080 \ No newline at end of file + - 8080:8080 + + # Mock Email server + mailpit: + image: axllent/mailpit + ports: + - 8025:8025 + - 1025:1025 + environment: + MP_SMTP_AUTH_ACCEPT_ANY: 1 + MP_SMTP_AUTH_ALLOW_INSECURE: 1 \ No newline at end of file