Mẫu liên hệ JavaScript Ví dụ về mã Node.js vào 2024

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 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, nhật ký gửi email và bản xem trước HTML/văn bản thuần túy/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 iOStạo mẫu email với Node.js.