3.1 KiB
3.1 KiB
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
- Paste Raw MIME Email: In the extension UI, paste the raw MIME email content into the "Raw MIME Email" textarea.
- Generate Draft: Click the "Auto-Draft with GPT" button to generate a response based on the parsed email.
In Code
// 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 headerstextContent: Plain text content of the emailhtmlContent: 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 subjectdate: Email datesender: Object withnameandemailpropertiesrecipient: Object withnameandemailpropertiescontent: The email content (HTML if available, otherwise plain text)cc: CC recipientsbcc: BCC recipientsmessageId: Message IDinReplyTo: In-Reply-To headerreferences: References headerheaders: All email headersraw: The original raw MIME content
Testing
You can test the parser using the test_parser.js script:
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