-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.mjs
More file actions
77 lines (69 loc) · 2.57 KB
/
Copy pathsend.mjs
File metadata and controls
77 lines (69 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Example: sending example 05's digest email via SMTP using nodemailer.
//
// Run `npm run examples` first (or at least
// `node examples/05-plain-text/run.mjs`) so dist/05-plain-text/digest.html
// and digest.txt exist.
//
// Install nodemailer: npm install nodemailer
// Then configure SMTP settings below.
//
// Alternatives:
// - SendGrid: @sendgrid/mail
// - Postmark: postmark
// - Mailgun: mailgun-js
// - Amazon SES: @aws-sdk/client-ses
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import { fileURLToPath } from "node:url";
// import nodemailer from "nodemailer";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const htmlPath = path.join(__dirname, "dist/05-plain-text/digest.html");
const textPath = path.join(__dirname, "dist/05-plain-text/digest.txt");
if (!fs.existsSync(htmlPath) || !fs.existsSync(textPath)) {
console.error("Missing dist/05-plain-text/digest.{html,txt} — run `npm run examples` first.");
process.exit(1);
}
const html = fs.readFileSync(htmlPath, "utf-8");
const text = fs.readFileSync(textPath, "utf-8");
// const transport = nodemailer.createTransport({
// host: "smtp.example.com",
// port: 587,
// secure: false,
// auth: { user: "your-username", pass: "your-password" },
// });
//
// const info = await transport.sendMail({
// from: '"Northwind Coffee" <digest@northwindcoffee.example>',
// to: "subscriber@example.com",
// subject: "This week at Northwind Coffee",
// text,
// html,
// });
// console.log("sent:", info.messageId);
// What nodemailer builds under the hood, spelled out by hand: a
// multipart/alternative message with the plain-text part first (least
// capable first) and the HTML part second, joined by a MIME boundary.
const boundary = `nwc-${crypto.randomBytes(16).toString("hex")}`;
const message =
"From: Northwind Coffee <digest@northwindcoffee.example>\r\n" +
"To: subscriber@example.com\r\n" +
"Subject: This week at Northwind Coffee\r\n" +
"MIME-Version: 1.0\r\n" +
`Content-Type: multipart/alternative; boundary="${boundary}"\r\n` +
"\r\n" +
`--${boundary}\r\n` +
"Content-Type: text/plain; charset=utf-8\r\n" +
"\r\n" +
text +
"\r\n" +
`--${boundary}\r\n` +
"Content-Type: text/html; charset=utf-8\r\n" +
"\r\n" +
html +
"\r\n" +
`--${boundary}--\r\n`;
console.log("SMTP config not set — edit send.mjs with your credentials.");
console.log(`HTML part: ${Buffer.byteLength(html)} bytes`);
console.log(`Text part: ${Buffer.byteLength(text)} bytes`);
console.log(`Multipart total: ${Buffer.byteLength(message)} bytes`);