diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index a744df28..4dd75fe3 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -680,6 +680,228 @@ export default function CourrierPage() { {/* Display all accounts */}
+ {/* Form for adding a new account */} + {showAddAccountForm && ( +
+

Add IMAP Account

+
{ + e.preventDefault(); + setLoading(true); + + const formData = new FormData(e.currentTarget); + + // Pull values from form with proper type handling + const formValues = { + email: formData.get('email')?.toString() || '', + password: formData.get('password')?.toString() || '', + host: formData.get('host')?.toString() || '', + port: parseInt(formData.get('port')?.toString() || '993'), + secure: formData.get('secure') === 'on', + display_name: formData.get('display_name')?.toString() || '', + smtp_host: formData.get('smtp_host')?.toString() || '', + smtp_port: formData.get('smtp_port')?.toString() ? + parseInt(formData.get('smtp_port')?.toString() || '587') : undefined, + smtp_secure: formData.get('smtp_secure') === 'on' + }; + + // If display_name is empty, use email + if (!formValues.display_name) { + formValues.display_name = formValues.email; + } + + try { + // First test the connection + const testResponse = await fetch('/api/courrier/test-connection', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + email: formValues.email, + password: formValues.password, + host: formValues.host, + port: formValues.port, + secure: formValues.secure + }) + }); + + const testResult = await testResponse.json(); + + if (!testResponse.ok) { + throw new Error(testResult.error || 'Connection test failed'); + } + + console.log('Connection test successful:', testResult); + + // If connection test is successful, save the account + const saveResponse = await fetch('/api/courrier/account', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(formValues) + }); + + const saveResult = await saveResponse.json(); + + if (!saveResponse.ok) { + throw new Error(saveResult.error || 'Failed to add account'); + } + + // Update accounts list + const newAccountObj = { + id: `account-${Date.now()}`, // generate unique string ID + name: formValues.display_name, + email: formValues.email, + color: `bg-blue-500`, // Default color class + folders: testResult.details.sampleFolders || ['INBOX', 'Sent', 'Drafts', 'Trash'] // Use discovered folders or defaults + }; + + setAccounts(prev => [...prev, newAccountObj]); + setShowAddAccountForm(false); + toast({ + title: "Account added successfully", + description: `Your email account ${formValues.email} has been added.`, + duration: 5000 + }); + } catch (error) { + console.error('Error adding account:', error); + toast({ + title: "Failed to add account", + description: error instanceof Error ? error.message : 'Unknown error', + variant: "destructive", + duration: 5000 + }); + } finally { + setLoading(false); + } + }}> +
+ + + IMAP Settings + SMTP Settings + + + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+
+ + +
+ + +
+
+
+ + +
+
+
+ + +
+
+
+
+ Note: SMTP settings are only needed for sending emails +
+
+
+ +
+ + +
+
+
+
+ )} + {accounts.map((account) => (