gcharanteja commited on
Commit
1e8fcff
·
1 Parent(s): 6ad81dc
Files changed (4) hide show
  1. Dockerfile +1 -0
  2. nginx-smtp.conf +8 -0
  3. server.js +10 -2
  4. test-email.mjs +36 -0
Dockerfile CHANGED
@@ -11,6 +11,7 @@ COPY . .
11
 
12
  ENV PORT=7860
13
  ENV DATA_DIR=/data
 
14
  EXPOSE 7860
15
 
16
  VOLUME ["/data"]
 
11
 
12
  ENV PORT=7860
13
  ENV DATA_DIR=/data
14
+ ENV SMTP_PROXY=http://nginx:2587
15
  EXPOSE 7860
16
 
17
  VOLUME ["/data"]
nginx-smtp.conf ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ stream {
2
+ server {
3
+ listen 2587;
4
+ proxy_pass smtp-relay.brevo.com:587;
5
+ proxy_connect_timeout 30s;
6
+ proxy_timeout 60s;
7
+ }
8
+ }
server.js CHANGED
@@ -88,12 +88,14 @@ function writeState(state) {
88
  fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2));
89
  }
90
 
 
 
91
  async function sendEmail(subject, body) {
92
  log("Fetching SMTP config...");
93
  const cfg = await getSmtpConfig();
94
  log(`SMTP config loaded (host=${cfg.host}, port=${cfg.port}, from=${cfg.from}, to=${cfg.to})`);
95
 
96
- const transporter = nodemailer.createTransport({
97
  host: cfg.host,
98
  port: cfg.port,
99
  secure: cfg.port === 465,
@@ -101,7 +103,13 @@ async function sendEmail(subject, body) {
101
  connectionTimeout: 30000,
102
  greetingTimeout: 30000,
103
  socketTimeout: 60000,
104
- });
 
 
 
 
 
 
105
 
106
  log("Sending email...");
107
  const result = await transporter.sendMail({
 
88
  fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2));
89
  }
90
 
91
+ const SMTP_PROXY = process.env.SMTP_PROXY || "";
92
+
93
  async function sendEmail(subject, body) {
94
  log("Fetching SMTP config...");
95
  const cfg = await getSmtpConfig();
96
  log(`SMTP config loaded (host=${cfg.host}, port=${cfg.port}, from=${cfg.from}, to=${cfg.to})`);
97
 
98
+ const opts = {
99
  host: cfg.host,
100
  port: cfg.port,
101
  secure: cfg.port === 465,
 
103
  connectionTimeout: 30000,
104
  greetingTimeout: 30000,
105
  socketTimeout: 60000,
106
+ };
107
+ if (SMTP_PROXY) {
108
+ opts.proxy = SMTP_PROXY;
109
+ log(`Using SMTP proxy: ${SMTP_PROXY}`);
110
+ }
111
+
112
+ const transporter = nodemailer.createTransport(opts);
113
 
114
  log("Sending email...");
115
  const result = await transporter.sendMail({
test-email.mjs ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import https from "node:https";
2
+
3
+ const API_BASE = "https://maxxcarl-keyvault.hf.space/secrets";
4
+ const API_KEY = "Azure@123";
5
+ const SECRETS = ["SMTP_HOST","SMTP_PORT","SMTP_USER","SMTP_PASS","FROM_EMAIL","TO_EMAIL"];
6
+
7
+ function fetchSecret(key) {
8
+ return new Promise((resolve, reject) => {
9
+ https.get(`${API_BASE}/${key}`, { headers: { "X-API-Key": API_KEY } }, res => {
10
+ let data = "";
11
+ res.on("data", c => data += c);
12
+ res.on("end", () => resolve(JSON.parse(data).value));
13
+ }).on("error", reject);
14
+ });
15
+ }
16
+
17
+ const [host, port, user, pass, from, to] = await Promise.all(SECRETS.map(fetchSecret));
18
+
19
+ const { createTransport } = await import("nodemailer");
20
+ const t = createTransport({
21
+ host, port: Number(port), secure: false,
22
+ auth: { user, pass },
23
+ connectionTimeout: 15000, greetingTimeout: 15000,
24
+ });
25
+
26
+ await t.verify();
27
+ console.log("SMTP connection OK, sending...");
28
+
29
+ await t.sendMail({
30
+ from, to,
31
+ subject: "Test from local machine",
32
+ text: `If you see this, SMTP works!\n\nhost=${host} port=${port} user=${user}`,
33
+ });
34
+
35
+ console.log("Email sent!");
36
+ process.exit(0);