File size: 12,837 Bytes
389e212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""Email service for sending notifications via SMTP.

This service provides async email sending capabilities with support for:
- HTML and plain text emails
- Attachments
- Multiple recipients (to, cc, bcc)
- Email templates using Jinja2
- Retry logic with exponential backoff
- Connection pooling
"""

import asyncio
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
from typing import List, Optional, Dict, Any, Union
from pathlib import Path
import aiosmtplib
from email_validator import validate_email, EmailNotValidError
from pydantic import BaseModel, EmailStr, Field, validator
from jinja2 import Environment, FileSystemLoader, select_autoescape
import structlog
from tenacity import retry, stop_after_attempt, wait_exponential

from src.core.config import settings
from src.core.logging import get_logger

logger = get_logger(__name__)


class EmailAttachment(BaseModel):
    """Email attachment model."""
    filename: str
    content: Union[bytes, str]
    content_type: str = "application/octet-stream"
    
    @validator("content")
    def validate_content(cls, v):
        """Ensure content is bytes."""
        if isinstance(v, str):
            return v.encode()
        return v


class EmailMessage(BaseModel):
    """Email message model with validation."""
    to: List[EmailStr]
    subject: str
    body: Optional[str] = None
    html_body: Optional[str] = None
    cc: Optional[List[EmailStr]] = None
    bcc: Optional[List[EmailStr]] = None
    reply_to: Optional[EmailStr] = None
    attachments: Optional[List[EmailAttachment]] = None
    headers: Optional[Dict[str, str]] = None
    template: Optional[str] = None
    template_data: Optional[Dict[str, Any]] = None
    
    @validator("to", "cc", "bcc", pre=True)
    def validate_emails(cls, v):
        """Validate email addresses."""
        if v is None:
            return v
        if isinstance(v, str):
            v = [v]
        validated = []
        for email in v:
            try:
                validated_email = validate_email(email)
                validated.append(validated_email.email)
            except EmailNotValidError as e:
                logger.warning(f"Invalid email address: {email} - {e}")
                continue
        return validated
    
    @validator("body", always=True)
    def validate_body(cls, v, values):
        """Ensure at least one body type is provided."""
        if not v and not values.get("html_body") and not values.get("template"):
            raise ValueError("Either body, html_body, or template must be provided")
        return v


class SMTPConfig(BaseModel):
    """SMTP configuration model."""
    host: str = Field(default_factory=lambda: settings.smtp_host)
    port: int = Field(default_factory=lambda: settings.smtp_port)
    username: Optional[str] = Field(default_factory=lambda: settings.smtp_username)
    password: Optional[str] = Field(default_factory=lambda: settings.smtp_password.get_secret_value() if settings.smtp_password else None)
    use_tls: bool = Field(default_factory=lambda: settings.smtp_use_tls)
    use_ssl: bool = Field(default_factory=lambda: settings.smtp_use_ssl)
    timeout: int = Field(default=30)
    from_email: EmailStr = Field(default_factory=lambda: settings.smtp_from_email)
    from_name: str = Field(default_factory=lambda: settings.smtp_from_name)


class EmailService:
    """Service for sending emails via SMTP."""
    
    def __init__(self, config: Optional[SMTPConfig] = None):
        """Initialize email service.
        
        Args:
            config: SMTP configuration. If not provided, uses settings.
        """
        self.config = config or SMTPConfig()
        self._template_env = self._setup_template_environment()
        self._connection_lock = asyncio.Lock()
        self._smtp_client: Optional[aiosmtplib.SMTP] = None
        
    def _setup_template_environment(self) -> Environment:
        """Setup Jinja2 template environment."""
        template_dir = Path(__file__).parent.parent / "templates" / "email"
        template_dir.mkdir(parents=True, exist_ok=True)
        
        return Environment(
            loader=FileSystemLoader(str(template_dir)),
            autoescape=select_autoescape(["html", "xml"]),
            enable_async=True
        )
    
    async def _get_smtp_client(self) -> aiosmtplib.SMTP:
        """Get or create SMTP client with connection pooling."""
        async with self._connection_lock:
            if self._smtp_client is None or not self._smtp_client.is_connected:
                self._smtp_client = aiosmtplib.SMTP(
                    hostname=self.config.host,
                    port=self.config.port,
                    timeout=self.config.timeout,
                    use_tls=self.config.use_ssl
                )
                
                await self._smtp_client.connect()
                
                if self.config.use_tls and not self.config.use_ssl:
                    await self._smtp_client.starttls()
                
                if self.config.username and self.config.password:
                    await self._smtp_client.login(
                        self.config.username,
                        self.config.password
                    )
                    
            return self._smtp_client
    
    async def _render_template(
        self,
        template_name: str,
        context: Dict[str, Any]
    ) -> tuple[str, str]:
        """Render email template.
        
        Args:
            template_name: Name of the template file
            context: Template context data
            
        Returns:
            Tuple of (html_body, text_body)
        """
        # Add default context
        default_context = {
            "app_name": "Cidadão.AI",
            "app_url": settings.app_url,
            "support_email": settings.support_email,
        }
        context = {**default_context, **context}
        
        # Render HTML template
        html_template = self._template_env.get_template(f"{template_name}.html")
        html_body = await html_template.render_async(**context)
        
        # Try to render text template, fallback to HTML strip
        try:
            text_template = self._template_env.get_template(f"{template_name}.txt")
            text_body = await text_template.render_async(**context)
        except Exception:
            # Simple HTML to text conversion
            import re
            text_body = re.sub(r"<[^>]+>", "", html_body)
            text_body = re.sub(r"\s+", " ", text_body).strip()
        
        return html_body, text_body
    
    def _create_message(self, email: EmailMessage) -> MIMEMultipart:
        """Create MIME message from EmailMessage."""
        msg = MIMEMultipart("alternative")
        
        # Set headers
        msg["Subject"] = email.subject
        msg["From"] = f"{self.config.from_name} <{self.config.from_email}>"
        msg["To"] = ", ".join(email.to)
        
        if email.cc:
            msg["Cc"] = ", ".join(email.cc)
        
        if email.reply_to:
            msg["Reply-To"] = email.reply_to
        
        # Add custom headers
        if email.headers:
            for key, value in email.headers.items():
                msg[key] = value
        
        # Add text part
        if email.body:
            msg.attach(MIMEText(email.body, "plain"))
        
        # Add HTML part
        if email.html_body:
            msg.attach(MIMEText(email.html_body, "html"))
        
        # Add attachments
        if email.attachments:
            for attachment in email.attachments:
                part = MIMEBase("application", "octet-stream")
                part.set_payload(attachment.content)
                encoders.encode_base64(part)
                part.add_header(
                    "Content-Disposition",
                    f"attachment; filename={attachment.filename}"
                )
                msg.attach(part)
        
        return msg
    
    @retry(
        stop=stop_after_attempt(3),
        wait=wait_exponential(multiplier=1, min=4, max=10)
    )
    async def send_email(self, email: EmailMessage) -> bool:
        """Send an email message.
        
        Args:
            email: Email message to send
            
        Returns:
            True if email was sent successfully
        """
        try:
            # Render template if specified
            if email.template:
                html_body, text_body = await self._render_template(
                    email.template,
                    email.template_data or {}
                )
                email.html_body = html_body
                email.body = text_body
            
            # Create MIME message
            msg = self._create_message(email)
            
            # Get SMTP client
            smtp = await self._get_smtp_client()
            
            # Prepare recipients
            recipients = email.to.copy()
            if email.cc:
                recipients.extend(email.cc)
            if email.bcc:
                recipients.extend(email.bcc)
            
            # Send email
            await smtp.send_message(msg)
            
            logger.info(
                "Email sent successfully",
                subject=email.subject,
                recipients=len(recipients),
                has_attachments=bool(email.attachments)
            )
            
            return True
            
        except Exception as e:
            logger.error(
                "Failed to send email",
                subject=email.subject,
                error=str(e),
                exc_info=True
            )
            raise
    
    async def send_batch(
        self,
        emails: List[EmailMessage],
        max_concurrent: int = 5
    ) -> List[bool]:
        """Send multiple emails concurrently.
        
        Args:
            emails: List of email messages
            max_concurrent: Maximum concurrent sends
            
        Returns:
            List of success status for each email
        """
        semaphore = asyncio.Semaphore(max_concurrent)
        
        async def send_with_semaphore(email: EmailMessage) -> bool:
            async with semaphore:
                try:
                    return await self.send_email(email)
                except Exception:
                    return False
        
        tasks = [send_with_semaphore(email) for email in emails]
        results = await asyncio.gather(*tasks, return_exceptions=True)
        
        return [
            result if isinstance(result, bool) else False
            for result in results
        ]
    
    async def close(self):
        """Close SMTP connection."""
        if self._smtp_client and self._smtp_client.is_connected:
            await self._smtp_client.quit()
            self._smtp_client = None


# Singleton instance
email_service = EmailService()


# Convenience functions
async def send_email(
    to: Union[str, List[str]],
    subject: str,
    body: Optional[str] = None,
    html_body: Optional[str] = None,
    template: Optional[str] = None,
    template_data: Optional[Dict[str, Any]] = None,
    attachments: Optional[List[EmailAttachment]] = None,
    **kwargs
) -> bool:
    """Send an email using the default email service.
    
    Args:
        to: Recipient email address(es)
        subject: Email subject
        body: Plain text body
        html_body: HTML body
        template: Template name to render
        template_data: Data for template rendering
        attachments: List of attachments
        **kwargs: Additional email fields (cc, bcc, reply_to, headers)
        
    Returns:
        True if email was sent successfully
    """
    if isinstance(to, str):
        to = [to]
    
    email = EmailMessage(
        to=to,
        subject=subject,
        body=body,
        html_body=html_body,
        template=template,
        template_data=template_data,
        attachments=attachments,
        **kwargs
    )
    
    return await email_service.send_email(email)


async def send_template_email(
    to: Union[str, List[str]],
    subject: str,
    template: str,
    template_data: Optional[Dict[str, Any]] = None,
    **kwargs
) -> bool:
    """Send an email using a template.
    
    Args:
        to: Recipient email address(es)
        subject: Email subject
        template: Template name
        template_data: Template context data
        **kwargs: Additional email fields
        
    Returns:
        True if email was sent successfully
    """
    return await send_email(
        to=to,
        subject=subject,
        template=template,
        template_data=template_data,
        **kwargs
    )