API: E-Mail (SMTP)
Funktion zum Senden von E-Mails über einen SMTP-Server.
C++-Registrierung: regEmail(js).
Implementierung: jsEmail.h.
send_email
send_email(config: object) → object
Sendet eine E-Mail über SMTP.
Parameter (Felder des config-Objekts):
| Feld | Typ | Erforderlich | Beschreibung |
|---|---|---|---|
host |
string | ✓ | SMTP-Server |
port |
number | ✓ | Port (25, 465, 587) |
user |
string | ✓ | SMTP-Authentifizierungs-Login |
pass |
string | ✓ | Passwort |
ssl |
bool | — | true — SMTPS (SSL ab dem ersten Byte, üblicherweise Port 465) |
starttls |
bool | — | true — STARTTLS (Verbindungs-Upgrade, üblicherweise Port 587) |
from |
object | ✓ | Absender (siehe unten) |
to |
object | ✓ | Empfänger (siehe unten) |
subject |
string | ✓ | E-Mail-Betreff |
body |
string | ✓ | E-Mail-Text (Klartext) |
Felder von from und to:
| Feld | Typ | Beschreibung |
|---|---|---|
email |
string | E-Mail-Adresse |
name |
string | Anzeigename |
Ergebnis:
| Feld | Typ | Beschreibung |
|---|---|---|
code |
number | 0 — Erfolg, negativ — Fehler |
log |
string | SMTP-Dialog (für Debugging) |
error |
string | Fehlermeldung oder "" |
Verbindungsmodi
| Modus | Port | Konfiguration |
|---|---|---|
| SMTPS (SSL) | 465 | ssl: true |
| STARTTLS | 587 | starttls: true |
| Klartext-SMTP | 25 | keine Flags |
Beispiele
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: "Warnung: Dienst nicht verfügbar",
body: "MyService hat um 14:35 Uhr nicht mehr geantwortet."
});
if (r.code === 0) {
console.log("E-Mail gesendet");
} else {
console.log("Fehler:", r.error);
console.log("SMTP-Protokoll:", r.log);
}
Gmail (App-Passwort)
Gmail erfordert ein App-Passwort, nicht Ihr reguläres Passwort. Aktivieren Sie es: Google-Konto → Sicherheit → Bestätigung in zwei Schritten → App-Passwörter. Hinweis: Gmail unterstützt SMTP nicht mehr.
Lokaler SMTP (ohne 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: "Warnung",
body: "Etwas ist schiefgelaufen"
});
Senden über Konfiguration
Konfiguration 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": "Testnachricht",
"body": "Dies ist eine Test-E-Mail von JsN"
}
}
Skript 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
Geben Sie bei einem Fehler r.log aus — es enthält den vollständigen SMTP-Dialog:
let r = send_email({ ... });
if (r.code !== 0) {
console.log("=== SMTP-PROTOKOLL ===");
console.log(r.log);
console.log("=== FEHLER ===");
console.log(r.error);
}