Send email via AWS SES. Args: to: Recipient email address subject: Email subject body: Email body html: True if body is HTML Returns: True if sent successfully
(self, to: str, subject: str, body: str, html: bool = False)
| 550 | ) |
| 551 | |
| 552 | def send(self, to: str, subject: str, body: str, html: bool = False) -> bool: |
| 553 | """ |
| 554 | Send email via AWS SES. |
| 555 | |
| 556 | Args: |
| 557 | to: Recipient email address |
| 558 | subject: Email subject |
| 559 | body: Email body |
| 560 | html: True if body is HTML |
| 561 | |
| 562 | Returns: |
| 563 | True if sent successfully |
| 564 | """ |
| 565 | try: |
| 566 | if html: |
| 567 | body_part = {'Html': {'Charset': 'UTF-8', 'Data': body}} |
| 568 | else: |
| 569 | body_part = {'Text': {'Charset': 'UTF-8', 'Data': body}} |
| 570 | |
| 571 | response = self.ses_client.send_email( |
| 572 | Source=f"{self.config.from_name} <{self.config.from_address}>", |
| 573 | Destination={'ToAddresses': [to]}, |
| 574 | Message={ |
| 575 | 'Subject': {'Charset': 'UTF-8', 'Data': subject}, |
| 576 | 'Body': body_part |
| 577 | } |
| 578 | ) |
| 579 | |
| 580 | message_id = response.get('MessageId') |
| 581 | logging.info(f"[EMAIL] SES email sent to {to}, MessageId: {message_id}") |
| 582 | return True |
| 583 | |
| 584 | except self.ClientError as e: |
| 585 | error_code = e.response['Error']['Code'] |
| 586 | error_message = e.response['Error']['Message'] |
| 587 | |
| 588 | if error_code == 'MessageRejected': |
| 589 | logging.error(f"[EMAIL] SES rejected message: {error_message}") |
| 590 | elif error_code == 'ConfigurationSetDoesNotExist': |
| 591 | logging.error(f"[EMAIL] SES configuration error: {error_message}") |
| 592 | else: |
| 593 | logging.error(f"[EMAIL] SES error ({error_code}): {error_message}") |
| 594 | |
| 595 | raise |
| 596 | |
| 597 | except Exception as e: |
| 598 | logging.error(f"[EMAIL] SES unexpected error: {e}") |
| 599 | raise |
| 600 | |
| 601 | def test_connection(self) -> bool: |
| 602 | """ |