API: Email (SMTP)

Function for sending email via an SMTP server.

C++ registration: regEmail(js). Implementation: jsEmail.h.


send_email

send_email(config: object) → object

Send an email via SMTP.

Parameters (fields of the config object):

Field Type Required Description
host string SMTP server
port number Port (25, 465, 587)
user string SMTP authentication login
pass string Password
ssl bool true — SMTPS (SSL from the first byte, usually port 465)
starttls bool true — STARTTLS (connection upgrade, usually port 587)
from object Sender (see below)
to object Recipient (see below)
subject string Email subject
body string Email body (plain text)

Fields of from and to:

Field Type Description
email string Email address
name string Display name

Result:

Field Type Description
code number 0 — success, negative — error
log string SMTP dialog (for debugging)
error string Error message or ""

Connection modes

Mode Port Config
SMTPS (SSL) 465 ssl: true
STARTTLS 587 starttls: true
Plain SMTP 25 no flags

Examples

Office 365

let r = send_email({
    host: "smtp.office365.com",
    port: 587,
    ssl: false,
    starttls: true,
    user: "user@yourdomain.com",
    pass: "your_password",
    from: { email: "user@yourdomain.com", name: "System Mailer" },
    to:   { email: "admin@example.com",   name: "Admin" },
    subject: "Alert: service unavailable",
    body: "MyService stopped responding at 14:35."
});

if (r.code === 0) {
    console.log("Email sent");
} else {
    console.log("Error:", r.error);
    console.log("SMTP log:", r.log);
}

Gmail (App Password)

Gmail requires an App Password, not your regular password. Enable it: Google Account → Security → Two-factor authentication → App passwords. Note: Gmail no longer supports SMTP.

Local SMTP (no TLS)

let r = send_email({
    host: "mail.internal.corp",
    port: 25,
    ssl: false,
    starttls: false,
    user: "mailer@internal.corp",
    pass: "password",
    from: { email: "mailer@internal.corp", name: "Mailer" },
    to:   { email: "admin@internal.corp", name: "Admin" },
    subject: "Alert",
    body: "Something went wrong"
});

Sending via config

Config CaEmail.json:

{
  "scriptfile": "./email_send.js",
  "arg": {
    "host": "smtp.office365.com",
    "port": 587,
    "ssl": false,
    "starttls": true,
    "user": "user@yourdomain.com",
    "pass": "CHANGE_ME",
    "from": {
      "email": "user@yourdomain.com",
      "name": "System Mailer"
    },
    "to": {
      "email": "recipient@example.com",
      "name": "Recipient"
    },
    "subject": "Test message",
    "body": "This is a test email from JsN"
  }
}

Script email_send.js:

let resp = send_email({
    host:     arg.host,
    port:     arg.port,
    ssl:      arg.ssl,
    starttls: arg.starttls,
    user:     arg.user,
    pass:     arg.pass,
    from:     arg.from,
    to:       arg.to,
    subject:  arg.subject,
    body:     arg.body
});

console.log(JSON.stringify(resp, null, 2));
JsN.exe CaEmail.json

SMTP debugging

On error, print r.log — it contains the full SMTP dialog:

let r = send_email({ ... });
if (r.code !== 0) {
    console.log("=== SMTP LOG ===");
    console.log(r.log);
    console.log("=== ERROR ===");
    console.log(r.error);
}