---
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-nextjs

บทเรียน

# ส่งอีเมลจาก Next.js (App Router และ Route Handler)

วิธีของ App Router: Server Action เรียก REST API โดยตรง ไม่ต้องใช้ไคลเอนต์ SMTP

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

## ทำไมที่นี่ใช้ REST แทน SMTP

การ deploy Next.js ส่วนใหญ่ทำงานบนฟังก์ชัน serverless หรือ edge ซึ่งการเชื่อมต่อ SMTP ขาออกที่ยาวนานไม่เสถียรหรือถูกบล็อกทั้งหมด การส่ง HTTPS POST ครั้งเดียวจาก Server Action เลี่ยงปัญหานี้ได้ทั้งหมด

## Server Action สำหรับฟอร์มติดต่อ

```
// app/actions.ts
'use server';

export async function sendContactMessage(formData: FormData) {
  const email = formData.get('email') as string;
  const message = formData.get('message') as string;

  const res = 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: 'you@yourdomain.com',
      from: 'hello@yourdomain.com',
      replyTo: email,
      subject: 'New contact message',
      text: message,
    }),
  });

  if (!res.ok) throw new Error('Could not send message');
}
```

## หรือ Route Handler หากอยากเรียกจากฝั่งไคลเอนต์

```
// app/api/contact/route.ts
export async function POST(request: Request) {
  const { email, message } = await request.json();

  const res = 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: 'you@yourdomain.com', from: 'hello@yourdomain.com', replyTo: email, subject: 'New contact message', text: message }),
  });

  return Response.json({ ok: res.ok }, { status: res.ok ? 200 : 502 });
}
```

## เรนเดอร์ด้วย React Email

สำหรับเนื้อหาที่มากกว่าข้อความธรรมดา ให้เรนเดอร์คอมโพเนนต์ React Email เป็น HTML ด้วย `@react-email/render` แล้วส่งผลลัพธ์เป็น `html` ใน body คำขอ เทมเพลตจะยังเป็นคอมโพเนนต์แทนการต่อสตริง

## เช็กลิสต์

- เก็บ `SENDCOREX_API_KEY` ไว้ฝั่งเซิร์ฟเวอร์เท่านั้น อย่าเปิดเผยผ่าน `NEXT_PUBLIC_`
- ตั้ง `replyTo` เป็นที่อยู่ของผู้เยี่ยมชม และใช้ `from` บนโดเมนที่ยืนยันแล้ว
- จำกัดอัตราการเรียก route ไม่อย่างนั้นบอตจะใช้ฟอร์มส่งข้อความหาคุณเป็นพันๆ ฉบับ

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

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

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

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

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

## อ่านต่อ

[บทเรียน ### วิธีส่งอีเมลด้วย Node.js (Nodemailer และ REST API) สองวิธีส่งอีเมลจาก Node.js — Nodemailer ผ่าน SMTP หรือการเรียก fetch ธรรมดา พร้อมโค้ดที่ใช้งานได้ การจัดการข้อผิดพลาด และการตั้งค่า DNS อ่านคู่มือ](https://sendcorex.com/th/blog/send-email-nodejs) [พื้นฐาน ### SMTP คืออะไร? คู่มือภาษาง่ายๆ SMTP คือโปรโตคอลที่ส่งอีเมลเกือบทุกฉบับบนอินเทอร์เน็ต นี่คือสิ่งที่มันทำจริงๆ ทีละขั้นตอน โดยไม่ใช้ศัพท์เทคนิคของ RFC อ่านคู่มือ](https://sendcorex.com/th/blog/what-is-smtp) [พื้นฐาน ### อีเมลทรานแซกชันกับการตลาด: ทำไมการแยกจึงสำคัญ การรีเซ็ตรหัสผ่านกับการส่งโปรโมชันไม่ใช่อีเมลประเภทเดียวกัน และการส่งแบบเดียวกันอาจทำให้ข้อความที่สำคัญที่สุดของคุณไว้ใจไม่ได้ อ่านคู่มือ](https://sendcorex.com/th/blog/transactional-vs-marketing-email)

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

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

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

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