예제 보내기

Node.js로 보내기

내장된 페치를 사용하여 Node.js 18+ 프로젝트에서 이메일을 보냅니다.

전제 조건

  • A Sendtern API 키로 저장 SENDTERN_API_KEY
  • Node.js 18 or newer, which ships with fetch
  1. 1

    이메일 보내기

    Create send.mjs. No package is needed.

    1// send.mjs
    2const res = await fetch("https://sendtern.com/emails", {
    3  method: "POST",
    4  headers: {
    5    Authorization: `Bearer ${process.env.SENDTERN_API_KEY}`,
    6    "Content-Type": "application/json",
    7    "Idempotency-Key": crypto.randomUUID(),
    8  },
    9  body: JSON.stringify({
    10    from: "Acme <hello@mail.example.com>",
    11    to: ["you@example.com"],
    12    subject: "Hello from Node.js",
    13    html: "<p>It works!</p>",
    14  }),
    15});
    16
    17if (!res.ok) throw new Error((await res.json()).message);
    18console.log(await res.json());
    
  2. 2

    응답을 확인하십시오

    성공적인 요청은 이메일 id와 함께 200 OK를 반환합니다. 이를 사용하여 나중에 이메일을 찾을 수 있습니다.

    1{
    2  "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d"
    3}
    
1node --env-file=.env send.mjs

예제 발신자를 확인된 도메인의 주소로 바꿉니다.