A Next.js application that monitors email accounts and automatically downloads PDF attachments, storing them locally while maintaining email metadata in a database.
- Multi-provider email support:
- IMAP (Gmail, Yahoo, etc.)
- Gmail API (OAuth2)
- Microsoft Outlook (Graph API)
- Automated PDF attachment processing
- Email metadata tracking
- Real-time monitoring dashboard
- Multiple email account management
- Secure OAuth2 integration
- Node.js >= 18
- PostgreSQL database
- Gmail API project (for Gmail integration)
- Microsoft Azure App Registration (for Outlook integration)
- Clone and install:
git clone <your-repo>
cd my-email-ingestion-app
npm install- Set up environment variables in
.env:
# Required Database Configuration
DATABASE_URL="postgresql://user:password@localhost:5432/email_ingestion"
# PDF Storage Configuration
PDF_STORAGE_PATH="./public/pdfs"
# IMAP Default Settings (for standard IMAP connections)
IMAP_HOST="imap.gmail.com"
IMAP_PORT="993"
# Gmail OAuth2 Credentials (required for Gmail API)
GMAIL_CLIENT_ID="your-client-id"
GMAIL_CLIENT_SECRET="your-client-secret"
GMAIL_REDIRECT_URI="http://localhost:3000/api/auth/gmail/callback"
# Microsoft Graph API Credentials (required for Outlook)
AZURE_TENANT_ID="your-tenant-id"
AZURE_CLIENT_ID="your-client-id"
AZURE_CLIENT_SECRET="your-client-secret"
OUTLOOK_REDIRECT_URI="http://localhost:3000/api/auth/outlook/callback"- Initialize the database:
npx prisma generate
npx prisma db push- Start development server:
npm run dev- Build for production:
npm run build
npm startDATABASE_URL: PostgreSQL connection string
PDF_STORAGE_PATH: Local path for storing PDF attachments (relative to project root)
IMAP_HOST: Default IMAP server hostIMAP_PORT: Default IMAP server port (usually 993 for SSL)
GMAIL_CLIENT_ID: OAuth2 client ID from Google Cloud ConsoleGMAIL_CLIENT_SECRET: OAuth2 client secretGMAIL_REDIRECT_URI: OAuth2 callback URL (must match Google Cloud Console)
AZURE_TENANT_ID: Azure AD tenant IDAZURE_CLIENT_ID: Azure application (client) IDAZURE_CLIENT_SECRET: Azure client secretOUTLOOK_REDIRECT_URI: OAuth2 callback URL (must match Azure portal)
- Navigate to the homepage (http://localhost:3000)
- Click "Add New Configuration"
- Choose your email provider:
- Enter email address
- Select "IMAP" as connection type
- Provide username and password
- Enter IMAP host (if different from default)
- Enter Gmail address
- Select "GMAIL" as connection type
- Click "Authenticate with Gmail"
- Complete Google OAuth flow
- Enter Outlook email
- Select "OUTLOOK" as connection type
- Click "Authenticate with Outlook"
- Complete Microsoft OAuth flow
- Access dashboard at http://localhost:3000/dashboard
- Monitor email processing:
- View configured accounts
- Check processing status
- Access downloaded PDFs
- Review email metadata
- Manual: Click "Check New Emails" in dashboard
- Per Account: Use "Check Inbox" button for specific accounts
- Automated: Set up cron job calling the API endpoint
POST /api/fetch-emails: Check for new emailsGET /api/email-metadata: Get processed email listGET /api/pdfs: List downloaded PDFsGET /api/pdfs/[id]/download: Download specific PDF
# Start development server
npm run dev
# Type checking
npm run type-check
# Linting
npm run lint
# Format code
npm run format
# Build application
npm run build
# Start production server
npm start
# Update database schema
npx prisma db push
# Reset database
npx prisma db reset
# Generate Prisma client
npx prisma generate- Verify credentials in
.env - Confirm redirect URIs match exactly
- Check required API scopes are enabled
- Verify IMAP settings for traditional email
- Confirm OAuth tokens are valid
- Check email account security settings
- Verify PDF_STORAGE_PATH is writable
- Check file permissions
- Confirm PDF attachment MIME types
- Verify PostgreSQL is running
- Check DATABASE_URL format
- Ensure database exists
- Set up production database
- Configure production environment variables
- Build application:
npm run build - Start server:
npm start
For detailed deployment guides, see our deployment documentation.