@agency-lang/email 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/email.d.ts +23 -0
- package/dist/src/email.js +51 -0
- package/index.agency +63 -0
- package/package.json +45 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type SmtpOptions = {
|
|
2
|
+
host?: string;
|
|
3
|
+
port?: number;
|
|
4
|
+
secure?: boolean;
|
|
5
|
+
user?: string;
|
|
6
|
+
pass?: string;
|
|
7
|
+
};
|
|
8
|
+
export type EmailParams = {
|
|
9
|
+
from: string;
|
|
10
|
+
to: string | string[];
|
|
11
|
+
subject: string;
|
|
12
|
+
html?: string;
|
|
13
|
+
text?: string;
|
|
14
|
+
cc?: string | string[];
|
|
15
|
+
bcc?: string | string[];
|
|
16
|
+
replyTo?: string;
|
|
17
|
+
};
|
|
18
|
+
export type EmailResult = {
|
|
19
|
+
messageId: string;
|
|
20
|
+
accepted: string[];
|
|
21
|
+
rejected: string[];
|
|
22
|
+
};
|
|
23
|
+
export declare function sendEmail(params: EmailParams, options?: SmtpOptions): Promise<EmailResult>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import nodemailer from "nodemailer";
|
|
2
|
+
function getSmtpConfig(options) {
|
|
3
|
+
const host = options?.host || process.env.SMTP_HOST;
|
|
4
|
+
if (!host) {
|
|
5
|
+
throw new Error("Missing SMTP host. Set SMTP_HOST env var or pass host option.");
|
|
6
|
+
}
|
|
7
|
+
// port=0 means "not provided" — fall through to env or default
|
|
8
|
+
const port = (options?.port && options.port > 0)
|
|
9
|
+
? options.port
|
|
10
|
+
: (Number(process.env.SMTP_PORT) || 587);
|
|
11
|
+
// secure is only explicitly set if port was explicitly provided as 465,
|
|
12
|
+
// or SMTP_SECURE env is set. Otherwise default based on resolved port.
|
|
13
|
+
const secure = options?.secure ?? (process.env.SMTP_SECURE === "true" || port === 465);
|
|
14
|
+
const user = options?.user || process.env.SMTP_USER;
|
|
15
|
+
const pass = options?.pass || process.env.SMTP_PASS;
|
|
16
|
+
const config = { host, port, secure };
|
|
17
|
+
if (user && pass) {
|
|
18
|
+
config.auth = { user, pass };
|
|
19
|
+
}
|
|
20
|
+
return config;
|
|
21
|
+
}
|
|
22
|
+
export async function sendEmail(params, options) {
|
|
23
|
+
const config = getSmtpConfig(options);
|
|
24
|
+
const transporter = nodemailer.createTransport(config);
|
|
25
|
+
const mailOptions = {
|
|
26
|
+
from: params.from,
|
|
27
|
+
to: Array.isArray(params.to) ? params.to.join(", ") : params.to,
|
|
28
|
+
subject: params.subject,
|
|
29
|
+
};
|
|
30
|
+
if (params.html)
|
|
31
|
+
mailOptions.html = params.html;
|
|
32
|
+
if (params.text)
|
|
33
|
+
mailOptions.text = params.text;
|
|
34
|
+
if (params.cc)
|
|
35
|
+
mailOptions.cc = Array.isArray(params.cc) ? params.cc.join(", ") : params.cc;
|
|
36
|
+
if (params.bcc)
|
|
37
|
+
mailOptions.bcc = Array.isArray(params.bcc) ? params.bcc.join(", ") : params.bcc;
|
|
38
|
+
if (params.replyTo)
|
|
39
|
+
mailOptions.replyTo = params.replyTo;
|
|
40
|
+
try {
|
|
41
|
+
const info = await transporter.sendMail(mailOptions);
|
|
42
|
+
return {
|
|
43
|
+
messageId: info.messageId ?? "",
|
|
44
|
+
accepted: info.accepted ?? [],
|
|
45
|
+
rejected: info.rejected ?? [],
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
transporter.close();
|
|
50
|
+
}
|
|
51
|
+
}
|
package/index.agency
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
## Installation
|
|
3
|
+
|
|
4
|
+
```
|
|
5
|
+
npm install @agency-lang/email
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
## Environment Variables
|
|
9
|
+
|
|
10
|
+
Set SMTP connection details:
|
|
11
|
+
- `SMTP_HOST` — SMTP server hostname (e.g. "smtp.gmail.com")
|
|
12
|
+
- `SMTP_PORT` — Port number (default: 587)
|
|
13
|
+
- `SMTP_USER` — Username for authentication (optional for unauthenticated relays)
|
|
14
|
+
- `SMTP_PASS` — Password or app password
|
|
15
|
+
- `SMTP_SECURE` — "true" for TLS on connect (port 465), "false" for STARTTLS (port 587)
|
|
16
|
+
|
|
17
|
+
Alternatively, pass these directly via parameters.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { sendEmail } from "pkg::@agency-lang/email"
|
|
23
|
+
|
|
24
|
+
node main() {
|
|
25
|
+
const result = sendEmail(
|
|
26
|
+
from: "you@gmail.com",
|
|
27
|
+
to: "friend@example.com",
|
|
28
|
+
subject: "Hello from Agency!",
|
|
29
|
+
text: "This email was sent by my agent."
|
|
30
|
+
)
|
|
31
|
+
print(result)
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Common SMTP Configurations
|
|
36
|
+
|
|
37
|
+
**Gmail:** host="smtp.gmail.com", port=587, user=your email, pass=app password
|
|
38
|
+
**Outlook:** host="smtp.office365.com", port=587
|
|
39
|
+
**Yahoo:** host="smtp.mail.yahoo.com", port=465, secure=true
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import { sendEmail as sendEmailImpl } from "./dist/src/email.js"
|
|
43
|
+
|
|
44
|
+
export def sendEmail(from: string, to: string, subject: string, html: string = "", text: string = "", cc: string = "", bcc: string = "", replyTo: string = "", host: string = "", port: number = 0, secure: boolean = false, user: string = "", pass: string = ""): Result {
|
|
45
|
+
"""
|
|
46
|
+
Send an email via SMTP using Nodemailer. Works with any email provider (Gmail, Outlook, Yahoo, self-hosted, etc). Requires SMTP_HOST env var or pass host directly. Authentication (SMTP_USER/SMTP_PASS) is optional. Set port to 0 for auto-detection (default 587). Secure is auto-detected from port and SMTP_SECURE env var when not explicitly set.
|
|
47
|
+
"""
|
|
48
|
+
return try sendEmailImpl({
|
|
49
|
+
from: from,
|
|
50
|
+
to: to,
|
|
51
|
+
subject: subject,
|
|
52
|
+
html: html,
|
|
53
|
+
text: text,
|
|
54
|
+
cc: cc,
|
|
55
|
+
bcc: bcc,
|
|
56
|
+
replyTo: replyTo
|
|
57
|
+
}, {
|
|
58
|
+
host: host,
|
|
59
|
+
port: port,
|
|
60
|
+
user: user,
|
|
61
|
+
pass: pass
|
|
62
|
+
})
|
|
63
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agency-lang/email",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "SMTP email integration for Agency via Nodemailer",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"agency": "./index.agency",
|
|
7
|
+
"main": "./index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/src/email.d.ts",
|
|
11
|
+
"import": "./dist/src/email.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"index.agency",
|
|
18
|
+
"index.js"
|
|
19
|
+
],
|
|
20
|
+
"author": "Aditya Bhargava",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/egonSchiele/agency-lang/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/egonSchiele/agency-lang",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"nodemailer": "^7.0.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"agency-lang": ">=0.9.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^25.0.6",
|
|
34
|
+
"@types/nodemailer": "^6.4.0",
|
|
35
|
+
"typescript": "^5.0.0",
|
|
36
|
+
"vitest": "^3.0.0",
|
|
37
|
+
"agency-lang": "0.9.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc",
|
|
41
|
+
"test": "vitest",
|
|
42
|
+
"test:run": "vitest run",
|
|
43
|
+
"test:agency": "agency tests/agency"
|
|
44
|
+
}
|
|
45
|
+
}
|