mail page rest
This commit is contained in:
parent
9a1ebf7bda
commit
6e7c9013a5
99
app/api/mail/[id]/route.ts
Normal file
99
app/api/mail/[id]/route.ts
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import { ImapFlow } from 'imapflow';
|
||||||
|
import { getServerSession } from 'next-auth';
|
||||||
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||||
|
import { prisma } from '@/lib/prisma';
|
||||||
|
|
||||||
|
export async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||||
|
try {
|
||||||
|
const session = await getServerSession(authOptions);
|
||||||
|
if (!session?.user?.id) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Unauthorized' },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get credentials from database
|
||||||
|
const credentials = await prisma.mailCredentials.findUnique({
|
||||||
|
where: {
|
||||||
|
userId: session.user.id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!credentials) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'No mail credentials found. Please configure your email account.' },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the current folder from the request URL
|
||||||
|
const url = new URL(request.url);
|
||||||
|
const folder = url.searchParams.get('folder') || 'INBOX';
|
||||||
|
|
||||||
|
// Connect to IMAP server
|
||||||
|
const client = new ImapFlow({
|
||||||
|
host: credentials.host,
|
||||||
|
port: credentials.port,
|
||||||
|
secure: true,
|
||||||
|
auth: {
|
||||||
|
user: credentials.email,
|
||||||
|
pass: credentials.password,
|
||||||
|
},
|
||||||
|
logger: false,
|
||||||
|
emitLogs: false,
|
||||||
|
tls: {
|
||||||
|
rejectUnauthorized: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await client.connect();
|
||||||
|
await client.mailboxOpen(folder);
|
||||||
|
|
||||||
|
// Fetch the full email content
|
||||||
|
const message = await client.fetchOne(params.id, {
|
||||||
|
source: true,
|
||||||
|
envelope: true,
|
||||||
|
flags: true
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!message) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Email not found' },
|
||||||
|
{ status: 404 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract email content
|
||||||
|
const result = {
|
||||||
|
id: message.uid.toString(),
|
||||||
|
from: message.envelope.from[0].address,
|
||||||
|
subject: message.envelope.subject || '(No subject)',
|
||||||
|
date: message.envelope.date.toISOString(),
|
||||||
|
read: message.flags.has('\\Seen'),
|
||||||
|
starred: message.flags.has('\\Flagged'),
|
||||||
|
folder: folder,
|
||||||
|
body: message.source.toString(),
|
||||||
|
to: message.envelope.to?.map(addr => addr.address).join(', ') || '',
|
||||||
|
cc: message.envelope.cc?.map(addr => addr.address).join(', ') || '',
|
||||||
|
bcc: message.envelope.bcc?.map(addr => addr.address).join(', ') || '',
|
||||||
|
};
|
||||||
|
|
||||||
|
return NextResponse.json(result);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
await client.logout();
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error during logout:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching email:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'An unexpected error occurred' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -40,7 +40,7 @@ interface Email {
|
|||||||
id: number;
|
id: number;
|
||||||
accountId: number;
|
accountId: number;
|
||||||
from: string;
|
from: string;
|
||||||
fromName?: string;
|
fromName: string;
|
||||||
to: string;
|
to: string;
|
||||||
subject: string;
|
subject: string;
|
||||||
body: string;
|
body: string;
|
||||||
@ -484,16 +484,21 @@ function renderEmailContent(email: Email) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// If parsing failed, try direct content extraction
|
// If parsing failed, try direct content extraction
|
||||||
let content = null;
|
let content = '';
|
||||||
|
let isHtml = false;
|
||||||
|
|
||||||
if (parsed.html) {
|
if (parsed.html) {
|
||||||
content = parsed.html;
|
content = parsed.html;
|
||||||
|
isHtml = true;
|
||||||
} else if (parsed.text) {
|
} else if (parsed.text) {
|
||||||
content = parsed.text;
|
content = parsed.text;
|
||||||
|
isHtml = false;
|
||||||
} else {
|
} else {
|
||||||
// Try to extract content directly from body
|
// Try to extract content directly from body
|
||||||
const htmlMatch = email.body.match(/<html[^>]*>[\s\S]*?<\/html>/i);
|
const htmlMatch = email.body.match(/<html[^>]*>[\s\S]*?<\/html>/i);
|
||||||
if (htmlMatch) {
|
if (htmlMatch) {
|
||||||
content = htmlMatch[0];
|
content = htmlMatch[0];
|
||||||
|
isHtml = true;
|
||||||
} else {
|
} else {
|
||||||
content = email.body
|
content = email.body
|
||||||
.replace(/<[^>]+>/g, '')
|
.replace(/<[^>]+>/g, '')
|
||||||
@ -507,6 +512,7 @@ function renderEmailContent(email: Email) {
|
|||||||
.replace(/=3D/g, '=')
|
.replace(/=3D/g, '=')
|
||||||
.replace(/=09/g, '\t')
|
.replace(/=09/g, '\t')
|
||||||
.trim();
|
.trim();
|
||||||
|
isHtml = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -527,7 +533,11 @@ function renderEmailContent(email: Email) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="prose max-w-none">
|
<div className="prose max-w-none">
|
||||||
|
{isHtml ? (
|
||||||
<div dangerouslySetInnerHTML={{ __html: content }} />
|
<div dangerouslySetInnerHTML={{ __html: content }} />
|
||||||
|
) : (
|
||||||
|
<div className="whitespace-pre-wrap">{content}</div>
|
||||||
|
)}
|
||||||
{attachmentElements}
|
{attachmentElements}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@ -755,25 +765,47 @@ export default function MailPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Update handleEmailSelect to set selectedEmail correctly
|
// Update handleEmailSelect to set selectedEmail correctly
|
||||||
const handleEmailSelect = (emailId: number) => {
|
const handleEmailSelect = async (emailId: number) => {
|
||||||
const email = emails.find(e => e.id === emailId);
|
try {
|
||||||
if (email) {
|
// Mark email as read
|
||||||
setSelectedEmail(email);
|
const markReadResponse = await fetch(`/api/mail/mark-read`, {
|
||||||
if (!email.read) {
|
|
||||||
// Mark as read in state
|
|
||||||
setEmails(emails.map(e =>
|
|
||||||
e.id === emailId ? { ...e, read: true } : e
|
|
||||||
));
|
|
||||||
|
|
||||||
// Update read status on server
|
|
||||||
fetch('/api/mail/mark-read', {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: {
|
||||||
body: JSON.stringify({ emailId })
|
'Content-Type': 'application/json',
|
||||||
}).catch(error => {
|
},
|
||||||
console.error('Error marking email as read:', error);
|
body: JSON.stringify({
|
||||||
|
emailId,
|
||||||
|
isRead: true,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!markReadResponse.ok) {
|
||||||
|
console.error('Failed to mark email as read');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch full email content
|
||||||
|
const response = await fetch(`/api/mail/${emailId}?folder=${currentView}`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch email content');
|
||||||
|
}
|
||||||
|
|
||||||
|
const fullEmail = await response.json();
|
||||||
|
|
||||||
|
// Update the email in the list with full content
|
||||||
|
setEmails(prevEmails =>
|
||||||
|
prevEmails.map(email =>
|
||||||
|
email.id === emailId
|
||||||
|
? { ...email, ...fullEmail, read: true }
|
||||||
|
: email
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update selected email
|
||||||
|
setSelectedEmail(fullEmail);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error selecting email:', error);
|
||||||
|
setError('Failed to load email content');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1290,10 +1322,12 @@ export default function MailPage() {
|
|||||||
preview += '...';
|
preview += '...';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Final preview:', preview);
|
||||||
return preview;
|
return preview;
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error generating preview:', e);
|
console.error('Error generating preview:', e);
|
||||||
return '(No preview available)';
|
return 'No preview available';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user