mail page imap connection mime 5 bis rest 16 login page 23
This commit is contained in:
parent
450121ff23
commit
368ff9bb75
@ -107,164 +107,111 @@ export async function GET() {
|
|||||||
password: '***'
|
password: '***'
|
||||||
});
|
});
|
||||||
|
|
||||||
const imapConfig: ImapConfig = {
|
const imap = new Imap({
|
||||||
user: credentials.email,
|
user: credentials.email,
|
||||||
password: credentials.password,
|
password: credentials.password,
|
||||||
host: credentials.host,
|
host: credentials.host,
|
||||||
port: credentials.port,
|
port: credentials.port,
|
||||||
tls: true,
|
tls: true,
|
||||||
authTimeout: 10000,
|
tlsOptions: { rejectUnauthorized: false },
|
||||||
connTimeout: 10000,
|
authTimeout: 30000,
|
||||||
debug: (info: string) => console.log('IMAP Debug:', info)
|
connTimeout: 30000
|
||||||
};
|
});
|
||||||
|
|
||||||
console.log('Connecting to IMAP server...');
|
return new Promise((resolve) => {
|
||||||
const imap = new Imap(imapConfig);
|
const emails: Email[] = [];
|
||||||
|
|
||||||
const connectPromise = new Promise((resolve, reject) => {
|
|
||||||
imap.once('ready', () => {
|
imap.once('ready', () => {
|
||||||
console.log('IMAP connection ready');
|
imap.openBox('INBOX', false, (err, box) => {
|
||||||
resolve(true);
|
if (err) {
|
||||||
|
imap.end();
|
||||||
|
resolve(NextResponse.json({ error: 'Failed to open inbox' }, { status: 500 }));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const total = box.messages.total;
|
||||||
|
const start = Math.max(1, total - 19); // Get last 20 emails
|
||||||
|
|
||||||
|
if (total === 0) {
|
||||||
|
imap.end();
|
||||||
|
resolve(NextResponse.json({ emails: [], mailUrl: null }));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const f = imap.seq.fetch(`${start}:${total}`, {
|
||||||
|
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
|
||||||
|
struct: true
|
||||||
|
});
|
||||||
|
|
||||||
|
f.on('message', (msg) => {
|
||||||
|
const email: any = {
|
||||||
|
id: '',
|
||||||
|
from: '',
|
||||||
|
subject: '',
|
||||||
|
date: new Date(),
|
||||||
|
read: true,
|
||||||
|
starred: false,
|
||||||
|
body: '',
|
||||||
|
to: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
msg.on('body', (stream, info) => {
|
||||||
|
let buffer = '';
|
||||||
|
stream.on('data', (chunk) => {
|
||||||
|
buffer += chunk.toString('utf8');
|
||||||
|
});
|
||||||
|
stream.on('end', () => {
|
||||||
|
if (info.which === 'HEADER.FIELDS (FROM TO SUBJECT DATE)') {
|
||||||
|
const headers = Imap.parseHeader(buffer);
|
||||||
|
email.from = headers.from?.[0] || '';
|
||||||
|
email.to = headers.to?.[0] || '';
|
||||||
|
email.subject = headers.subject?.[0] || '(No subject)';
|
||||||
|
email.date = new Date(headers.date?.[0] || Date.now());
|
||||||
|
} else {
|
||||||
|
email.body = buffer;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
msg.once('attributes', (attrs) => {
|
||||||
|
email.id = attrs.uid;
|
||||||
|
email.read = attrs.flags?.includes('\\Seen') || false;
|
||||||
|
email.starred = attrs.flags?.includes('\\Flagged') || false;
|
||||||
|
});
|
||||||
|
|
||||||
|
msg.once('end', () => {
|
||||||
|
emails.push(email);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
f.once('error', (err) => {
|
||||||
|
console.error('Fetch error:', err);
|
||||||
|
imap.end();
|
||||||
|
resolve(NextResponse.json({ error: 'Failed to fetch emails' }, { status: 500 }));
|
||||||
|
});
|
||||||
|
|
||||||
|
f.once('end', () => {
|
||||||
|
imap.end();
|
||||||
|
resolve(NextResponse.json({
|
||||||
|
emails: emails.sort((a, b) => b.date.getTime() - a.date.getTime()),
|
||||||
|
mailUrl: null
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
imap.once('error', (err: Error) => {
|
|
||||||
console.error('IMAP connection error:', err);
|
imap.once('error', (err) => {
|
||||||
reject(err);
|
console.error('IMAP error:', err);
|
||||||
|
resolve(NextResponse.json({ error: 'IMAP connection error' }, { status: 500 }));
|
||||||
});
|
});
|
||||||
|
|
||||||
imap.connect();
|
imap.connect();
|
||||||
});
|
});
|
||||||
|
|
||||||
await connectPromise;
|
|
||||||
|
|
||||||
console.log('Opening INBOX...');
|
|
||||||
const openBoxPromise = new Promise<ImapBox>((resolve, reject) => {
|
|
||||||
imap.openBox('INBOX', false, (err: Error | null, box: ImapBox) => {
|
|
||||||
if (err) {
|
|
||||||
console.error('Error opening INBOX:', err);
|
|
||||||
reject(err);
|
|
||||||
} else {
|
|
||||||
console.log('INBOX opened successfully');
|
|
||||||
resolve(box);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const box = await openBoxPromise;
|
|
||||||
console.log('Total messages in INBOX:', box.messages.total);
|
|
||||||
|
|
||||||
const fetchPromise = (imap: Imap, seqno: number): Promise<ImapMessage> => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const f = imap.fetch(seqno, {
|
|
||||||
bodies: ['HEADER', 'TEXT'],
|
|
||||||
struct: true,
|
|
||||||
markSeen: false
|
|
||||||
});
|
|
||||||
|
|
||||||
f.on('message', (msg: ImapMessage) => {
|
|
||||||
resolve(msg);
|
|
||||||
});
|
|
||||||
|
|
||||||
f.once('error', (err: Error) => {
|
|
||||||
console.error('Fetch error:', err);
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const processMessage = (msg: ImapMessage): Promise<any> => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let headerContent = '';
|
|
||||||
let bodyContent = '';
|
|
||||||
let headersParsed = false;
|
|
||||||
let bodyParsed = false;
|
|
||||||
|
|
||||||
// Process headers
|
|
||||||
msg.body['HEADER']?.on('data', (chunk: Buffer) => {
|
|
||||||
headerContent += chunk.toString('utf8');
|
|
||||||
});
|
|
||||||
|
|
||||||
// Process body
|
|
||||||
msg.body['TEXT']?.on('data', (chunk: Buffer) => {
|
|
||||||
bodyContent += chunk.toString('utf8');
|
|
||||||
});
|
|
||||||
|
|
||||||
const tryResolve = () => {
|
|
||||||
if (headersParsed && bodyParsed) {
|
|
||||||
try {
|
|
||||||
const headers = parseEmailHeaders(headerContent);
|
|
||||||
const contentType = headerContent.match(/Content-Type:\s*([^;\r\n]+)/i)?.[1] || 'text/plain';
|
|
||||||
const decodedBody = decodeEmailBody(bodyContent, contentType);
|
|
||||||
|
|
||||||
resolve({
|
|
||||||
uid: msg.attributes.uid,
|
|
||||||
flags: msg.attributes.flags,
|
|
||||||
size: msg.attributes.size,
|
|
||||||
body: decodedBody,
|
|
||||||
...headers
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error processing message:', error);
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
msg.body['HEADER']?.on('end', () => {
|
|
||||||
headersParsed = true;
|
|
||||||
tryResolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
msg.body['TEXT']?.on('end', () => {
|
|
||||||
bodyParsed = true;
|
|
||||||
tryResolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
msg.body['HEADER']?.on('error', reject);
|
|
||||||
msg.body['TEXT']?.on('error', reject);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const messages: any[] = [];
|
|
||||||
// Fetch the most recent 20 emails
|
|
||||||
const start = Math.max(1, box.messages.total - 19); // last 20 emails
|
|
||||||
const end = box.messages.total;
|
|
||||||
for (let seqno = start; seqno <= end; seqno++) {
|
|
||||||
const msg = await fetchPromise(imap, seqno);
|
|
||||||
const processedMsg = await processMessage(msg);
|
|
||||||
messages.push(processedMsg);
|
|
||||||
}
|
|
||||||
imap.end();
|
|
||||||
|
|
||||||
console.log('Raw messages:', messages);
|
|
||||||
|
|
||||||
const emails: Email[] = messages.map((msg) => {
|
|
||||||
return {
|
|
||||||
id: msg.uid.toString(),
|
|
||||||
from: msg.from,
|
|
||||||
subject: msg.subject || '(No subject)',
|
|
||||||
date: new Date(msg.date),
|
|
||||||
read: !msg.flags?.includes('\\Unseen'),
|
|
||||||
starred: msg.flags?.includes('\\Flagged') || false,
|
|
||||||
body: msg.body || '',
|
|
||||||
to: msg.to
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
emails,
|
|
||||||
mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null
|
|
||||||
}, {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching emails:', error);
|
console.error('Error in mail API:', error);
|
||||||
const status = error instanceof Error && error.message.includes('Invalid login')
|
|
||||||
? 401
|
|
||||||
: 500;
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: error instanceof Error ? error.message : 'Failed to fetch emails' },
|
{ error: error instanceof Error ? error.message : 'Unknown error' },
|
||||||
{ status }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,23 +38,21 @@ export function Email() {
|
|||||||
if (!isRefresh) setLoading(true);
|
if (!isRefresh) setLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/emails');
|
const response = await fetch('/api/mail');
|
||||||
const data: EmailResponse = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// Handle session expiration
|
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
signIn(); // Redirect to login
|
signIn();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle specific error messages
|
const errorData = await response.json();
|
||||||
if (response.status === 404) {
|
throw new Error(errorData.error || 'Failed to fetch emails');
|
||||||
setError("L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur.");
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(data.error || 'Failed to fetch emails');
|
const data = await response.json();
|
||||||
|
if (!Array.isArray(data.emails)) {
|
||||||
|
throw new Error('Invalid response format');
|
||||||
}
|
}
|
||||||
|
|
||||||
setEmails(data.emails || []);
|
setEmails(data.emails || []);
|
||||||
@ -62,7 +60,7 @@ export function Email() {
|
|||||||
setError(null);
|
setError(null);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching emails:', err);
|
console.error('Error fetching emails:', err);
|
||||||
setError(err instanceof Error ? err.message : 'Erreur lors de la récupération des emails');
|
setError(err instanceof Error ? err.message : 'Error fetching emails');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRefreshing(false);
|
setRefreshing(false);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user