API: メール (SMTP)

SMTPサーバー経由でメールを送信する関数。

C++登録: regEmail(js)。 実装: jsEmail.h


send_email

send_email(config: object) → object

SMTP経由でメールを送信します。

パラメータ (config オブジェクトのフィールド):

フィールド 必須 説明
host string SMTPサーバー
port number ポート (25, 465, 587)
user string SMTP認証ログイン
pass string パスワード
ssl bool true — SMTPS (最初のバイトからSSL、通常ポート465)
starttls bool true — STARTTLS (接続アップグレード、通常ポート587)
from object 送信者 (下記参照)
to object 受信者 (下記参照)
subject string メール件名
body string メール本文 (プレーンテキスト)

from および to のフィールド:

フィールド 説明
email string メールアドレス
name string 表示名

結果:

フィールド 説明
code number 0 — 成功、負の値 — エラー
log string SMTPダイアログ (デバッグ用)
error string エラーメッセージまたは ""

接続モード

モード ポート 設定
SMTPS (SSL) 465 ssl: true
STARTTLS 587 starttls: true
プレーンSMTP 25 フラグなし

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 (アプリパスワード)

Gmailでは通常のパスワードではなく、アプリパスワードが必要です。 有効化: Googleアカウント → セキュリティ → 2段階認証プロセス → アプリパスワード。 注意: GmailはSMTPをサポートしなくなりました。

ローカルSMTP (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"
});

設定ファイル経由での送信

設定ファイル 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"
  }
}

スクリプト 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デバッグ

エラー時は r.log を出力してください — 完全なSMTPダイアログが含まれています:

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