diff --git a/README.md b/README.md index 45460203..9981f70a 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ Some features: - name: Send mail uses: dawidd6/action-send-mail@ env: - # Optional http proxy: - HTTP_PROXY: http://proxy.example.test:3128 + # Optional proxy, NO_PROXY is honored too: + SMTP_PROXY: http://proxy.example.test:3128 with: # Specify connection via URL (replaces server_address, server_port, secure, # username and password) diff --git a/main.js b/main.js index 12c1c7ad..4b65f4aa 100644 --- a/main.js +++ b/main.js @@ -6,6 +6,22 @@ import fs from "node:fs"; import showdown from "showdown"; import path from "node:path"; +// smtp_proxy or smtps_proxy, exempted by no_proxy, each preferred lowercase like curl. +function getProxy(host) { + const env = (name) => process.env[name] || process.env[name.toUpperCase()]; + + const proxy = env("smtp_proxy") || env("smtps_proxy"); + if (!proxy) return undefined; + + host = `.${host.toLowerCase()}`; + const excluded = (env("no_proxy") || "") + .split(",") + .map((entry) => entry.trim().replace(/^\./, "").toLowerCase()) + .some((entry) => entry && (entry === "*" || host.endsWith(`.${entry}`))); + + return excluded ? undefined : proxy; +} + function getText(textOrFile, convertMarkdown) { let text = textOrFile; @@ -188,7 +204,7 @@ async function main() { : undefined, logger: nodemailerdebug == "true" ? true : nodemailerlog, debug: nodemailerdebug, - proxy: process.env.HTTP_PROXY, + proxy: getProxy(serverAddress), }); const messageOptions = {