Added Save in Sent for emailAsUser.ts

This commit is contained in:
2025-10-27 17:37:51 +01:00
parent f918e735a5
commit efaebb2f4e

View File

@@ -4,6 +4,11 @@ import { FastifyInstance } from "fastify";
import {sendMailAsUser} from "../utils/emailengine"; import {sendMailAsUser} from "../utils/emailengine";
import {encrypt, decrypt} from "../utils/crypt" import {encrypt, decrypt} from "../utils/crypt"
import {secrets} from "../utils/secrets"; import {secrets} from "../utils/secrets";
// @ts-ignore
import MailComposer from 'nodemailer/lib/mail-composer/index.js'
import {ImapFlow} from "imapflow"
export default async function emailAsUserRoutes(server: FastifyInstance) { export default async function emailAsUserRoutes(server: FastifyInstance) {
// Create E-Mail Account // Create E-Mail Account
@@ -164,11 +169,14 @@ export default async function emailAsUserRoutes(server: FastifyInstance) {
smtp_host: string smtp_host: string
smtp_port: number smtp_port: number
smtp_ssl: boolean smtp_ssl: boolean
imap_host: string
imap_port: number
imap_ssl: boolean
} }
// @ts-ignore // @ts-ignore
const { data, error } = await server.supabase const { data, error } = await server.supabase
.from("user_credentials") .from("user_credentials")
.select("id, email_encrypted,password_encrypted, smtp_host_encrypted, smtp_port, smtp_ssl, user_id, tenant_id") .select("id, email_encrypted,password_encrypted, smtp_host_encrypted, smtp_port, smtp_ssl,imap_host_encrypted,imap_port, imap_ssl, user_id, tenant_id")
.eq("id", body.account) .eq("id", body.account)
.eq("tenant_id", req.user.tenant_id) .eq("tenant_id", req.user.tenant_id)
.eq("type", "mail") .eq("type", "mail")
@@ -197,7 +205,7 @@ export default async function emailAsUserRoutes(server: FastifyInstance) {
}, },
}) })
const info = await transporter.sendMail({ const message = {
from: accountData.email, from: accountData.email,
to: body.to, to: body.to,
cc: body.cc ? body.cc : undefined, cc: body.cc ? body.cc : undefined,
@@ -206,8 +214,43 @@ export default async function emailAsUserRoutes(server: FastifyInstance) {
html: body.html ? body.html : undefined, html: body.html ? body.html : undefined,
text: body.text, text: body.text,
attachments: body.attachments ? body.attachments : undefined, attachments: body.attachments ? body.attachments : undefined,
}
const info = await transporter.sendMail(message)
const imapClient = new ImapFlow({
host: accountData.imap_host,
port: accountData.imap_port,
secure: accountData.imap_ssl,
auth: {
user: accountData.email,
pass: accountData.password,
},
logger: false
}) })
await imapClient.connect()
const mail = new MailComposer(message)
const raw = await mail.compile().build() // → Buffer mit kompletter MIME
for await (const mailbox of await imapClient.list()) {
// mailbox.flags enthält z. B. ['\\Sent', '\\HasChildren']
console.log(mailbox.specialUse)
if (mailbox.specialUse == '\\Sent') {
console.log('📨 Sent folder gefunden:', mailbox.path)
await imapClient.mailboxOpen(mailbox.path)
await imapClient.append(mailbox.path, raw, ['\\Seen'])
await imapClient.logout()
break
}
}
if(info.response.includes("OK")){ if(info.response.includes("OK")){
reply.send({success: true}) reply.send({success: true})
@@ -217,6 +260,7 @@ export default async function emailAsUserRoutes(server: FastifyInstance) {
} catch (err) { } catch (err) {
console.log(err)
reply.code(500).send({ error: "Failed to send E-Mail as User" }) reply.code(500).send({ error: "Failed to send E-Mail as User" })
} }
}) })