Overview
Med Agenda uses the Resend email service to send automated notifications for appointment confirmations, reminders, and other system events. The email system is integrated throughout the application lifecycle to keep patients and doctors informed.Email Service Architecture
The email service uses Resend’s REST API with Spring’s RestTemplate:EmailService.java:13-39
- API Key: Stored in application properties (
@Valueannotation) - From Address: Default sender email ([email protected])
- API URL:
https://api.resend.com/emails - Authentication: Bearer token authentication
When Notifications Are Sent
1. Appointment Confirmation
When a new consultation is created, an automatic confirmation email is sent:ConsultationService.java:68-73
POST /consultations/create
Email Details:
- To: Patient’s email (currently hardcoded to [email protected] for testing)
- Subject: “Consulta confirmada”
- Body: Appointment date and time
2. Appointment Reminders
While not currently implemented in the codebase, the system architecture supports scheduled reminders:Email Integration Flow
Email Payload Structure
The system sends emails using Resend’s API format:from: Sender email addressto: Recipient email address (or array for multiple recipients)subject: Email subject linehtml: HTML-formatted email body
HTML Email Formatting
Currently, emails use simple HTML formatting:EmailService.java:34
Configuration and Security
The email service uses Spring Boot’s@Value annotation for configuration:
application.properties or environment variables:
Error Handling
The current implementation doesn’t include explicit error handling. Enhanced version:Notification Types
Current Notifications
| Event | Trigger | Recipient | Template |
|---|---|---|---|
| Appointment Created | POST /consultations/create | Patient | ”Consulta confirmada” |
Potential Future Notifications
| Event | Trigger | Recipient | Template |
|---|---|---|---|
| Appointment Updated | PUT /consultations/update | Patient & Doctor | ”Consulta reagendada” |
| Appointment Canceled | DELETE /consultations/ | Patient & Doctor | ”Consulta cancelada” |
| 24h Reminder | Scheduled task | Patient | ”Lembrete: Consulta amanhã” |
| 1h Reminder | Scheduled task | Patient | ”Sua consulta é em 1 hora” |
| Diagnosis Created | POST /diagnosis | Patient | ”Novo diagnóstico disponível” |
| Payment Confirmed | Payment status change | Patient | ”Pagamento confirmado” |
Integration with Consultation Lifecycle
Emails are sent during consultation creation:ConsultationService.java:42-76
Resend API Features
Resend provides additional features that can be integrated:- Email Tracking: Track opens, clicks, and bounces
- Templates: Use pre-built email templates
- Attachments: Include PDFs or other documents
- Batch Sending: Send multiple emails efficiently
- Scheduling: Schedule emails for future delivery
- Domain Verification: Use custom domain for professional emails
Testing Email Notifications
During development, use Resend’s test mode:Best Practices
- Use environment variables for API keys and sensitive data
- Implement retry logic for failed email sends
- Log email events for debugging and auditing
- Validate email addresses before sending
- Use HTML templates for professional-looking emails
- Include unsubscribe options for transactional emails
- Rate limit email sending to avoid API throttling
- Send to actual patient emails instead of hardcoded addresses in production
Performance Considerations
- Email sending is synchronous - consider async processing for better performance
- Use Spring’s @Async annotation for non-blocking email sends:
Monitoring and Analytics
Integrate email tracking:sent: Email sent to Resenddelivered: Email delivered to recipientopened: Recipient opened emailclicked: Recipient clicked linkbounced: Email bouncedcomplained: Marked as spam