mime change
This commit is contained in:
parent
338f23104e
commit
126f243266
81
app/api/courrier/send/route.ts
Normal file
81
app/api/courrier/send/route.ts
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import { getServerSession } from 'next-auth';
|
||||||
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||||
|
import { prisma } from '@/lib/prisma';
|
||||||
|
import nodemailer from 'nodemailer';
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
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 email data from the request
|
||||||
|
const { to, cc, bcc, subject, body, attachments } = await request.json();
|
||||||
|
|
||||||
|
if (!to) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Recipient is required' },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create SMTP transporter
|
||||||
|
const transporter = nodemailer.createTransport({
|
||||||
|
host: credentials.host,
|
||||||
|
port: credentials.port,
|
||||||
|
secure: true,
|
||||||
|
auth: {
|
||||||
|
user: credentials.email,
|
||||||
|
pass: credentials.password,
|
||||||
|
},
|
||||||
|
tls: {
|
||||||
|
rejectUnauthorized: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Prepare email options
|
||||||
|
const mailOptions = {
|
||||||
|
from: credentials.email,
|
||||||
|
to: to,
|
||||||
|
cc: cc || undefined,
|
||||||
|
bcc: bcc || undefined,
|
||||||
|
subject: subject || '(No subject)',
|
||||||
|
html: body,
|
||||||
|
attachments: attachments?.map((file: any) => ({
|
||||||
|
filename: file.name,
|
||||||
|
content: file.content,
|
||||||
|
contentType: file.type
|
||||||
|
})) || []
|
||||||
|
};
|
||||||
|
|
||||||
|
// Send the email
|
||||||
|
await transporter.sendMail(mailOptions);
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error sending email:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Failed to send email' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
lib/imap.ts
21
lib/imap.ts
@ -19,7 +19,7 @@ export async function getImapClient() {
|
|||||||
throw new Error('No mail credentials found. Please configure your email account.');
|
throw new Error('No mail credentials found. Please configure your email account.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new ImapFlow({
|
return new ImapFlow({
|
||||||
host: credentials.host,
|
host: credentials.host,
|
||||||
port: credentials.port,
|
port: credentials.port,
|
||||||
secure: true,
|
secure: true,
|
||||||
@ -32,9 +32,6 @@ export async function getImapClient() {
|
|||||||
rejectUnauthorized: false
|
rejectUnauthorized: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await client.connect();
|
|
||||||
return client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function moveEmails(emailIds: number[], targetFolder: string) {
|
export async function moveEmails(emailIds: number[], targetFolder: string) {
|
||||||
@ -44,7 +41,8 @@ export async function moveEmails(emailIds: number[], targetFolder: string) {
|
|||||||
for (const id of emailIds) {
|
for (const id of emailIds) {
|
||||||
const message = await imap.fetchOne(id.toString(), { uid: true });
|
const message = await imap.fetchOne(id.toString(), { uid: true });
|
||||||
if (message) {
|
if (message) {
|
||||||
await imap.messageMove(message.uid, targetFolder);
|
const uid = typeof message.uid === 'number' ? message.uid.toString() : message.uid;
|
||||||
|
await imap.messageMove(uid, targetFolder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -52,17 +50,24 @@ export async function moveEmails(emailIds: number[], targetFolder: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function markAsRead(emailIds: number[], isRead: boolean) {
|
export async function markAsRead(emailIds: (string | number)[], isRead: boolean) {
|
||||||
const imap = await getImapClient();
|
const imap = await getImapClient();
|
||||||
|
try {
|
||||||
|
await imap.connect();
|
||||||
const lock = await imap.getMailboxLock('INBOX');
|
const lock = await imap.getMailboxLock('INBOX');
|
||||||
try {
|
try {
|
||||||
for (const id of emailIds) {
|
for (const id of emailIds) {
|
||||||
const message = await imap.fetchOne(id.toString(), { uid: true });
|
const stringId = typeof id === 'number' ? id.toString() : id;
|
||||||
|
const message = await imap.fetchOne(stringId, { uid: true });
|
||||||
if (message) {
|
if (message) {
|
||||||
await imap.messageFlagsAdd(message.uid, isRead ? ['\\Seen'] : [], { uid: true });
|
const uid = typeof message.uid === 'number' ? message.uid.toString() : message.uid;
|
||||||
|
await imap.messageFlagsAdd(uid, isRead ? ['\\Seen'] : [], { uid: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.release();
|
lock.release();
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
await imap.logout();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user