Mẫu liên hệ JavaScript Ví dụ về mã Node.js ( 2023 )
Tạo và gửi biểu mẫu liên hệ JavaScript với Node, React, React Native, Koa, Express, Fastify và Nodemailer SMTP.
- Search page
- Table of Contents
Cài đặt và yêu cầu
Bạn sẽ cần phải cài đặt nodemailer
phụ thuộc npm:
npm install nodemailer
Mã nguồn và ví dụ
Ví dụ này sử dụng Người gửi thư ghi chú thư viện và nhà tài trợ chính thức của nó Chuyển tiếp email để gửi và xem trước thư đi.
Bạn sẽ cần đến Tạo mật khẩu để gửi thư đi – vui lòng làm theo Gửi Email với Hướng dẫn SMTP miền tùy chỉnh.
// 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ể đi đến Tài khoản của tôi → Email để xem trạng thái gửi email theo thời gian thực của bạn, nhật ký khả năng gửi email và xem trước HTML/văn bản gốc/tệp đính kèm.
Tái bút 🎉 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 với Node.js.