Ví dụ về mã Node.js cho biểu mẫu liên hệ JavaScript năm 2025
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 viết bởi
Forward Email Đội
Đã xuất bản
8/1/25
Đến giờ đọc rồi
Ít hơn 5 phút
Cài đặt và Yêu cầu
Bạn sẽ cần cài đặt nodemailer
npm dependency:
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ó là Chuyển tiếp Email để gửi và xem trước thư đ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: replace `user` and `pass` values from:
// <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 gửi email theo thời gian thực, nhật ký khả năng gửi email và bản xem trước HTML/văn bản thuần túy/tệp đính kèm.
P.S. 🎉 Bạn cũng có thể xem trước email trong trình duyệt và Trình mô phỏng iOS và tạo mẫu email bằng Node.js.