import Inject from "@entity-access/entity-access/dist/di/di.js"; import { Prepare } from "@entity-access/server-pages/dist/decorators/Prepare.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import SocialMailContext from "../../../../../../server/model/SocialMailContext.js"; import EmailAddressService from "../../../../../../server/services/EmailAddressService.js"; import EmailLogService from "../../../../../../server/services/email-logs/EmailLogService.js"; import EmailParserService from "../../../../../../server/smtp/services/EmailParserService.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; @Prepare.authorize @Prepare.parseForm export default class extends Page { @Inject db: SocialMailContext; @Inject eas: EmailAddressService; @Inject els: EmailLogService; @Inject eps: EmailParserService; async run() { this.sessionUser.ensureLoggedIn(); let from = this.form.fields.from; if (from.includes("<")) { const tokens = /[^\<]*\<(?[^\>]+)\>/.exec(from); if (tokens?.groups) { const { id } = tokens.groups; from = id; } } const recipients = (this.form.fields.recipients as string).split(","); const priority = Number(this.form.fields.priority || "0"); // const priority = this.form.fields.priority; // create mailbox if doesn't exist only if this is the admin // or user has ability to create mailbox const file = this.form.files[0]; const sender = await this.eas.getMailboxByAddress(from); if (!sender) { throw new EntityAccessError(`No mailbox found for ${from}`); } const parsed = await this.eps.parse(file, true); const headers = {}; const copyHeaders = new Set([ "List-Unsubscribe", "List-Unsubscribe-Post", "Return-Path", "In-Reply-To", "Reply-To", "Auto-Unsubscribe-On-Fail" ].map((x) => x.toLowerCase())); let doNotSendDR = false; let hasUnsubscribePost = false; for(const h of parsed.headers) { const lowerKey = h.keyLowerCase; if(!copyHeaders.has(lowerKey)) { continue; } headers[lowerKey] = h.value; switch(lowerKey) { case "list-unsubscribe": doNotSendDR = true; break; case "list-unsubscribe-post": hasUnsubscribePost = true; doNotSendDR = true; break; } } headers["Auto-Unsubscribe-On-Fail"] ??= "yes"; if(!hasUnsubscribePost) { headers["List-Unsubscribe-Post"] ??= "List-Unsubscribe=One-Click"; } const now = DateTime.now; const l = await this.els.save(null, file); const email = this.db.emails.add({ senderID: sender.mailboxID, mimeFileID: l.logID, recipients: recipients.map((r) => this.db.emailRecipients.add({ emailAddressText: r, type: "to", })), headerMessageID: parsed.messageId, subject: parsed.subject, dateCreated: now, headers: JSON.stringify(headers), textBody: parsed.text, htmlBody: parsed.html || parsed.textAsHtml, status: "scheduled", doNotTrack: true, priority, doNotSendDR, dateSent: now }); await this.db.saveChanges(); await this.db.emailLogs.withKeys({ logID: l.logID}).update({ emailID: email.emailID }); return this.json({ id: email.emailID }); } }