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

This commit is contained in:
alma 2025-04-16 10:14:51 +02:00
parent 368ff9bb75
commit 8876054496
2 changed files with 45 additions and 26 deletions

View File

@ -124,21 +124,25 @@ export async function GET() {
imap.once('ready', () => { imap.once('ready', () => {
imap.openBox('INBOX', false, (err, box) => { imap.openBox('INBOX', false, (err, box) => {
if (err) { if (err) {
console.error('Error opening inbox:', err);
imap.end(); imap.end();
resolve(NextResponse.json({ error: 'Failed to open inbox' }, { status: 500 })); resolve(NextResponse.json({ emails: [], error: 'Failed to open inbox' }));
return; return;
} }
const total = box.messages.total; console.log('Inbox opened, total messages:', box.messages.total);
const start = Math.max(1, total - 19); // Get last 20 emails
if (total === 0) { if (box.messages.total === 0) {
imap.end(); imap.end();
resolve(NextResponse.json({ emails: [], mailUrl: null })); resolve(NextResponse.json({
emails: [],
mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null
}));
return; return;
} }
const f = imap.seq.fetch(`${start}:${total}`, { const start = Math.max(1, box.messages.total - 19); // Get last 20 emails
const f = imap.seq.fetch(`${start}:${box.messages.total}`, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'], bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
struct: true struct: true
}); });
@ -166,7 +170,7 @@ export async function GET() {
email.from = headers.from?.[0] || ''; email.from = headers.from?.[0] || '';
email.to = headers.to?.[0] || ''; email.to = headers.to?.[0] || '';
email.subject = headers.subject?.[0] || '(No subject)'; email.subject = headers.subject?.[0] || '(No subject)';
email.date = new Date(headers.date?.[0] || Date.now()); email.date = headers.date?.[0] || new Date().toISOString();
} else { } else {
email.body = buffer; email.body = buffer;
} }
@ -187,14 +191,18 @@ export async function GET() {
f.once('error', (err) => { f.once('error', (err) => {
console.error('Fetch error:', err); console.error('Fetch error:', err);
imap.end(); imap.end();
resolve(NextResponse.json({ error: 'Failed to fetch emails' }, { status: 500 })); resolve(NextResponse.json({
emails: [],
error: 'Failed to fetch emails'
}));
}); });
f.once('end', () => { f.once('end', () => {
console.log('Fetch completed, found emails:', emails.length);
imap.end(); imap.end();
resolve(NextResponse.json({ resolve(NextResponse.json({
emails: emails.sort((a, b) => b.date.getTime() - a.date.getTime()), emails: emails.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()),
mailUrl: null mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null
})); }));
}); });
}); });
@ -202,17 +210,20 @@ export async function GET() {
imap.once('error', (err) => { imap.once('error', (err) => {
console.error('IMAP error:', err); console.error('IMAP error:', err);
resolve(NextResponse.json({ error: 'IMAP connection error' }, { status: 500 })); resolve(NextResponse.json({
emails: [],
error: 'IMAP connection error'
}));
}); });
imap.connect(); imap.connect();
}); });
} catch (error) { } catch (error) {
console.error('Error in mail API:', error); console.error('Error in mail API:', error);
return NextResponse.json( return NextResponse.json({
{ error: error instanceof Error ? error.message : 'Unknown error' }, emails: [],
{ status: 500 } error: error instanceof Error ? error.message : 'Unknown error'
); });
} }
} }

View File

@ -39,23 +39,29 @@ export function Email() {
try { try {
const response = await fetch('/api/mail'); const response = await fetch('/api/mail');
const data = await response.json();
if (!response.ok) { if (!response.ok) {
if (response.status === 401) { if (response.status === 401) {
signIn(); signIn();
return; return;
} }
throw new Error(data.error || 'Failed to fetch emails');
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to fetch emails');
} }
const data = await response.json(); // Transform the email data to match the expected format
if (!Array.isArray(data.emails)) { const transformedEmails = (data.emails || []).map((email: any) => ({
throw new Error('Invalid response format'); id: email.id,
} subject: email.subject,
sender: {
name: email.from.split('<')[0].trim().replace(/"/g, '') || email.from,
email: (email.from.match(/<(.+)>/) || [])[1] || email.from
},
date: email.date,
isUnread: !email.read
}));
setEmails(data.emails || []); setEmails(transformedEmails);
setMailUrl(data.mailUrl); setMailUrl(data.mailUrl);
setError(null); setError(null);
} catch (err) { } catch (err) {
@ -143,7 +149,9 @@ export function Email() {
) : ( ) : (
<div className="space-y-2 max-h-[220px] overflow-y-auto"> <div className="space-y-2 max-h-[220px] overflow-y-auto">
{emails.length === 0 ? ( {emails.length === 0 ? (
<p className="text-center text-gray-500">Aucun email non lu</p> <p className="text-center text-gray-500">
{loading ? 'Loading emails...' : 'No unread emails'}
</p>
) : ( ) : (
emails.map((email) => ( emails.map((email) => (
<div <div
@ -156,7 +164,7 @@ export function Email() {
{email.sender.name} {email.sender.name}
</span> </span>
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<span className="w-1.5 h-1.5 bg-blue-600 rounded-full"></span> {email.isUnread && <span className="w-1.5 h-1.5 bg-blue-600 rounded-full"></span>}
<span className="text-xs text-gray-500">{formatDate(email.date)}</span> <span className="text-xs text-gray-500">{formatDate(email.date)}</span>
</div> </div>
</div> </div>