JavaScript Contact Forms Node.js Code Example in 2024

Create and send JavaScript contact forms with Node, React, React Native, Koa, Express, Fastify, and Nodemailer SMTP.

Install and Requirements

You will need to install nodemailer npm dependency:

npm install nodemailer

Source Code and Example

This example uses the Nodemailer library and its official sponsor Forward Email to send and preview outbound mail.

You will need to Generate Password to send outbound mail – please follow our Send Email with Custom Domain SMTP Guide.

// 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>'
});

Run the app to send the email:

node app

Now you can go to My Account → Emails to see your real-time email delivery status, email deliverability logs, and HTML/plaintext/attachment previews.

P.S. 🎉 You can also preview emails in browsers and the iOS Simulator and create email templates with Node.js.