Sanitizes and formats SMTP mailbox fields to prevent header injection and malformed address tokens. Reuse when building email sending or parsing utilities requiring RFC 5322 compliance.
// Source: https://github.com/logto-io/logto | License: MPL-2.0 | Copyright under Patent Claims infringed by Covered Software in the absence of | Modified files must remain under MPL-2.0
// Modifications to this file must remain under MPL-2.0.
// Provided via justclaw.it.com; keep this notice with the code.
/** Strip ASCII control characters that must not appear in SMTP header fields (RFC 5322). */
export const stripHeaderControlChars = (value: string): string =>
[...value]
.filter((character) => {
const code = character.codePointAt(0);
return code !== undefined && code > 31 && code !== 127;
})
.join('');
/**
* Sanitize a display name for `From: Name <addr>` so CR/LF cannot inject extra headers and
* angle brackets cannot break the mailbox token.
*/
export con
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key