|
| 1 | +# SendGrid Email Integration Setup |
| 2 | + |
| 3 | +This document explains how to set up and use the SendGrid email integration for the MTA Data Assistant. |
| 4 | + |
| 5 | +## Setup Instructions |
| 6 | + |
| 7 | +### 1. Environment Variables |
| 8 | + |
| 9 | +Add your SendGrid API key to your environment variables: |
| 10 | + |
| 11 | +```bash |
| 12 | +# .env.local |
| 13 | +SENDGRID_API_KEY=your_sendgrid_api_key_here |
| 14 | +``` |
| 15 | + |
| 16 | +### 2. DNS Records (Already Completed) |
| 17 | + |
| 18 | +The following DNS records have been added to your `mta-data.com` domain: |
| 19 | + |
| 20 | +- **CNAME** `url622.mta-data.com` → `sendgrid.net` |
| 21 | +- **CNAME** `56285903.mta-data.com` → `sendgrid.net` |
| 22 | +- **CNAME** `em3637.mta-data.com` → `u56285903.wl154.sendgrid.net` |
| 23 | +- **CNAME** `s1._domainkey.mta-data.com` → `s1.domainkey.u56285903.wl154.sendgrid.net` |
| 24 | +- **CNAME** `s2._domainkey.mta-data.com` → `s2.domainkey.u56285903.wl154.sendgrid.net` |
| 25 | +- **TXT** `_dmarc.mta-data.com` → `v=DMARC1; p=none;` |
| 26 | + |
| 27 | +### 3. Features Available |
| 28 | + |
| 29 | +#### For Users: |
| 30 | +- **Email Button**: Click the email icon on any AI response to send it via email |
| 31 | +- **Email Modal**: Enter recipient email address and send formatted emails |
| 32 | +- **Professional Templates**: Beautiful HTML emails with MTA branding |
| 33 | + |
| 34 | +#### For AI Assistant: |
| 35 | +- **Direct Email Tool**: The AI can send emails directly using the `sendEmail` tool |
| 36 | +- **Natural Language**: Users can ask "Email this analysis to john@example.com" |
| 37 | + |
| 38 | +### 4. Testing |
| 39 | + |
| 40 | +#### Test the Integration: |
| 41 | +```bash |
| 42 | +# Send a test email |
| 43 | +curl -X POST http://localhost:3000/api/email/test \ |
| 44 | + -H "Content-Type: application/json" \ |
| 45 | + -d '{"to": "your-email@example.com"}' |
| 46 | +``` |
| 47 | + |
| 48 | +#### Test in the UI: |
| 49 | +1. Start your development server: `npm run dev` |
| 50 | +2. Go to the chat interface |
| 51 | +3. Ask a question and get a response |
| 52 | +4. Click the email icon (📧) on the response |
| 53 | +5. Enter an email address and send |
| 54 | + |
| 55 | +### 5. Email Features |
| 56 | + |
| 57 | +- **HTML Formatting**: Professional email templates with MTA branding |
| 58 | +- **Conversation Links**: Links to continue conversations in the web interface |
| 59 | +- **Mobile Responsive**: Emails look great on all devices |
| 60 | +- **Authentication**: Emails are sent from your verified `mta-data.com` domain |
| 61 | +- **Reply-To**: Recipients can reply to the sender |
| 62 | + |
| 63 | +### 6. API Endpoints |
| 64 | + |
| 65 | +- `POST /api/email/chat` - Send chat response via email |
| 66 | +- `POST /api/email/test` - Send test email |
| 67 | + |
| 68 | +### 7. Troubleshooting |
| 69 | + |
| 70 | +#### Common Issues: |
| 71 | + |
| 72 | +1. **"Invalid API Key"**: Check that `SENDGRID_API_KEY` is set correctly |
| 73 | +2. **"Sender not verified"**: Ensure your domain is verified in SendGrid |
| 74 | +3. **"Email not received"**: Check spam folder, verify recipient email |
| 75 | + |
| 76 | +#### Debug Steps: |
| 77 | + |
| 78 | +1. Check environment variables: `console.log(process.env.SENDGRID_API_KEY)` |
| 79 | +2. Test with the test endpoint: `/api/email/test` |
| 80 | +3. Check SendGrid dashboard for delivery status |
| 81 | +4. Verify DNS records are propagated |
| 82 | + |
| 83 | +### 8. Usage Examples |
| 84 | + |
| 85 | +#### User Interface: |
| 86 | +```typescript |
| 87 | +// User clicks email button → modal opens → enters email → sends |
| 88 | +``` |
| 89 | + |
| 90 | +#### AI Assistant: |
| 91 | +```typescript |
| 92 | +// User: "Email this analysis to john@example.com" |
| 93 | +// AI: Uses sendEmail tool to send the response |
| 94 | +``` |
| 95 | + |
| 96 | +#### Direct API: |
| 97 | +```typescript |
| 98 | +const response = await fetch('/api/email/chat', { |
| 99 | + method: 'POST', |
| 100 | + headers: { 'Content-Type': 'application/json' }, |
| 101 | + body: JSON.stringify({ |
| 102 | + to: 'recipient@example.com', |
| 103 | + messageContent: 'AI response content', |
| 104 | + conversationId: 'conv-123' |
| 105 | + }) |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +## Security Notes |
| 110 | + |
| 111 | +- API key is server-side only (not exposed to client) |
| 112 | +- Email validation prevents injection attacks |
| 113 | +- Rate limiting should be implemented for production |
| 114 | +- Consider implementing user authentication for email features |
| 115 | + |
| 116 | +## Next Steps |
| 117 | + |
| 118 | +1. Test the integration with the test endpoint |
| 119 | +2. Verify emails are received and formatted correctly |
| 120 | +3. Test the UI email functionality |
| 121 | +4. Consider adding email templates for different types of responses |
| 122 | +5. Implement rate limiting for production use |
0 commit comments