Skip to content

Commit

Permalink
feat: add a log processor to output received email from smtp
Browse files Browse the repository at this point in the history
  • Loading branch information
loopingz committed Jun 21, 2023
1 parent df651f6 commit 98f132c
Show file tree
Hide file tree
Showing 3 changed files with 1,580 additions and 1,489 deletions.
64 changes: 64 additions & 0 deletions src/processors/log.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { suite, test } from "@testdeck/mocha";
import { WorkerOutput } from "@webda/workout";
import * as assert from "assert";
import { HeaderValue } from "mailparser";
import * as sinon from "sinon";
import { SmtpSession } from "../server";
import { getFakeSession } from "../server.spec";
import { LogProcessor } from "./log";

@suite
class LogProcessorTest {
@test
async mailer() {
let nodemailer = new LogProcessor(
undefined,
{
type: "log",
},
new WorkerOutput()
);

let session: SmtpSession = getFakeSession();
let msg;

session.email.to = [
{
html: "Html content",
text: "Text content",
value: [{ name: "Test", address: "test@plop.com" }]
}
];
session.email.html = "Html content";
session.email.text = "Text content";
session.email.subject = "Subject";
session.email.from = {
html: "Html content",
text: "Text content",
value: [{ name: "Test", address: "" }]
};

const headers = new Map<string, HeaderValue>();
headers.set("plop", "test");
// @ts-ignore
session.email.attachments.push({
contentDisposition: "plop",
headers
});
let calls = [];
let stub = sinon.stub(console, "log").callsFake((...args) => {
calls.push(args);
});
await nodemailer.onMail(session);
stub.restore();
msg = calls.map(c => c.join(" ")).join("\n").replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/, "UTC_DATE");
assert.strictEqual(msg, `Email received UTC_DATE from 127.0.0.1
--------------------------------------------------------------------------------
from: Text content
to: test@plop.com
subject: Subject
text: Text content
--------------------------------------------------------------------------------
`);
}
}
44 changes: 44 additions & 0 deletions src/processors/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { SmtpComponentConfig } from "../component";
import { SmtpProcessor } from "../processor";
import { SmtpSession } from "../server";
import { NodeMailerProcessor } from "./nodemailer";

export interface LogProcessorConfig extends SmtpComponentConfig {
type: "log";
/**
* Fields to log
*
* @default ["from", "to", "cc", "subject", "text"]
*/
fields?: string[];
}

export class LogProcessor<
T extends LogProcessorConfig = LogProcessorConfig
> extends SmtpProcessor<T> {
type: string = "log";

init(): void {
this.config.fields ??= ["from", "to", "cc", "subject", "text"];
}

/**
* Send with current NodeMailer transporter
* @param session
* @returns
*/
async onMail(session: SmtpSession): Promise<void> {
const email = NodeMailerProcessor.transformEmail(session.email);
console.log("Email received", new Date().toISOString(), "from", session.remoteAddress);
console.log("-".repeat(80));
this.config.fields.filter(f => email[f] !== undefined).forEach(f => {
let value = email[f];
if (value instanceof Array) {
value = email[f].join(", ");
}
console.log(f +":", value)
});
console.log("-".repeat(80));
console.log("");
}
}

0 comments on commit 98f132c

Please sign in to comment.