Bounce management

This commit is contained in:
James Pattinson
2025-11-10 16:57:29 +00:00
parent 7fd237c28b
commit 051bd05149
15 changed files with 1198 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ from ..core.database import get_db
from ..models.models import EmailTemplate
from sqlalchemy.orm import Session
from ..core.config import settings
from .bounce_service import bounce_service
class EmailService:
@@ -21,7 +22,8 @@ class EmailService:
to_email: str,
subject: str,
html_body: str,
text_body: Optional[str] = None
text_body: Optional[str] = None,
db: Optional[Session] = None
) -> dict:
"""
Send an email using SMTP2GO API
@@ -31,10 +33,18 @@ class EmailService:
subject: Email subject
html_body: HTML content of the email
text_body: Plain text content (optional)
db: Database session for bounce checking
Returns:
dict: API response
Raises:
ValueError: If email is bounced or invalid
"""
# Check if email is bounced before sending
if db and bounce_service.is_email_bounced(to_email, db):
raise ValueError(f"Email {to_email} is in bounce list and cannot receive emails")
payload = {
"to": [to_email],
"sender": f"{self.from_name} <{self.from_email}>",
@@ -89,7 +99,7 @@ class EmailService:
if template.text_body:
text_body = self.render_template(template.text_body, variables)
return await self.send_email(to_email, subject, html_body, text_body)
return await self.send_email(to_email, subject, html_body, text_body, db)
async def send_welcome_email(self, to_email: str, first_name: str, db: Session) -> dict:
"""Send welcome email to new user"""