Send React Emails - Tutorial 2026

ส่งอีเมลแอปเว็บ React พร้อมด้วยเทมเพลต HTML, CSS และ JSX ตัวอย่าง และการรวมเข้ากับการผลิต SMTP

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: 'you@example.com',
    pass: '****************************'
  },
});

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

const options = {
  from: 'you@example.com',
  to: 'user@gmail.com',
  subject: 'hello world',
  html
};

transporter.sendMail(options);

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

node app

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

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