Skip to content

Commit

Permalink
feat/related-attachments add relatedAttachments field
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-schaber committed Jan 27, 2023
1 parent e8a77a5 commit 49c6457
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
43 changes: 30 additions & 13 deletions config/mail/content.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Attachment, resolveAttachment } from "./attachments.ts";
import {
Attachment,
resolveAttachment,
ResolvedAttachment,
} from "./attachments.ts";
import { quotedPrintableEncode } from "./encoding.ts";

export interface Content {
Expand All @@ -8,7 +12,25 @@ export interface Content {
relatedAttachments?: Attachment[];
}

export function resolveContent({
export interface ResolvedContent {
mimeType: string;
content: string;
transferEncoding?: string;
relatedAttachments: ResolvedAttachment[];
}

export function resolveContent(content: Content): ResolvedContent {
return {
mimeType: content.mimeType,
content: content.content,
transferEncoding: content.transferEncoding,
relatedAttachments: content.relatedAttachments
? content.relatedAttachments.map((v) => resolveAttachment(v))
: [],
};
}

export function resolveMessage({
text,
html,
relatedAttachments,
Expand All @@ -18,8 +40,8 @@ export function resolveContent({
html?: string;
relatedAttachments?: Attachment[];
mimeContent?: Content[];
}): Content[] {
const newContent = [...mimeContent ?? []];
}): ResolvedContent[] {
const newContent = [...mimeContent ?? []].map((v) => resolveContent(v));

if (text === "auto" && html) {
text = html
Expand All @@ -33,22 +55,17 @@ export function resolveContent({
mimeType: 'text/plain; charset="utf-8"',
content: quotedPrintableEncode(text),
transferEncoding: "quoted-printable",
relatedAttachments: [],
});
}

if (html) {
const newMime: Content = {
newContent.push(resolveContent({
mimeType: 'text/html; charset="utf-8"',
content: quotedPrintableEncode(html),
transferEncoding: "quoted-printable",
};

if (relatedAttachments) {
newMime.relatedAttachments = relatedAttachments.map((attachment) =>
resolveAttachment(attachment)
);
}
newContent.push(newMime);
relatedAttachments,
}));
}

return newContent;
Expand Down
6 changes: 3 additions & 3 deletions config/mail/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
resolveAttachment,
ResolvedAttachment,
} from "./attachments.ts";
import { Content, resolveContent } from "./content.ts";
import { Content, ResolvedContent, resolveMessage } from "./content.ts";
import {
isSingleMail,
mailList,
Expand Down Expand Up @@ -49,7 +49,7 @@ export interface ResolvedSendConfig {
from: saveMailObject;
date: string;
subject: string;
mimeContent: Content[];
mimeContent: ResolvedContent[];
inReplyTo?: string;
replyTo?: saveMailObject;
references?: string;
Expand Down Expand Up @@ -86,7 +86,7 @@ export function resolveSendConfig(config: SendConfig): ResolvedSendConfig {
bcc: parseMailList(bcc),
from: parseSingleEmail(from),
date,
mimeContent: resolveContent({
mimeContent: resolveMessage({
mimeContent,
html,
relatedAttachments,
Expand Down

0 comments on commit 49c6457

Please sign in to comment.