API: E-mail (SMTP)
Funkcja do wysyłania wiadomości e-mail przez serwer SMTP.
Rejestracja w C++: regEmail(js).
Implementacja: jsEmail.h.
send_email
send_email(config: object) → object
Wyślij wiadomość e-mail przez SMTP.
Parametry (pola obiektu config):
| Pole | Typ | Wymagane | Opis |
|---|---|---|---|
host |
string | ✓ | Serwer SMTP |
port |
number | ✓ | Port (25, 465, 587) |
user |
string | ✓ | Login do uwierzytelnienia SMTP |
pass |
string | ✓ | Hasło |
ssl |
bool | — | true — SMTPS (SSL od pierwszego bajtu, zazwyczaj port 465) |
starttls |
bool | — | true — STARTTLS (podniesienie poziomu połączenia, zazwyczaj port 587) |
from |
object | ✓ | Nadawca (patrz poniżej) |
to |
object | ✓ | Odbiorca (patrz poniżej) |
subject |
string | ✓ | Temat wiadomości |
body |
string | ✓ | Treść wiadomości (zwykły tekst) |
Pola from i to:
| Pole | Typ | Opis |
|---|---|---|
email |
string | Adres e-mail |
name |
string | Nazwa wyświetlana |
Wynik:
| Pole | Typ | Opis |
|---|---|---|
code |
number | 0 — sukces, wartość ujemna — błąd |
log |
string | Dialog SMTP (do debugowania) |
error |
string | Komunikat błędu lub "" |
Tryby połączenia
| Tryb | Port | Konfiguracja |
|---|---|---|
| SMTPS (SSL) | 465 | ssl: true |
| STARTTLS | 587 | starttls: true |
| Zwykłe SMTP | 25 | brak flag |
Przykłady
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("E-mail wysłany");
} else {
console.log("Błąd:", r.error);
console.log("Log SMTP:", r.log);
}
Gmail (Hasło aplikacji)
Gmail wymaga Hasła aplikacji, a nie zwykłego hasła. Włącz je: Konto Google → Bezpieczeństwo → Uwierzytelnianie dwuetapowe → Hasła aplikacji. Uwaga: Gmail nie obsługuje już SMTP.
Lokalny SMTP (bez 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: "Coś poszło nie tak"
});
Wysyłanie przez konfigurację
Konfiguracja 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"
}
}
Skrypt 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
Debugowanie SMTP
W przypadku błędu, wypisz r.log — zawiera on pełny dialog SMTP:
let r = send_email({ ... });
if (r.code !== 0) {
console.log("=== LOG SMTP ===");
console.log(r.log);
console.log("=== BŁĄD ===");
console.log(r.error);
}