|
2 | 2 | Third party email service configuration for sending emails. |
3 | 3 | """ |
4 | 4 |
|
5 | | -from typing import Dict, Union |
6 | | - |
7 | 5 | from smtplib import SMTPException, SMTPAuthenticationError, SMTPSenderRefused |
8 | | -import botocore.exceptions |
9 | | - |
10 | | -from app.utils import EmailSender, SESEmailSender |
11 | | -from app.schemas.email import SendEmailRequestBody, EmailProvider |
| 6 | +from app.utils import EmailSender |
| 7 | +from app.schemas.email import SendEmailRequestBody |
12 | 8 |
|
13 | 9 |
|
14 | 10 | class EmailService: |
15 | 11 | """ |
16 | 12 | Service layer for handling email sending logic. |
17 | 13 | """ |
18 | 14 |
|
19 | | - def __init__( |
20 | | - self, email_sender: Dict[EmailProvider, Union[EmailSender, SESEmailSender]] |
21 | | - ): |
| 15 | + def __init__(self, email_sender: EmailSender): |
22 | 16 | self.email_sender = email_sender |
23 | 17 |
|
24 | 18 | def send_email(self, email_details: SendEmailRequestBody, body: str): |
25 | 19 | """ |
26 | 20 | Sends an email based on email details. |
| 21 | +
|
27 | 22 | Args: |
28 | 23 | email_details (SendEmailRequestBody): Details for the email. |
29 | 24 | body (str): body of the email. |
30 | 25 |
|
31 | 26 | Returns: |
32 | | - dict: A response dictionary containing: |
33 | | - - "success" (bool): Indicates whether the email was sent successfully. |
34 | | - - "error": Error message if the email sending fails. |
| 27 | + bool: True if no failure occured on sending email. |
35 | 28 | """ |
36 | 29 | try: |
37 | | - result = self._send_email_by_provider(email_details, body) |
| 30 | + result = self.email_sender.send_email( |
| 31 | + to_email=email_details.recipient, |
| 32 | + subject=email_details.subject, |
| 33 | + body=body, |
| 34 | + cc=email_details.cc, |
| 35 | + bcc=email_details.bcc, |
| 36 | + is_html=True, |
| 37 | + ) |
38 | 38 | return result |
39 | | - except Exception as error: |
40 | | - self._handle_email_error(error) |
41 | | - |
42 | | - def _send_email_by_provider(self, email_details: SendEmailRequestBody, body: str): |
43 | | - provider = email_details.provider |
44 | | - sender = self.email_sender.get(provider) |
45 | | - result = sender.send_email( |
46 | | - to_email=email_details.recipient, |
47 | | - subject=email_details.subject, |
48 | | - body=body, |
49 | | - cc=email_details.cc, |
50 | | - bcc=email_details.bcc, |
51 | | - is_html=True, |
52 | | - ) |
53 | | - return result |
54 | | - |
55 | | - def _handle_email_error(self, error: Exception): |
56 | | - if isinstance( |
57 | | - error, (SMTPAuthenticationError, botocore.exceptions.ClientError) |
58 | | - ): |
59 | | - raise PermissionError(f"Authentication failed: {str(error)}") from error |
60 | | - elif isinstance(error, SMTPSenderRefused): |
61 | | - raise ValueError(f"Email address refused: {str(error)}") from error |
62 | | - else: |
63 | | - raise ConnectionError(f"Email sending failed: {str(error)}") from error |
| 39 | + except SMTPAuthenticationError as exc: |
| 40 | + raise PermissionError( |
| 41 | + "Failed to authenticate with the SMTP server." |
| 42 | + ) from exc |
| 43 | + except SMTPSenderRefused as exc: |
| 44 | + raise ValueError("Sender address refused by the SMTP server.") from exc |
| 45 | + except SMTPException as smtp_exc: |
| 46 | + raise ConnectionError(f"SMTP error occurred: {smtp_exc}") from smtp_exc |
0 commit comments