chore: update SMTP module and tests; remove unused assets
This commit is contained in:
parent
81fd572e48
commit
037970d52b
4 changed files with 38 additions and 3 deletions
|
|
@ -13,6 +13,20 @@ function b64(input: string) {
|
|||
return Buffer.from(input, "utf8").toString("base64")
|
||||
}
|
||||
|
||||
function extractEnvelopeAddress(from: string): string {
|
||||
// Prefer address inside angle brackets
|
||||
const angle = from.match(/<\s*([^>\s]+)\s*>/)
|
||||
if (angle?.[1]) return angle[1]
|
||||
// Fallback: address inside parentheses
|
||||
const paren = from.match(/\(([^)\s]+@[^)\s]+)\)/)
|
||||
if (paren?.[1]) return paren[1]
|
||||
// Fallback: first email-like substring
|
||||
const email = from.match(/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/)
|
||||
if (email?.[0]) return email[0]
|
||||
// Last resort: use whole string
|
||||
return from
|
||||
}
|
||||
|
||||
export async function sendSmtpMail(cfg: SmtpConfig, to: string, subject: string, html: string) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const socket = tls.connect(cfg.port, cfg.host, { rejectUnauthorized: false }, () => {
|
||||
|
|
@ -44,7 +58,8 @@ export async function sendSmtpMail(cfg: SmtpConfig, to: string, subject: string,
|
|||
await wait(/^334 /)
|
||||
send(b64(cfg.password))
|
||||
await wait(/^235 /)
|
||||
send(`MAIL FROM:<${cfg.from.match(/<(.+)>/)?.[1] ?? cfg.from}>`)
|
||||
const envelopeFrom = extractEnvelopeAddress(cfg.from)
|
||||
send(`MAIL FROM:<${envelopeFrom}>`)
|
||||
await wait(/^250 /)
|
||||
send(`RCPT TO:<${to}>`)
|
||||
await wait(/^250 /)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue