smtp
The SMTP extension allows sending emails directly from your scripts. Supports STARTTLS (port 587) and direct TLS (port 465) with AUTH LOGIN authentication.
One-shot Send
The simplest way to send an email — connects, sends, and disconnects in one call:
smtp.send({
host: "smtp.example.com",
port: 587,
user: "you@example.com",
password: "your-password",
from: "you@example.com",
to: "recipient@example.com",
subject: "Hello",
body: "This is the email body."
});
Persistent Connection
For sending multiple emails, connect once and reuse the connection:
const conn = smtp.connect("smtp.example.com", 587, "you@example.com", "your-password");
conn.send({
from: "you@example.com",
to: "alice@example.com",
subject: "First email",
body: "Hello Alice!"
});
conn.send({
from: "you@example.com",
to: "bob@example.com",
subject: "Second email",
body: "Hello Bob!"
});
conn.close();
API
| Method | Description |
|---|---|
smtp.send(options) | One-shot send — connects, sends, and disconnects |
smtp.connect(host, port, user, password) | Open a persistent SMTP connection |
conn.send(options) | Send an email on an open connection |
conn.close() | Close the connection |
smtp.send() options
| Property | Required | Description |
|---|---|---|
host | Yes | SMTP server hostname |
port | No | Port number (default: 587) |
user | No | Username for authentication |
password | No | Password for authentication |
from | Yes | Sender email address |
to | Yes | Recipient email address |
subject | Yes | Email subject line |
body | Yes | Email body (plain text) |
conn.send() options
| Property | Required | Description |
|---|---|---|
from | Yes | Sender email address |
to | Yes | Recipient email address |
subject | Yes | Email subject line |
body | Yes | Email body (plain text) |
TLS Support
- Port 587 — STARTTLS (upgrades plain connection to TLS)
- Port 465 — Direct TLS (implicit, connection is encrypted from the start)
TLS is handled automatically based on the port number. No additional configuration is needed.
Error Handling
Both smtp.send() and conn.send() throw on failure:
try {
smtp.send({ ... });
} catch (e) {
console.error("Failed to send: " + e.message);
}
Common errors:
SMTP connection failed— could not connect or TLS handshake failedSMTP send failed— server rejected the message (bad credentials, invalid recipient, etc.)
Requirements
The SMTP extension requires OpenSSL to be installed on the system (typically already present if using the crypto extension):
# Debian/Ubuntu
sudo apt-get install libssl3
# RHEL/Fedora
sudo dnf install openssl-libs
Notes
- Up to 8 simultaneous connections are supported
- Emails are sent as plain text with UTF-8 encoding
- Authentication uses AUTH LOGIN mechanism
- Always close persistent connections when done