infrastructure.events.handlers.email_event_handler¶
src.infrastructure.events.handlers.email_event_handler
¶
Email event handler stub for domain events.
This module implements a STUB email handler that logs when emails would be sent. Provides structure for future email service integration (SendGrid, AWS SES, etc.) without blocking current development.
Email Templates Needed (Future): - welcome_email: Sent after UserRegistrationSucceeded - Subject: "Welcome to Dashtam!" - Body: Welcome message + email verification link - Variables: {user_email, verification_token}
- password_changed_email: Sent after UserPasswordChangeSucceeded
- Subject: "Your Dashtam password was changed"
- Body: Security notification + support contact
- Variables: {user_email, change_timestamp, ip_address}
Integration Strategy (Future Phase): 1. Create EmailProtocol in src/domain/protocols/email_protocol.py 2. Implement SendGridEmailAdapter or SESEmailAdapter in src/infrastructure/email/ 3. Add email template engine (Jinja2) in src/infrastructure/email/templates/ 4. Update this handler to use EmailProtocol instead of logging 5. Add email sending to container with get_email_service()
Usage
Container wires up subscriptions at startup¶
event_bus = get_event_bus() email_handler = EmailEventHandler(logger=get_logger())
Subscribe to SUCCEEDED events only¶
event_bus.subscribe(UserRegistrationSucceeded, email_handler.handle_user_registration_succeeded) event_bus.subscribe(UserPasswordChangeSucceeded, email_handler.handle_user_password_change_succeeded)
Reference
- docs/architecture/domain-events-architecture.md (Lines 1304-1362)
Classes¶
EmailEventHandler
¶
Event handler stub for email sending.
STUB IMPLEMENTATION: Currently logs when emails would be sent. Replace with real EmailProtocol implementation when email service is ready.
Subscribes to SUCCEEDED events only (don't email on ATTEMPT or FAILURE).
Attributes:
| Name | Type | Description |
|---|---|---|
_logger |
Logger protocol implementation (from container). |
|
_settings |
Application settings for configuration (e.g., verification URL). |
Example
Create handler¶
handler = EmailEventHandler(logger=get_logger(), settings=get_settings())
Subscribe to events (in container)¶
event_bus.subscribe(UserRegistrationSucceeded, handler.handle_user_registration_succeeded)
Events automatically trigger email logging¶
await event_bus.publish(UserRegistrationSucceeded( ... user_id=uuid7(), ... email="test@example.com" ... ))
Log output:¶
Source code in src/infrastructure/events/handlers/email_event_handler.py
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 | |
Functions¶
__init__
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
LoggerProtocol
|
Logger protocol implementation from container. Used for structured logging of email events (stub only). |
required |
settings
|
Settings
|
Application settings providing configuration such as verification_url_base for email links. |
required |
Example
from src.core.container import get_logger, get_settings logger = get_logger() settings = get_settings() handler = EmailEventHandler(logger=logger, settings=settings)
Source code in src/infrastructure/events/handlers/email_event_handler.py
handle_user_registration_succeeded
async
¶
Send welcome email after successful registration (STUB).
STUB: Logs email would be sent. Future: Send actual email via EmailProtocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
UserRegistrationSucceeded
|
UserRegistrationSucceeded event with user_id and email. |
required |
Email Template (Future): - Template: welcome_email - Subject: "Welcome to Dashtam!" - To: event.email - Variables: - user_email: event.email - verification_token: (generated from user_id) - verification_link: f"{settings.verification_url_base}/verify?token={{token}}"
Notes
- Email sent asynchronously (fail-open - don't block registration)
- Includes email verification link (click to verify)
- User can resend verification email if needed
Source code in src/infrastructure/events/handlers/email_event_handler.py
handle_user_password_change_succeeded
async
¶
Send password change notification email (STUB).
STUB: Logs email would be sent. Future: Send actual email via EmailProtocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
UserPasswordChangeSucceeded
|
UserPasswordChangeSucceeded event with user_id. |
required |
Email Template (Future): - Template: password_changed_email - Subject: "Your Dashtam password was changed" - To: (fetch user email from database via user_id) - Variables: - change_timestamp: event.occurred_at - initiated_by: event.initiated_by - support_email: "support@dashtam.com" - support_message: "If you didn't make this change, contact us immediately."
Notes
- CRITICAL security notification (user must be alerted)
- Sent even if initiated_by="admin" (user should know)
- Email sent asynchronously (fail-open)
- Future: Fetch user email from database (not in event to keep events lean)
Source code in src/infrastructure/events/handlers/email_event_handler.py
handle_password_reset_request_succeeded
async
¶
Send password reset email after successful request (STUB).
STUB: Logs email would be sent. Future: Send actual email via EmailProtocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
PasswordResetRequestSucceeded
|
PasswordResetRequestSucceeded event with user_id, email, and reset_token. |
required |
Email Template (Future): - Template: password_reset_email - Subject: "Reset your Dashtam password" - To: event.email - Variables: - user_email: event.email - reset_token: event.reset_token - reset_link: f"{settings.reset_url_base}/reset?token={{token}}" - expiration: "This link expires in 1 hour"
Notes
- Email sent asynchronously (fail-open - don't block request)
- Includes password reset link with token
- Link expires after 1 hour (security requirement)
- User can request new reset email if needed
Source code in src/infrastructure/events/handlers/email_event_handler.py
handle_password_reset_confirm_succeeded
async
¶
Send password reset confirmation email (STUB).
STUB: Logs email would be sent. Future: Send actual email via EmailProtocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
PasswordResetConfirmSucceeded
|
PasswordResetConfirmSucceeded event with user_id and email. |
required |
Email Template (Future): - Template: password_reset_completed_email - Subject: "Your Dashtam password was reset" - To: event.email - Variables: - user_email: event.email - reset_timestamp: event.occurred_at - support_email: "support@dashtam.com" - support_message: "If you didn't make this change, contact us immediately."
Notes
- CRITICAL security notification (user must be alerted)
- Sent after password reset is complete
- Email sent asynchronously (fail-open)
Source code in src/infrastructure/events/handlers/email_event_handler.py
handle_email_verification_succeeded
async
¶
Send welcome email after email verification (STUB - Optional).
STUB: Logs email would be sent. Future: Send actual email via EmailProtocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
EmailVerificationSucceeded
|
EmailVerificationSucceeded event with user_id and email. |
required |
Email Template (Future): - Template: email_verified_welcome_email - Subject: "Welcome to Dashtam! Your email is verified" - To: event.email - Variables: - user_email: event.email - dashboard_link: f"{settings.app_base_url}/dashboard" - getting_started_guide: Link to user guide
Notes
- Optional welcome email (user already received one at registration)
- Email sent asynchronously (fail-open)
- Can be disabled in settings if redundant