Send email via SendGrid API. 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)
| 465 | raise ImportError(error_message) |
| 466 | |
| 467 | def send(self, to: str, subject: str, body: str, html: bool = False) -> bool: |
| 468 | """ |
| 469 | Send email via SendGrid API. |
| 470 | |
| 471 | Args: |
| 472 | to: Recipient email address |
| 473 | subject: Email subject |
| 474 | body: Email body |
| 475 | html: True if body is HTML |
| 476 | |
| 477 | Returns: |
| 478 | True if sent successfully |
| 479 | """ |
| 480 | try: |
| 481 | message = self.Mail( |
| 482 | from_email=(self.config.from_address, self.config.from_name), |
| 483 | to_emails=to, |
| 484 | subject=subject, |
| 485 | html_content=body if html else None, |
| 486 | plain_text_content=body if not html else None |
| 487 | ) |
| 488 | |
| 489 | sg = self.SendGridAPIClient(self.config.sendgrid_api_key) |
| 490 | response = sg.send(message) |
| 491 | |
| 492 | if response.status_code in (200, 201, 202): |
| 493 | logging.info(f"[EMAIL] SendGrid email sent to {to}") |
| 494 | return True |
| 495 | else: |
| 496 | logging.error(f"[EMAIL] SendGrid returned status {response.status_code}") |
| 497 | return False |
| 498 | |
| 499 | except Exception as e: |
| 500 | logging.error(f"[EMAIL] SendGrid error: {e}") |
| 501 | raise |
| 502 | |
| 503 | def test_connection(self) -> bool: |
| 504 | """ |