---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://sendcorex.com/llms.txt"
---

Source: https://sendcorex.com/th/blog/send-email-nodejs

บทเรียน

# วิธีส่งอีเมลด้วย Node.js (Nodemailer และ REST API)

สองวิธี — Nodemailer ผ่าน SMTP หรือการเรียก fetch ธรรมดาไปยัง REST API พร้อมโค้ดที่ใช้งานได้และการตั้งค่า DNS ที่ช่วยไม่ให้เข้าสแปม

โดยทีม Sendcorex·อัปเดต 21 ก.ย. 2026·อ่าน 9 นาที

**สรุปสั้นๆ:** หากต้องการส่งอีเมลด้วย REST API ใน Node.js ให้ส่งคำขอ `POST` ด้วย `fetch` (มีในตัวตั้งแต่ Node 18) ไปยัง endpoint ส่งของผู้ให้บริการ ใส่ API key ใน header `Authorization` และ body JSON ที่มี from, to, subject และ html ไม่ต้องใช้ SDK ดูโค้ดเต็มใน ตัวเลือกที่ 2

มีสองวิธีที่ดีในการส่งอีเมลจาก Node.js: ผ่าน SMTP ด้วย Nodemailer หรือการเรียก `fetch` ธรรมดาไปยัง REST API ใช้ Node.js 18 ขึ้นไปก็พอสำหรับทั้งสองแบบ คุณยังต้องมีบัญชี Sendcorex และโดเมนส่งที่ยืนยันแล้ว (เผยแพร่ระเบียน SPF, DKIM และ DMARC ที่แดชบอร์ดให้ไว้ก่อนส่งจริง)

## ตัวเลือกที่ 1: Nodemailer (SMTP)

```
npm install nodemailer
```

```
import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  host: 'smtp.sendcorex.com',
  port: 587,
  secure: false, // STARTTLS on 587
  auth: { user: process.env.SENDCOREX_API_KEY, pass: process.env.SENDCOREX_API_KEY },
});

const info = await transporter.sendMail({
  from: '"Your App" <hello@yourdomain.com>',
  to: 'customer@example.com',
  subject: 'Welcome!',
  text: 'You are in.',
  html: '<h1>You are in.</h1>',
});

console.log('Sent:', info.messageId);
```

## ตัวเลือกที่ 2: REST API ด้วย fetch

ไม่มี dependency ใช้ได้ในรันไทม์ serverless และ edge ที่มักบล็อก SMTP ขาออก:

```
const response = await fetch('https://graph.sendcorex.com/v3.0/emails', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.SENDCOREX_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    to: 'customer@example.com',
    from: 'hello@yourdomain.com',
    subject: 'Welcome!',
    html: '<h1>You are in.</h1>',
  }),
});

if (!response.ok) throw new Error(`Send failed: ${response.status}`);
```

## ข้อผิดพลาดและการลองใหม่

| อาการ | วิธีแก้ |
| --- | --- |
| `Invalid login` | ไม่มี API key หรือผิด ตรวจว่าตัวแปรสภาพแวดล้อมถูกโหลดและไม่มีช่องว่างเกิน |
| `ETIMEDOUT` | โฮสต์บล็อกพอร์ต SMTP ลอง 587/465 หรือเปลี่ยนไปใช้ REST API |
| เข้าสแปม | ตรวจ SPF/DKIM ใน header ของ Gmail จริง ส่งทั้ง text และ html และเพิ่มปริมาณทีละน้อย |

การตอบกลับ 4xx หมายถึงคำขอผิด อย่าลองใหม่ 429 และ 5xx เป็นแบบชั่วคราว ลองใหม่ด้วย exponential backoff

## คำถามที่พบบ่อย

### ส่งอีเมลด้วย REST API ใน Node.js อย่างไร?

เรียก endpoint ส่งของผู้ให้บริการด้วย fetch: POST body JSON (from, to, subject, html) และใส่ API key ใน header Authorization Node.js 18+ มี fetch ในตัว จึงไม่ต้องใช้แพ็กเกจใด ตรวจสถานะการตอบกลับ และลองใหม่เฉพาะข้อผิดพลาด 429 และ 5xx

### ควรใช้ SMTP (Nodemailer) หรือ REST API?

REST API มักเร็วกว่าและให้ ID ข้อความกับรหัสข้อผิดพลาดที่ชัดเจนในการตอบกลับ SMTP ด้วย Nodemailer เหมาะกว่าเมื่อไลบรารีหรือเฟรมเวิร์กรองรับ SMTP อยู่แล้ว ทั้งสองส่งเหมือนกัน

### ติดตามอีเมลที่ส่งใน Node.js อย่างไร?

บันทึก ID ข้อความที่การเรียกส่งคืนมา แล้วใช้ webhook (delivered, bounced, opened, clicked) หรือ API บันทึกอีเมลเพื่อติดตามว่าเกิดอะไรกับแต่ละอีเมล

### ในหน้านี้

ส่งอีเมลแรกในไม่กี่นาที

สร้างบัญชีฟรี ยืนยันโดเมน แล้ววางโค้ดด้านบน

[เริ่มฟรี — 3,000 อีเมล/เดือน](https://app.sendcorex.com/authentication/register?appid=smtp)

ไม่ต้องใช้บัตรเครดิต

## อ่านต่อ

[พื้นฐาน ### SMTP คืออะไร? คู่มือภาษาง่ายๆ SMTP คือโปรโตคอลที่ส่งอีเมลเกือบทุกฉบับบนอินเทอร์เน็ต นี่คือสิ่งที่มันทำจริงๆ ทีละขั้นตอน โดยไม่ใช้ศัพท์เทคนิคของ RFC อ่านคู่มือ](https://sendcorex.com/th/blog/what-is-smtp) [การส่งถึง ### อธิบาย SPF, DKIM และ DMARC ระเบียน DNS สามตัวคั่นระหว่างอีเมลของคุณกับกล่องจดหมาย นี่คือสิ่งที่แต่ละตัวพิสูจน์จริงๆ และสิ่งที่เกิดขึ้นเมื่อขาดตัวใดตัวหนึ่ง อ่านคู่มือ](https://sendcorex.com/th/blog/spf-dkim-dmarc-explained) [บทเรียน ### ส่งอีเมลจาก Next.js (App Router และ Route Handler) Route Handler, Server Action และเทมเพลต React Email พร้อมฟอร์มติดต่อจริง การจัดการข้อผิดพลาด และการตั้งค่า DNS อ่านคู่มือ](https://sendcorex.com/th/blog/send-email-nextjs)

ส่งอีเมลแรกในไม่กี่นาที

สร้างบัญชีฟรี ยืนยันโดเมน แล้ววางโค้ดด้านบน

ไม่ต้องใช้บัตรเครดิต

[เริ่มฟรี — 3,000 อีเมล/เดือน](https://app.sendcorex.com/authentication/register?appid=smtp) [ดูราคา](https://sendcorex.com/th/pricing)
