-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a log processor to output received email from smtp
- Loading branch information
Showing
3 changed files
with
1,580 additions
and
1,489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
-------------------------------------------------------------------------------- | ||
`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
} | ||
} |
Oops, something went wrong.