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

This commit is contained in:
alma 2025-04-16 10:17:26 +02:00
parent 8876054496
commit bdf2743bc7
2 changed files with 31 additions and 15 deletions

View File

@ -198,12 +198,14 @@ export async function GET() {
}); });
f.once('end', () => { f.once('end', () => {
console.log('Fetch completed, found emails:', emails.length); console.log('Emails before sending:', emails);
imap.end(); const response = {
resolve(NextResponse.json({ emails: emails,
emails: emails.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()),
mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null
})); };
console.log('Final response:', response);
imap.end();
resolve(NextResponse.json(response));
}); });
}); });
}); });

View File

@ -41,6 +41,11 @@ export function Email() {
const response = await fetch('/api/mail'); const response = await fetch('/api/mail');
const data = await response.json(); const data = await response.json();
// Add debug logs
console.log('Raw API response:', data);
console.log('Type of data:', typeof data);
console.log('Type of data.emails:', data.emails ? typeof data.emails : 'undefined');
if (!response.ok) { if (!response.ok) {
if (response.status === 401) { if (response.status === 401) {
signIn(); signIn();
@ -49,17 +54,26 @@ export function Email() {
throw new Error(data.error || 'Failed to fetch emails'); throw new Error(data.error || 'Failed to fetch emails');
} }
// Ensure data.emails is an array
const emailsArray = Array.isArray(data.emails) ? data.emails : [];
console.log('Emails array:', emailsArray);
// Transform the email data to match the expected format // Transform the email data to match the expected format
const transformedEmails = (data.emails || []).map((email: any) => ({ const transformedEmails = emailsArray.map((email: any) => {
id: email.id, console.log('Processing email:', email);
subject: email.subject, return {
sender: { id: email.id || String(Math.random()), // Fallback ID if none exists
name: email.from.split('<')[0].trim().replace(/"/g, '') || email.from, subject: email.subject || '(No subject)',
email: (email.from.match(/<(.+)>/) || [])[1] || email.from sender: {
}, name: email.from ? (email.from.split('<')[0].trim().replace(/"/g, '') || email.from) : 'Unknown',
date: email.date, email: email.from ? ((email.from.match(/<(.+)>/) || [])[1] || email.from) : 'unknown@example.com'
isUnread: !email.read },
})); date: email.date || new Date().toISOString(),
isUnread: !email.read
};
});
console.log('Transformed emails:', transformedEmails);
setEmails(transformedEmails); setEmails(transformedEmails);
setMailUrl(data.mailUrl); setMailUrl(data.mailUrl);