104 lines
3.1 KiB
Markdown
104 lines
3.1 KiB
Markdown
|
|
# MIME Email Parser for Gmail Auto-Draft Extension
|
||
|
|
|
||
|
|
This component allows the Gmail Auto-Draft extension to parse raw MIME emails and extract their content properly.
|
||
|
|
|
||
|
|
## Features
|
||
|
|
|
||
|
|
- Parse raw MIME email content into structured data
|
||
|
|
- Extract headers, plain text content, and HTML content
|
||
|
|
- Support for multipart emails with nested boundaries
|
||
|
|
- Decode quoted-printable and base64 encoded content
|
||
|
|
- Extract sender, recipient, subject, and other metadata
|
||
|
|
|
||
|
|
## Usage
|
||
|
|
|
||
|
|
### In the Chrome Extension
|
||
|
|
|
||
|
|
1. **Paste Raw MIME Email**: In the extension UI, paste the raw MIME email content into the "Raw MIME Email" textarea.
|
||
|
|
2. **Generate Draft**: Click the "Auto-Draft with GPT" button to generate a response based on the parsed email.
|
||
|
|
|
||
|
|
### In Code
|
||
|
|
|
||
|
|
```javascript
|
||
|
|
// In content.js
|
||
|
|
const mimeContent = document.querySelector('.mime-content').value;
|
||
|
|
if (isMimeEmail(mimeContent)) {
|
||
|
|
// Send to background script for parsing
|
||
|
|
chrome.runtime.sendMessage({
|
||
|
|
action: 'parseMimeEmail',
|
||
|
|
mimeContent
|
||
|
|
}, response => {
|
||
|
|
if (response && response.success) {
|
||
|
|
console.log('Parsed email:', response.emailData);
|
||
|
|
// Use the parsed email data
|
||
|
|
} else {
|
||
|
|
console.error('Failed to parse email:', response.error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// In background.js
|
||
|
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||
|
|
if (message.action === 'parseMimeEmail') {
|
||
|
|
try {
|
||
|
|
const emailData = extractEmailThread(message.mimeContent);
|
||
|
|
sendResponse({ success: true, emailData });
|
||
|
|
} catch (error) {
|
||
|
|
sendResponse({ success: false, error: error.message });
|
||
|
|
}
|
||
|
|
return true; // async
|
||
|
|
}
|
||
|
|
});
|
||
|
|
```
|
||
|
|
|
||
|
|
## API
|
||
|
|
|
||
|
|
### `parseMimeEmail(mimeContent)`
|
||
|
|
|
||
|
|
Parses a raw MIME email into structured data.
|
||
|
|
|
||
|
|
**Parameters:**
|
||
|
|
- `mimeContent` (string): The raw MIME content of the email
|
||
|
|
|
||
|
|
**Returns:** Object with properties:
|
||
|
|
- `headers`: Object containing all email headers
|
||
|
|
- `textContent`: Plain text content of the email
|
||
|
|
- `htmlContent`: HTML content of the email (if available)
|
||
|
|
- `raw`: The original raw MIME content
|
||
|
|
|
||
|
|
### `extractEmailThread(mimeContent)`
|
||
|
|
|
||
|
|
Extracts structured data from a MIME email, including sender, recipient, and content.
|
||
|
|
|
||
|
|
**Parameters:**
|
||
|
|
- `mimeContent` (string): The raw MIME content of the email
|
||
|
|
|
||
|
|
**Returns:** Object with properties:
|
||
|
|
- `subject`: Email subject
|
||
|
|
- `date`: Email date
|
||
|
|
- `sender`: Object with `name` and `email` properties
|
||
|
|
- `recipient`: Object with `name` and `email` properties
|
||
|
|
- `content`: The email content (HTML if available, otherwise plain text)
|
||
|
|
- `cc`: CC recipients
|
||
|
|
- `bcc`: BCC recipients
|
||
|
|
- `messageId`: Message ID
|
||
|
|
- `inReplyTo`: In-Reply-To header
|
||
|
|
- `references`: References header
|
||
|
|
- `headers`: All email headers
|
||
|
|
- `raw`: The original raw MIME content
|
||
|
|
|
||
|
|
## Testing
|
||
|
|
|
||
|
|
You can test the parser using the `test_parser.js` script:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
node test_parser.js
|
||
|
|
```
|
||
|
|
|
||
|
|
This will parse a sample MIME email and output the structured data.
|
||
|
|
|
||
|
|
## Limitations
|
||
|
|
|
||
|
|
- The parser handles common MIME formats but may not support all edge cases
|
||
|
|
- Very large emails may cause performance issues
|
||
|
|
- Some complex nested multipart structures may not be fully parsed
|