Skip to content

Commit

Permalink
feat: add custom header support (closes #22) (#23)
Browse files Browse the repository at this point in the history
* feat: add custom header support (closes #22)

* deno fmt, lint
  • Loading branch information
mathe42 authored May 18, 2022
1 parent e3dbf9d commit 1f7732c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,16 @@ export interface SendConfig {
* allowes preprocessors to hande different email types
*/
internalTag?: string | symbol;
headers: Record<string, string>;
}
```

All of it should be clear by name except:

#### headers

Add custom headers to the email.

#### mimeContent

There are use cases where you want to do encoding etc. on your own. This option
Expand Down
7 changes: 7 additions & 0 deletions client/basic/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ export class SMTPClient {

await this.#connection.writeCmd("Date: ", config.date);

const obj = Object.entries(config.headers);

for (let i = 0; i < obj.length; i++) {
const [name, value] = obj[i];
await this.#connection.writeCmd(name + ": ", value);
}

if (config.inReplyTo) {
await this.#connection.writeCmd("InReplyTo: ", config.inReplyTo);
}
Expand Down
13 changes: 13 additions & 0 deletions config/mail/headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface Headers {
[headerName: string]: string;
}

export function validateHeaders(
headers: Headers,
) {
return !(Object.keys(headers).some((v) =>
v.includes("\n") || v.includes("\r")
) || Object.values(headers).some((v) =>
v.includes("\n") || v.includes("\r")
));
}
10 changes: 9 additions & 1 deletion config/mail/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
validateEmailList,
} from "./email.ts";
import { ResolvedClientOptions } from "../client.ts";

import { Headers, validateHeaders } from "./headers.ts";
/**
* Config for a mail
*/
Expand All @@ -37,6 +37,7 @@ export interface SendConfig {
* allowes preprocessors to hande different email types
*/
internalTag?: string | symbol;
headers?: Headers;
}

export interface ResolvedSendConfig {
Expand All @@ -53,6 +54,7 @@ export interface ResolvedSendConfig {
priority?: "high" | "normal" | "low";
attachments: ResolvedAttachment[];
internalTag?: string | symbol;
headers: Headers;
}

export function resolveSendConfig(config: SendConfig): ResolvedSendConfig {
Expand All @@ -72,6 +74,7 @@ export function resolveSendConfig(config: SendConfig): ResolvedSendConfig {
priority,
attachments,
internalTag,
headers,
} = config;

return {
Expand All @@ -94,6 +97,7 @@ export function resolveSendConfig(config: SendConfig): ResolvedSendConfig {
references,
priority,
internalTag,
headers: headers ?? ({} as Headers),
};
}

Expand Down Expand Up @@ -160,6 +164,10 @@ export function validateConfig(
warn.push("You should provide at least html or text content!");
}

if (!validateHeaders(config.headers)) {
errors.push(`Headers are not allowed to include linebreaks!`);
}

if (client.client.warning === "log" && warn.length > 0) {
console.warn(warn.join("\n"));
}
Expand Down

0 comments on commit 1f7732c

Please sign in to comment.