API: Email (SMTP)
Funzione per l'invio di email tramite un server SMTP.
Registrazione C++: regEmail(js).
Implementazione: jsEmail.h.
send_email
send_email(config: object) → object
Invia un'email via SMTP.
Parametri (campi dell'oggetto config):
| Campo | Tipo | Obbligatorio | Descrizione |
|---|---|---|---|
host |
string | ✓ | Server SMTP |
port |
number | ✓ | Porta (25, 465, 587) |
user |
string | ✓ | Login per autenticazione SMTP |
pass |
string | ✓ | Password |
ssl |
bool | — | true — SMTPS (SSL dal primo byte, solitamente porta 465) |
starttls |
bool | — | true — STARTTLS (upgrade della connessione, solitamente porta 587) |
from |
object | ✓ | Mittente (vedi sotto) |
to |
object | ✓ | Destinatario (vedi sotto) |
subject |
string | ✓ | Oggetto dell'email |
body |
string | ✓ | Corpo dell'email (testo semplice) |
Campi di from e to:
| Campo | Tipo | Descrizione |
|---|---|---|
email |
string | Indirizzo email |
name |
string | Nome visualizzato |
Risultato:
| Campo | Tipo | Descrizione |
|---|---|---|
code |
number | 0 — successo, negativo — errore |
log |
string | Dialogo SMTP (per debug) |
error |
string | Messaggio di errore o "" |
Modalità di connessione
| Modalità | Porta | Configurazione |
|---|---|---|
| SMTPS (SSL) | 465 | ssl: true |
| STARTTLS | 587 | starttls: true |
| SMTP semplice | 25 | nessun flag |
Esempi
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: servizio non disponibile",
body: "MyService ha smesso di rispondere alle 14:35."
});
if (r.code === 0) {
console.log("Email inviata");
} else {
console.log("Errore:", r.error);
console.log("Log SMTP:", r.log);
}
Gmail (Password App)
Gmail richiede una Password App, non la tua password normale. Abilitala: Account Google → Sicurezza → Autenticazione a due fattori → Password app. Nota: Gmail non supporta più SMTP.
SMTP locale (senza 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: "Qualcosa è andato storto"
});
Invio tramite configurazione
Configurazione 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": "Messaggio di test",
"body": "Questa è un'email di test da 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
Debug SMTP
In caso di errore, stampa r.log — contiene l'intero dialogo SMTP:
let r = send_email({ ... });
if (r.code !== 0) {
console.log("=== LOG SMTP ===");
console.log(r.log);
console.log("=== ERRORE ===");
console.log(r.error);
}