|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { ImapFlow } from 'imapflow' |
| 3 | +import { type NextRequest, NextResponse } from 'next/server' |
| 4 | +import { getSession } from '@/lib/auth' |
| 5 | + |
| 6 | +const logger = createLogger('ImapMailboxesAPI') |
| 7 | + |
| 8 | +interface ImapMailboxRequest { |
| 9 | + host: string |
| 10 | + port: number |
| 11 | + secure: boolean |
| 12 | + rejectUnauthorized: boolean |
| 13 | + username: string |
| 14 | + password: string |
| 15 | +} |
| 16 | + |
| 17 | +export async function POST(request: NextRequest) { |
| 18 | + const session = await getSession() |
| 19 | + if (!session?.user?.id) { |
| 20 | + return NextResponse.json({ success: false, message: 'Unauthorized' }, { status: 401 }) |
| 21 | + } |
| 22 | + |
| 23 | + try { |
| 24 | + const body = (await request.json()) as ImapMailboxRequest |
| 25 | + const { host, port, secure, rejectUnauthorized, username, password } = body |
| 26 | + |
| 27 | + if (!host || !username || !password) { |
| 28 | + return NextResponse.json( |
| 29 | + { success: false, message: 'Missing required fields: host, username, password' }, |
| 30 | + { status: 400 } |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + const client = new ImapFlow({ |
| 35 | + host, |
| 36 | + port: port || 993, |
| 37 | + secure: secure ?? true, |
| 38 | + auth: { |
| 39 | + user: username, |
| 40 | + pass: password, |
| 41 | + }, |
| 42 | + tls: { |
| 43 | + rejectUnauthorized: rejectUnauthorized ?? true, |
| 44 | + }, |
| 45 | + logger: false, |
| 46 | + }) |
| 47 | + |
| 48 | + try { |
| 49 | + await client.connect() |
| 50 | + |
| 51 | + const listResult = await client.list() |
| 52 | + const mailboxes = listResult.map((mailbox) => ({ |
| 53 | + path: mailbox.path, |
| 54 | + name: mailbox.name, |
| 55 | + delimiter: mailbox.delimiter, |
| 56 | + })) |
| 57 | + |
| 58 | + await client.logout() |
| 59 | + |
| 60 | + mailboxes.sort((a, b) => { |
| 61 | + if (a.path === 'INBOX') return -1 |
| 62 | + if (b.path === 'INBOX') return 1 |
| 63 | + return a.path.localeCompare(b.path) |
| 64 | + }) |
| 65 | + |
| 66 | + return NextResponse.json({ |
| 67 | + success: true, |
| 68 | + mailboxes, |
| 69 | + }) |
| 70 | + } catch (error) { |
| 71 | + try { |
| 72 | + await client.logout() |
| 73 | + } catch { |
| 74 | + // Ignore logout errors |
| 75 | + } |
| 76 | + throw error |
| 77 | + } |
| 78 | + } catch (error) { |
| 79 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error' |
| 80 | + logger.error('Error fetching IMAP mailboxes:', errorMessage) |
| 81 | + |
| 82 | + let userMessage = 'Failed to connect to IMAP server' |
| 83 | + if ( |
| 84 | + errorMessage.includes('AUTHENTICATIONFAILED') || |
| 85 | + errorMessage.includes('Invalid credentials') |
| 86 | + ) { |
| 87 | + userMessage = 'Invalid username or password. For Gmail, use an App Password.' |
| 88 | + } else if (errorMessage.includes('ENOTFOUND') || errorMessage.includes('getaddrinfo')) { |
| 89 | + userMessage = 'Could not find IMAP server. Please check the hostname.' |
| 90 | + } else if (errorMessage.includes('ECONNREFUSED')) { |
| 91 | + userMessage = 'Connection refused. Please check the port and SSL settings.' |
| 92 | + } else if (errorMessage.includes('certificate') || errorMessage.includes('SSL')) { |
| 93 | + userMessage = |
| 94 | + 'TLS/SSL error. Try disabling "Verify TLS Certificate" for self-signed certificates.' |
| 95 | + } else if (errorMessage.includes('timeout')) { |
| 96 | + userMessage = 'Connection timed out. Please check your network and server settings.' |
| 97 | + } |
| 98 | + |
| 99 | + return NextResponse.json({ success: false, message: userMessage }, { status: 500 }) |
| 100 | + } |
| 101 | +} |
0 commit comments