A Python command-line tool for automatically extracting code snippets from emails using OpenAI's API. Originally designed for forwarding emails, now enhanced with intelligent code detection and extraction capabilities for developers and teams.
- ๐ค AI-Powered Code Extraction - Uses OpenAI to identify and extract code from emails
- ๐ง Extract code from latest email or search by criteria
- ๐ Search by subject line (perfect for code review emails, patches, etc.)
- ๐ฎ Search by sender (GitHub, GitLab, code sharing services)
- ๐๏ธ Filter unread emails only
- ๐ Watch mode for continuous monitoring
- ๐ Secure Gmail authentication
- ๐ค Optional forwarding to team members
- ๐ป Terminal-friendly code display with syntax highlighting
- ๐ก๏ธ Regex fallback when AI is unavailable
- Clone the repository:
git clone https://github.com/iscolering/gmail-code-extractor.git
cd gmail-code-extractor- Install required dependencies:
pip install -r requirements.txt- Set up environment variables by creating a
.envfile:
cp .env.example .env- Edit
.envwith your credentials:
EMAIL_USERNAME=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
OPENAI_API_KEY=your-openai-api-key
FORWARD_TO=teammate1@email.com,teammate2@email.com # Optional- Enable 2-factor authentication on your Gmail account
- Generate an App Password:
- Go to Google Account Settings
- Security โ 2-Step Verification โ App passwords
- Generate a new app password for "Mail"
- Use this password in your
.envfile
- Get an OpenAI API key from OpenAI Platform
- Add the key to your
.envfile asOPENAI_API_KEY - Ensure you have API credits available
Extract code from the latest email:
python getCodeCLI.py --code-onlyExtract code from unread emails only:
python getCodeCLI.py --unseen --code-onlyExtract code from emails with specific subject:
python getCodeCLI.py --subject "code review" --code-onlyExtract code from specific sender:
python getCodeCLI.py --from "noreply@github.com" --code-onlyContinuously monitor for new emails with code:
python getCodeCLI.py --watch 60 --code-onlyMonitor specific code-related emails:
python getCodeCLI.py --watch 30 --subject "pull request" --code-onlyPerfect for development teams and individual developers:
Code Review Monitoring:
# Extract code from GitHub notifications
python getCodeCLI.py --from "notifications@github.com" --code-only
# Monitor GitLab merge request emails
python getCodeCLI.py --subject "merge request" --watch 300 --code-only
# Extract code snippets from team emails
python getCodeCLI.py --subject "code snippet" --code-onlyDevelopment Workflow:
# Monitor code sharing emails
python getCodeCLI.py --watch 120 --subject "code" --code-only
# Extract patches and diffs
python getCodeCLI.py --subject "patch" --code-only
# Monitor Stack Overflow notifications with code
python getCodeCLI.py --from "noreply@stackoverflow.com" --code-onlyTeam Collaboration:
# Extract code from team discussions
python getCodeCLI.py --unseen --code-only
# Monitor shared code via email
python getCodeCLI.py --watch 60 --from "team@company.com" --code-onlyThe tool still supports the original forwarding functionality:
# Forward emails (original behavior)
python getCodeCLI.py --subject "verification code"
# Forward and extract code
python getCodeCLI.py --from "noreply@service.com"Verify your Gmail credentials:
python getCodeCLI.py --test-login| Option | Description |
|---|---|
--code-only |
Only extract and display code, don't forward emails |
--subject TEXT |
Search for emails with specific subject |
--from EMAIL |
Search for emails from specific sender |
--unseen |
Only search unread emails |
--watch SECONDS |
Watch for new emails (check every N seconds) |
--test-login |
Test IMAP login credentials |
| Variable | Description | Required |
|---|---|---|
EMAIL_USERNAME |
Your Gmail address | Yes |
EMAIL_PASSWORD |
Gmail App Password | Yes |
OPENAI_API_KEY |
OpenAI API key for code extraction | Yes |
FORWARD_TO |
Comma-separated emails for forwarding | No |
- Uses OpenAI GPT models to intelligently identify code snippets
- Recognizes various programming languages
- Handles mixed content (code + text)
- Preserves code formatting and structure
- Regex patterns for common code block formats
- Markdown code block detection (
code) - Inline code detection (
code) - HTML code tag detection (
) - Indented code block detection
- Clean terminal display with separators
- Language identification when possible
- Preserves original code formatting
- Clear source indication (AI vs Regex)
- Python 3.7+
- Gmail account with App Password enabled
- OpenAI API key with available credits
- Internet connection
Create a requirements.txt file:
python-dotenv
openai>=1.0.0
- Never commit your
.env file to version control
- Use Gmail App Passwords, not your regular password
- Keep your OpenAI API key secure
- Monitor your OpenAI API usage and costs
- Consider rate limiting for watch mode to avoid excessive API calls
OpenAI API Errors:
- Verify your API key is correct and active
- Check your OpenAI account has available credits
- Ensure you're not hitting rate limits
Login Failed Error:
- Ensure 2FA is enabled on your Gmail account
- Use an App Password, not your regular password
- Check that IMAP is enabled in Gmail settings
No Code Found:
- Try the regex fallback if AI extraction fails
- Verify the email actually contains code snippets
- Check if the code is in a supported format
Rate Limiting:
- Reduce watch frequency to avoid API limits
- Consider batching requests for multiple emails
- Fork the repository
- Create a feature branch (
git checkout -b feature/code-extraction)
- Commit your changes (
git commit -m 'Add code extraction feature')
- Push to the branch (
git push origin feature/code-extraction)
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues, please open an issue on GitHub.
- Added OpenAI API integration for intelligent code extraction
- Implemented regex fallback for code detection
- Added
--code-only mode
- Enhanced terminal output formatting
- Maintained backward compatibility with forwarding features
- Basic email forwarding functionality
- Search by subject and sender
- Watch mode for continuous monitoring