mail page imap connection mime 5 bis rest 16 login page 2

This commit is contained in:
alma 2025-04-15 23:32:22 +02:00
parent 5b2eb5286a
commit fae693d06e
2 changed files with 39 additions and 25 deletions

View File

@ -4,27 +4,34 @@ import Imap from 'imap';
export async function POST(request: Request) { export async function POST(request: Request) {
try { try {
const { email, password, host, port } = await request.json(); const { email, password, host, port } = await request.json();
// Ensure port is a valid number
const portNumber = parseInt(port, 10);
if (isNaN(portNumber) || portNumber < 0 || portNumber > 65535) {
throw new Error('Invalid port number');
}
// IMAP configuration const imapConfig = {
const imapConfig: Imap.Config = {
user: email, user: email,
password: password, password: password,
host: host, host: host,
port: parseInt(port, 10), port: portNumber,
tls: true, tls: true,
tlsOptions: {
rejectUnauthorized: false,
servername: host
},
authTimeout: 10000, authTimeout: 10000,
connTimeout: 10000 connTimeout: 10000,
debug: console.log
}; };
// Create a promise-based IMAP connection console.log('Attempting IMAP connection with config:', {
...imapConfig,
password: '[REDACTED]'
});
const imap = new Imap(imapConfig); const imap = new Imap(imapConfig);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
imap.once('ready', () => { imap.once('ready', () => {
console.log('IMAP connection successful');
imap.end(); imap.end();
resolve(NextResponse.json({ success: true })); resolve(NextResponse.json({ success: true }));
}); });
@ -32,19 +39,18 @@ export async function POST(request: Request) {
imap.once('error', (err: Error) => { imap.once('error', (err: Error) => {
console.error('IMAP connection error:', err); console.error('IMAP connection error:', err);
reject(NextResponse.json({ reject(NextResponse.json({
error: 'Failed to connect to email server', error: 'IMAP connection failed',
details: err.message details: err.message
}, { status: 401 })); }, { status: 500 }));
}); });
imap.connect(); imap.connect();
}); });
} catch (error) { } catch (error) {
console.error('Error in test connection:', error); console.error('Error in test-connection:', error);
return NextResponse.json({ return NextResponse.json({
error: 'Failed to test connection', error: 'Failed to test connection',
details: error instanceof Error ? error.message : 'Unknown error' details: error instanceof Error ? error.message : 'Unknown error'
}, { status: 500 }); }, { status: 500 });
} }
} }

View File

@ -50,18 +50,18 @@ export default function EmailLoginPage() {
}; };
return ( return (
<div className="flex min-h-screen items-center justify-center bg-gray-50"> <div className="flex min-h-screen items-center justify-center bg-white">
<Card className="w-full max-w-md"> <Card className="w-full max-w-md shadow-lg">
<CardHeader> <CardHeader>
<CardTitle>Email Login</CardTitle> <CardTitle className="text-2xl font-bold text-gray-900">Email Login</CardTitle>
<CardDescription> <CardDescription className="text-gray-600">
Enter your email credentials to access your mailbox Enter your email credentials to access your mailbox
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<form onSubmit={handleSubmit} className="space-y-4"> <form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="email">Email Address</Label> <Label htmlFor="email" className="text-gray-700">Email Address</Label>
<Input <Input
id="email" id="email"
type="email" type="email"
@ -69,39 +69,47 @@ export default function EmailLoginPage() {
value={credentials.email} value={credentials.email}
onChange={(e) => setCredentials({ ...credentials, email: e.target.value })} onChange={(e) => setCredentials({ ...credentials, email: e.target.value })}
required required
className="border-gray-300 focus:border-blue-500 focus:ring-blue-500"
/> />
</div> </div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="password">Password</Label> <Label htmlFor="password" className="text-gray-700">Password</Label>
<Input <Input
id="password" id="password"
type="password" type="password"
value={credentials.password} value={credentials.password}
onChange={(e) => setCredentials({ ...credentials, password: e.target.value })} onChange={(e) => setCredentials({ ...credentials, password: e.target.value })}
required required
className="border-gray-300 focus:border-blue-500 focus:ring-blue-500"
/> />
</div> </div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="host">IMAP Host</Label> <Label htmlFor="host" className="text-gray-700">IMAP Host</Label>
<Input <Input
id="host" id="host"
type="text" type="text"
value={credentials.host} value={credentials.host}
onChange={(e) => setCredentials({ ...credentials, host: e.target.value })} onChange={(e) => setCredentials({ ...credentials, host: e.target.value })}
required required
className="border-gray-300 focus:border-blue-500 focus:ring-blue-500"
/> />
</div> </div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="port">IMAP Port</Label> <Label htmlFor="port" className="text-gray-700">IMAP Port</Label>
<Input <Input
id="port" id="port"
type="text" type="text"
value={credentials.port} value={credentials.port}
onChange={(e) => setCredentials({ ...credentials, port: e.target.value })} onChange={(e) => setCredentials({ ...credentials, port: e.target.value })}
required required
className="border-gray-300 focus:border-blue-500 focus:ring-blue-500"
/> />
</div> </div>
<Button type="submit" className="w-full" disabled={loading}> <Button
type="submit"
className="w-full bg-blue-600 hover:bg-blue-700 text-white"
disabled={loading}
>
{loading ? 'Connecting...' : 'Connect'} {loading ? 'Connecting...' : 'Connect'}
</Button> </Button>
</form> </form>