How To Send Emails Using Your Python 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.py then paste the following code.

Pterodactyl Panel, Create File dialog. Enter File Name (index.py)

What this code does:

  • Creates a Discord bot that listens for commands. We use the discord.py library for this.
  • The bot uses ! as its starting symbol (the command prefix).
  • Adds a command:
  • When a user types the command, the bot will:
    • Separate the message: It easily grabs the first word after !mail as the email address to send to, and the rest of the text as the email body.
    • Build the Email: It uses a special Python tool (MIMEText) to package the message content into a standard email format, setting a subject and who the email is From and To.
    • Connect to the Email Server (like connecting to Gmail or Outlook):
      • It finds the email server using the saved details (SMTP_HOST, SMTP_PORT).
      • It quickly calls server.starttls() to turn on security (encryption) , so your login details are safe.
      • It then logs in using the bot owner’s email and password (SMTP_USER, SMTP_PASS).
    • Send the email to the address you typed.
    • Reply in Discord with one of two simple messages:
      • “Email sent to **<recipient>**!” if everything worked perfectly.
      • “Email failed. Error: **<error message>**” if there was a problem (like a wrong password or a typo in the recipient’s email).
    • Prevents Crashes: This is all done inside a special try...except block, which means if something goes wrong, the bot just sends the error message and keeps running instead of stopping completely.
  • Uses Python’s built-in smtplib tool for all the work of talking to the email server.
import discord
from discord.ext import commands
import smtplib
from email.mime.text import MIMEText

# ---- SMTP SETTINGS ----
SMTP_HOST = "smtp.gmail.com"
SMTP_PORT = 587
SMTP_USER = "[email protected]"
SMTP_PASS = "your_password_or_app_password"
# -----------------------

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    print("Bot is online!")

@bot.command()
async def mail(ctx, recipient: str, *, text: str):
    try:
        msg = MIMEText(text)
        msg["Subject"] = "Message from Discord Bot"
        msg["From"] = SMTP_USER
        msg["To"] = recipient

        server = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
        server.starttls()
        server.login(SMTP_USER, SMTP_PASS)
        server.sendmail(SMTP_USER, recipient, msg.as_string())
        server.quit()

        await ctx.send(f"Email sent to **{recipient}**!")
    except Exception as e:
        await ctx.send(f"Email failed. Error: `{e}`")

bot.run("YOUR_DISCORD_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