How to Automatically Create Custom Email IDs for Users with Node.js and cPanel

How to Automatically Create Custom Email IDs for Users with Node.js and cPanel

To automatically create an email ID for a user with your custom domain email when they create an account on your website, you can use the nodemailer library in Node.js. Here's how you can implement this:

  1. Install nodemailer using npm:
npm install nodemailer
  1. Set up your custom email domain in your cPanel. Note down the SMTP server details, username, and password.

  2. In your Node.js code, import nodemailer and create a transporter object:

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  host: 'your-smtp-server',
  port: 465, // or 587 for TLS
  secure: true, // true for 465, false for other ports
  auth: {
    user: 'your-email-username',
    pass: 'your-email-password',
  },
});

Replace your-smtp-server, your-email-username, and your-email-password with the details for your custom email domain.

  1. When a user creates an account on your website, generate a unique username for them (if they haven't chosen one), and use it to create their email ID with your custom domain.
const username = generateUsername(); // replace with your own function to generate a unique username
const email = `${username}@your-domain.com`;
  1. Use the transporter object to send an email to your user with their new email ID.
const mailOptions = {
  from: 'your-name <your-email>',
  to: user.email,
  subject: 'Your new email ID',
  text: `Your new email ID is ${email}`,
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Replace your-name and your-email with your own name and email address.

That's it! When the user creates an account on your website, they will receive an email with their new email ID based on their username and your custom domain.