Add Slack notification support to email backend#108
Conversation
Implement a new email backend that sends notifications to Slack whenever an email is sent. This backend wraps around the existing console and SendGrid email backends, allowing for easy integration in both development and production environments. Update settings to configure the new backend and add a Slack webhook URL for notifications.
WalkthroughThis change introduces a new email backend, Changes
Sequence Diagram(s)sequenceDiagram
participant App as Application
participant SNB as SlackNotificationEmailBackend
participant Fallback as Wrapped Email Backend
participant Slack as Slack Webhook API
App->>SNB: send_messages(email_list)
SNB->>Fallback: open connection (if needed)
SNB->>Fallback: send emails
Fallback-->>SNB: email send status
alt Slack webhook is configured
SNB->>Slack: POST notification (details: recipients, sender, subject)
Slack-->>SNB: acknowledgment/response
end
SNB->>Fallback: close connection (if needed)
SNB-->>App: return send status
Tip ⚡🧪 Multi-step agentic review comment chat (experimental)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
web/settings.py (1)
243-244: Consider replacing print statements with logging.Using
- print("Using console email backend with Slack notifications for development") + import logging + logger = logging.getLogger(__name__) + logger.info("Using console email backend with Slack notifications for development")web/email_backend.py (1)
35-45: Consider batching Slack notifications for large email sends.Right now, a Slack POST is triggered for every email sent. If many emails are sent at once, Slack requests might bottleneck. You could provide an optional batching mechanism to reduce overhead, though it may be an edge case.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
web/email_backend.py(1 hunks)web/settings.py(2 hunks)
🔇 Additional comments (8)
web/settings.py (2)
249-249: Confirmed production email backend change.Switching the email backend to SlackNotificationEmailBackend in production makes sense and aligns with the PR objective.
282-284:Details
✅ Verification successful
Ensure secure handling of webhook values.
Storing the Slack webhook in an environment variable is a good practice. Double-check that the webhook is never logged or exposed elsewhere in code or logs.
Consider verifying no sensitive data is accidentally exposed:
🏁 Script executed:
#!/bin/bash # Search for any references to EMAIL_SLACK_WEBHOOK or SLACK_WEBHOOK_URL outside settings rg 'EMAIL_SLACK_WEBHOOK|SLACK_WEBHOOK_URL' -A 5Length of output: 3021
Secure webhook values handling verified
I've reviewed the usage of the Slack webhook in the codebase. The environment variables
SLACK_WEBHOOK_URLandEMAIL_SLACK_WEBHOOKare properly set inweb/settings.pyand referenced securely in other modules without exposing their values in logs. The search output confirms that while these variables are utilized in modules likeweb/utils.py,web/views.py,web/slack.py, andweb/email_backend.py, there is no evidence of their values being logged or printed. Only a generic warning message is printed when the webhook is missing, which does not include any sensitive details.No further changes are required regarding secure handling.
web/email_backend.py (6)
1-2: No issues found with these imports.
4-7: No issues found with these imports.
8-8: No issues found with logger setup.
11-28: Solid approach to initializing a hybrid backend based on DEBUG.The logic for switching between console and Sendgrid backends is straightforward, and your fallback to None for the webhook is a good defensive measure.
29-34: No concerns for open/close delegation.
46-80: Review potential exposure of sensitive recipient data.The
_notify_slackmethod posts recipients’ addresses to Slack, which may be considered PII. Confirm that your Slack channel is private and that this disclosure does not violate privacy requirements or user expectations.Do you want me to scan your codebase for places where personally identifying info might be leaked?
Implement a new email backend that sends notifications to Slack whenever an email is sent. This backend wraps around the existing console and SendGrid email backends, allowing for easy integration in both development and production environments. Update settings to configure the new backend and add a Slack webhook URL for notifications.
Summary by CodeRabbit