Send React Emails - Tutorial 2026

Send emails from React and Node.js web apps using HTML, CSS, and JSX templates. Production-ready SMTP integration guide with code examples and best practices.

React email templates illustration

การติดตั้งและความต้องการ

คุณจะต้องติดตั้ง dependencies npm @react-email/render และ nodemailer:

npm install @react-email/render nodemailer

ซอร์สโค้ดและตัวอย่าง

สร้างเทมเพลตอีเมลของคุณด้วยไฟล์ .jsx หรือ .js:

// email.jsx
import * as React from 'react';
import { Html } from '@react-email/html';
import { Button } from '@react-email/button';

export function Email(props) {
  const { url } = props;

  return (
    <Html lang="en">
      <Button href={url}>เยี่ยมชมเว็บไซต์ของเรา</Button>
    </Html>
  );
}

ในตัวอย่างนี้ เราใช้ไลบรารี Nodemailer และผู้สนับสนุนอย่างเป็นทางการของมัน Forward Email เพื่อส่งและดูตัวอย่างอีเมลขาออก

คุณจะต้อง สร้างรหัสผ่าน เพื่อส่งอีเมลขาออก – กรุณาทำตาม คู่มือส่งอีเมลด้วย SMTP โดเมนที่กำหนดเอง

// app.js
import { render } from '@react-email/render';
import nodemailer from 'nodemailer';
import { Email } from './email';

const transporter = nodemailer.createTransport({
  host: 'smtp.forwardemail.net',
  port: 465,
  secure: true,
  auth: {
    // TODO: แทนที่ค่า `user` และ `pass` จาก:
    // <https://forwardemail.net/guides/send-email-with-custom-domain-smtp>
    user: '[email protected]',
    pass: '****************************'
  },
});

const html = render(Email({ url: "https://example.com" }));

const options = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'hello world',
  html
};

transporter.sendMail(options);

รันแอปเพื่อส่งอีเมล:

node app

ตอนนี้คุณสามารถไปที่ บัญชีของฉัน → อีเมล เพื่อดูสถานะการส่งอีเมลแบบเรียลไทม์, บันทึกการส่งอีเมล, และดูตัวอย่าง HTML/ข้อความธรรมดา/ไฟล์แนบ

ป.ล. 🎉 คุณยังสามารถ ดูตัวอย่างอีเมลในเบราว์เซอร์และ iOS Simulator และ สร้างเทมเพลตอีเมลด้วย Node.js ได้อีกด้วย