How To Send Emails Using Your NodeJS Discord Bot

1. Log In to Your Panel


2. Select the ‘Files’ Tab

Cybrancee Panel in the files page, "Files" tab on the sidebar is highlighted

3. Create Your Main Bot’s File

Create a new file called index.js then paste the following code.

Cybrancee Panel, Create File dialog. Enter File Name (index.js)

What this code does:

  • Creates a Discord bot that uses ! as the command prefix.
  • It loads login details (SMTP settings and passwords) from a hidden environment file.
  • It sets up the nodemailer tool to handle connecting to your email server (like Gmail) for sending mail.
  • Adds a command:
  • When a user types the command, the bot will:
    • Separate the message: It grabs the recipient’s email and the message body.
    • Connect Securely: It connects and logs in to the email server, automatically using encryption (TLS/SSL) to protect the login.
    • Send the Email using the transporter.sendMail() function.
    • Reply in Discord with one of two simple messages:
      • “Email sent to **<recipient>**!” if it works.
      • “Email failed. Error: **<error message>**” if there is a problem.
    • Prevents Crashes: It uses try...catch so errors are reported but the bot stays online.
  • Uses discord.js for chat and nodemailer for email sending.
const { Client, IntentsBitField } = require('discord.js');
const nodemailer = require('nodemailer');

const SMTP_HOST = 'smtp.gmail.com';
const SMTP_PORT = 587;
const SMTP_USER = 'USER';
const SMTP_PASS = 'PASSWORD';

const transporter = nodemailer.createTransport({
    host: SMTP_HOST,
    port: SMTP_PORT,
    secure: SMTP_PORT === 465,
    auth: {
        user: SMTP_USER,
        pass: SMTP_PASS,
    },
});

const client = new Client({ 
    intents: [
        IntentsBitField.Flags.Guilds,
        IntentsBitField.Flags.GuildMessages,
        IntentsBitField.Flags.MessageContent,
    ] 
});

client.on('ready', () => {
    console.log(`Bot is online! Logged in as ${client.user.tag}`);
});

const PREFIX = '!';

client.on('messageCreate', async message => {
    if (message.author.bot || !message.content.startsWith(PREFIX)) return;

    const parts = message.content.slice(PREFIX.length).trim().split(/\s+/);
    const command = parts.shift().toLowerCase();

    if (command === 'mail') {
        if (parts.length < 2) {
            return message.channel.send('Please use the format: `!mail [email protected] Your message here`');
        }

        const recipient = parts[0];
        const text = parts.slice(1).join(' '); 

        try {
            await transporter.sendMail({
                from: SMTP_USER,
                to: recipient,
                subject: "Message from Discord Bot",
                text: text,
            });

            await message.channel.send(`Email sent to **${recipient}**!`);
        } catch (error) {
            await message.channel.send(`Email failed. Error: \`${error.message}\``);
        }
    }
});

client.login("YOUR_BOT_TOKEN");

Discord Bot Hosting

Starts at $1.49

External link icon
Was this article helpful?
Please Share Your Feedback
How Can We Improve This Article?
Table of Contents