JavaScript Contact Forms Node.js - Tutorial 2026

Tạo và gửi biểu mẫu liên hệ JavaScript bằng Node, React, React Native, Koa, Express, Fastify và Nodemailer SMTP.

Cài đặt và Yêu cầu

Bạn sẽ cần cài đặt phụ thuộc npm nodemailer:

npm install nodemailer

Mã Nguồn và Ví dụ

Ví dụ này sử dụng thư viện Nodemailer và nhà tài trợ chính thức của nó Forward Email để gửi và xem trước thư gửi đi.

Bạn sẽ cần Tạo Mật khẩu để gửi thư đi – vui lòng làm theo Hướng dẫn Gửi Email với SMTP Tên Miền Tùy Chỉnh của chúng tôi.

// app.js
import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  host: 'smtp.forwardemail.net',
  port: 465,
  secure: true,
  auth: {
    // TODO: thay thế giá trị `user` và `pass` từ:
    // <https://forwardemail.net/guides/send-email-with-custom-domain-smtp>
    user: 'you@example.com',
    pass: '****************************'
  },
});

await transporter.sendMail({
  from: 'you@example.com',
  to: 'user@gmail.com',
  subject: 'hello world',
  html: '<h1>hello world</h1>'
});

Chạy ứng dụng để gửi email:

node app

Bây giờ bạn có thể truy cập Tài khoản của tôi → Email để xem trạng thái giao nhận email theo thời gian thực, nhật ký khả năng gửi email, và xem trước HTML/văn bản thuần/đính kèm.

P.S. 🎉 Bạn cũng có thể xem trước email trên trình duyệt và iOS Simulatortạo mẫu email với Node.js.