API : E-mail (SMTP)
Fonction pour envoyer un e-mail via un serveur SMTP.
Enregistrement C++ : regEmail(js).
Implémentation : jsEmail.h.
send_email
send_email(config: object) → object
Envoyer un e-mail via SMTP.
Paramètres (champs de l'objet config) :
| Champ | Type | Requis | Description |
|---|---|---|---|
host |
string | ✓ | Serveur SMTP |
port |
number | ✓ | Port (25, 465, 587) |
user |
string | ✓ | Identifiant d'authentification SMTP |
pass |
string | ✓ | Mot de passe |
ssl |
bool | — | true — SMTPS (SSL dès le premier octet, généralement port 465) |
starttls |
bool | — | true — STARTTLS (mise à niveau de connexion, généralement port 587) |
from |
object | ✓ | Expéditeur (voir ci-dessous) |
to |
object | ✓ | Destinataire (voir ci-dessous) |
subject |
string | ✓ | Objet de l'e-mail |
body |
string | ✓ | Corps de l'e-mail (texte brut) |
Champs de from et to :
| Champ | Type | Description |
|---|---|---|
email |
string | Adresse e-mail |
name |
string | Nom affiché |
Résultat :
| Champ | Type | Description |
|---|---|---|
code |
number | 0 — succès, négatif — erreur |
log |
string | Dialogue SMTP (pour débogage) |
error |
string | Message d'erreur ou "" |
Modes de connexion
| Mode | Port | Configuration |
|---|---|---|
| SMTPS (SSL) | 465 | ssl: true |
| STARTTLS | 587 | starttls: true |
| SMTP standard | 25 | aucun drapeau |
Exemples
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: "Alerte : service indisponible",
body: "MyService a cessé de répondre à 14:35."
});
if (r.code === 0) {
console.log("E-mail envoyé");
} else {
console.log("Erreur :", r.error);
console.log("Journal SMTP :", r.log);
}
Gmail (Mot de passe d'application)
Gmail nécessite un Mot de passe d'application, et non votre mot de passe habituel. Activez-le : Compte Google → Sécurité → Authentification à deux facteurs → Mots de passe d'applications. Remarque : Gmail ne prend plus en charge SMTP.
SMTP local (sans 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: "Alerte",
body: "Quelque chose s'est mal passé"
});
Envoi via configuration
Configuration 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": "Destinataire"
},
"subject": "Message test",
"body": "Ceci est un e-mail test de 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
Débogage SMTP
En cas d'erreur, affichez r.log — il contient l'intégralité du dialogue SMTP :
let r = send_email({ ... });
if (r.code !== 0) {
console.log("=== JOURNAL SMTP ===");
console.log(r.log);
console.log("=== ERREUR ===");
console.log(r.error);
}