DEV Community

SWAPNIL AHMMED SHISHIR
SWAPNIL AHMMED SHISHIR

Posted on

How to Send Emails on User Sign-Up with Node.js, Express.js & cPanel

Great! You're using Node.js and Express.js, and your website is hosted on a cPanel server. You want to send an email at sign-up time.

Here’s how you can do it step by step:


✅ Step-by-Step: Send Email After Signup (Node.js + Express + cPanel)

🔧 1. Install nodemailer

Run this in your project folder:

npm install nodemailer
Enter fullscreen mode Exit fullscreen mode

📧 2. Create an Email Account in cPanel

  1. Go to your cPanel.
  2. Click on Email Accounts.
  3. Create an email (e.g. no-reply@yourdomain.com).
  4. Go to Connect Devices or Configure Email Client to get SMTP details.

For example:

Setting Value
SMTP Host mail.yourdomain.com
SMTP Port 465 or 587
Username no-reply@yourdomain.com
Password (your email password)
Encryption SSL (for port 465) or TLS (587)

🧑‍💻 3. Send Email in Signup Route

Here’s a sample Express route that sends an email:

const express = require("express");
const nodemailer = require("nodemailer");
const router = express.Router();

router.post("/signup", async (req, res) => {
  const { email, name } = req.body;

  // 1. Send the email
  const transporter = nodemailer.createTransport({
    host: "mail.yourdomain.com",
    port: 465, // or 587
    secure: true, // true for port 465, false for 587
    auth: {
      user: "no-reply@yourdomain.com",
      pass: "your_email_password",
    },
  });

  const mailOptions = {
    from: '"Your Website" <no-reply@yourdomain.com>',
    to: email,
    subject: "Welcome to Our Site!",
    html: `<p>Hi ${name},</p><p>Thanks for signing up!</p>`,
  };

  try {
    await transporter.sendMail(mailOptions);
    res.status(200).json({ message: "Signup successful. Email sent!" });
  } catch (error) {
    console.error("Email error:", error);
    res.status(500).json({ error: "Signup successful but email failed." });
  }
});

module.exports = router;
Enter fullscreen mode Exit fullscreen mode

🧪 4. Test Your API

Make a POST request to /signup with body:

{
  "email": "user@example.com",
  "name": "John Doe"
}
Enter fullscreen mode Exit fullscreen mode

Use Postman or your frontend form.


✅ Optional but Recommended

  • Make sure your domain's DNS records include proper SPF, DKIM, and DMARC (cPanel > Email Deliverability).
  • Avoid using @gmail.com as sender; always use your domain email like no-reply@yourdomain.com.

Top comments (0)