Linux/Unix

How to send email by telnet on Linux

Telnet is a network protocol that allows a user to remotely access and control another computer over the Internet or local area network (LAN).

We can also use it to check the state of a remote port if it’s open or note. But did you know that we can use Telnet to send email ?

Sending emails using this protocol may seem like a daunting task at first, but it can be a useful skill for troubleshooting email communication.

In this step-by-step guide, we will how we can of sending an email using Telnet.

Before we begin:

  • Ensure that Telnet is installed on your Linux system. If it is not, you can installet using your system’s package manager ( dnf, yum or apt ) depending on your Linux distribution ( RHEL, ubuntu …).
  • Have the necessary information on hand, including the SMTP server address, port number.

Important: Telnet is not a common method nowadays due to security concerns, and it’s generally recommended to use dedicated email libraries or tools for this purpose ( like postfix or xmail) But like I said, it can be a useful skill for troubleshooting email communication. Or for educational purposes.

Step 1: Connect to the SMTP server

First use the command bellow to connecte to your SMTP server :

# telnet smtp.example.com 25

Step 2 : Greet the SMTP server

After successfully connecting, the server will respond with a welcome message. You can greet the server with EHLO command :

EHLO

Step 3 : specify the sender :

Start the email conversation with the sender email

MAIL FROM: <your_email@example.com>

Step 4 : specify the recipient

Enter the recipient’s email address with the following command :

RCPT TO: <recipient_email@example.com>

Step 5 : Begin the email data entry

Start the data entry for your email by typing :

DATA

Press Enter and enter your email content including the subject and body. To end the email, type a « . » on a new line and press Enter.

Step 6 : Quit the Telnet session

To exit the Telnet session, use « quit » command :

QUIT

Sending email by telnet using Script

If you want to script the email sending process using Telnet without interactive mode, you can create a script to automate the commands. Below is an example using a Bash script :

#!/bin/bash

# Replace these variables with your actual information
SMTP_SERVER="smtp.your-email-provider.com"
FROM_EMAIL="your-email@example.com"
TO_EMAIL="recipient-email@example.com"
SUBJECT="Your Subject"
BODY="Your email content goes here."

# Connect to the SMTP server
{
  sleep 1
  echo "EHLO example.com"
  sleep 1
  echo "MAIL FROM: <$FROM_EMAIL>"
  sleep 1
  echo "RCPT TO: <$TO_EMAIL>"
  sleep 1
  echo "DATA"
  sleep 1
  echo "Subject: $SUBJECT"
  echo "From: $FROM_EMAIL"
  echo "To: $TO_EMAIL"
  echo ""
  echo "$BODY"
  echo "."
  sleep 1
  echo "QUIT"
} | telnet $SMTP_SERVER 25

This script uses a code block to send a series of commands to Telnet. It introduces sleep commands to give Telnet some time to process each command.

Save the script to a file, make it executable by running the commande bellow :

# chmod +x telnet_script.sh

And then run it :

# ./telnet_script.sh

Can we send a n email with attachement using Telnet ?

Yes you can. Here’s a basic example that demonstrates how to send a simple text file as an attachment using Telnet :

#!/bin/bash

# Replace these variables with your actual information
SMTP_SERVER="smtp.your-email-provider.com"
FROM_EMAIL="your-email@example.com"
TO_EMAIL="recipient-email@example.com"
SUBJECT="Your Subject"
BODY="Please see the attached file."

# File path of the attachment
ATTACHMENT_FILE="path/to/your/file.txt"

# Encode the attachment file in Base64
BASE64_ATTACHMENT=$(base64 "$ATTACHMENT_FILE")

# Connect to the SMTP server
{
  sleep 1
  echo "EHLO example.com"
  sleep 1
  echo "MAIL FROM: <$FROM_EMAIL>"
  sleep 1
  echo "RCPT TO: <$TO_EMAIL>"
  sleep 1
  echo "DATA"
  sleep 1
  echo "Subject: $SUBJECT"
  echo "From: $FROM_EMAIL"
  echo "To: $TO_EMAIL"
  echo "MIME-Version: 1.0"
  echo "Content-Type: multipart/mixed; boundary=boundarystring"
  echo ""
  echo "--boundarystring"
  echo "Content-Type: text/plain"
  echo ""
  echo "$BODY"
  echo ""
  echo "--boundarystring"
  echo "Content-Type: application/octet-stream; name=\"$(basename "$ATTACHMENT_FILE")\""
  echo "Content-Transfer-Encoding: base64"
  echo "Content-Disposition: attachment; filename=\"$(basename "$ATTACHMENT_FILE")\""
  echo ""
  echo "$BASE64_ATTACHMENT"
  echo "--boundarystring--"
  echo "."
  sleep 1
  echo "QUIT"
} | telnet $SMTP_SERVER 25

Replace the placeholders and the ATTACHMENT_FILE variable with the path to your attachment file. This script assumes that the attachment is a text file.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *