Skip to content

Commit

Permalink
feat: ✨ add email client with local email server
Browse files Browse the repository at this point in the history
  • Loading branch information
thomashbrnrd committed Jul 17, 2024
1 parent 1746cfd commit a201eb9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion backend/src/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from datetime import datetime
from smtplib import SMTP

import boto3
from gelfformatter import GelfFormatter
Expand Down Expand Up @@ -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",
]
]

# Emails
SMTPClient = SMTP(os.environ["EMAIL_HOST"], os.environ["EMAIL_PORT"])
12 changes: 12 additions & 0 deletions backend/src/utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"] = "[email protected]"
msg["To"] = to
msg.set_content(message)
SMTPClient.send_message(msg)
14 changes: 14 additions & 0 deletions backend/tests/test_email.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
message="Message de test",
)
14 changes: 13 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,4 +60,14 @@ services:
- KEYCLOAK_ADMIN=admin
- KEYCLOAK_ADMIN_PASSWORD=password
ports:
- 8080:8080
- 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

0 comments on commit a201eb9

Please sign in to comment.