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 이메일 본문 (일반 텍스트)

fromto의 필드:

필드 타입 설명
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: "시스템 메일러" },
    to:   { email: "admin@example.com",   name: "관리자" },
    subject: "경고: 서비스 이용 불가",
    body: "MyService가 14:35에 응답을 중지했습니다."
});

if (r.code === 0) {
    console.log("이메일이 발송되었습니다.");
} else {
    console.log("오류:", r.error);
    console.log("SMTP 로그:", 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: "메일러" },
    to:   { email: "admin@internal.corp", name: "관리자" },
    subject: "경고",
    body: "문제가 발생했습니다."
});

설정을 통한 발송

설정 파일 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": "시스템 메일러"
    },
    "to": {
      "email": "recipient@example.com",
      "name": "수신자"
    },
    "subject": "테스트 메시지",
    "body": "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 로그 ===");
    console.log(r.log);
    console.log("=== 오류 ===");
    console.log(r.error);
}